Blog Pages

1/17/18

Raspberry Pi 3 GUI - Part 7A - 3 LED Control Buttons with Tkinter

Controlling 3 LED's with Tkinter on Raspberry Pi 3. This code adds some color to the status text so it's easier to tell when the LED's are on.

(1) WINDOW WITH 3 BUTTONS - LED's "ON"




(2) WINDOW - LED's "OFF"




(3) CODE
#! /usr/bin/env python

#import Tkinter module, GPIO and atexit
from Tkinter import*
import RPi.GPIO as GPIO
import atexit

# setup GPIO 
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.OUT)#red led
GPIO.setup(11,GPIO.OUT)#yellow led
GPIO.setup(15,GPIO.OUT)#green led

# create window, window title, window size
window = Tk()
window.title( 'tkinter7.PY')
window.geometry('300x200')

# define dynamic properties for led button
def red_led_on():
    GPIO.output(7, True)
    red_led_status.configure(text='ON',bg='red')
    button_red_led.configure(command=red_led_off)
    
def red_led_off():
    GPIO.output(7, False)
    red_led_status.configure(text='OFF',bg='white')
    button_red_led.configure(command=red_led_on)

def yel_led_on():
    GPIO.output(11, True)
    yel_led_status.configure(text='ON',bg='yellow')
    button_yel_led.configure(command=yel_led_off)

def yel_led_off():
    GPIO.output(11, False)
    yel_led_status.configure(text='OFF',bg='white')
    button_yel_led.configure(command=yel_led_on)

def grn_led_on():
    GPIO.output(15, True)
    grn_led_status.configure(text='ON',bg='green')
    button_grn_led.configure(command=grn_led_off)

def grn_led_off():
    GPIO.output(15, False)
    grn_led_status.configure(text='OFF',bg='white')
    button_grn_led.configure(command=grn_led_on)

def cleanup():
        GPIO.output(7, False)
        GPIO.output(11, False)
        GPIO.output(15, False)
        GPIO.cleanup()

# create text box 
red_led_status = Label(window, relief='flat', width=4)
yel_led_status = Label(window, relief='flat', width=4)
grn_led_status = Label(window, relief='flat', width=4)

# create button
button_red_led = Button(window)
button_yel_led = Button(window)
button_grn_led = Button(window)

# set button text
button_red_led.configure(text='RED LED',command=red_led_on)
button_yel_led.configure(text='YEL LED',command=yel_led_on)
button_grn_led.configure(text='GRN LED',command=grn_led_on)

# set initial status text
red_led_status.configure(text='OFF',bg='white')                     
yel_led_status.configure(text='OFF',bg='white') 
grn_led_status.configure(text='OFF',bg='white') 

# set location of button and text
button_red_led.grid(row=1,column=1,columnspan=1)
red_led_status.grid(row=1,column=2,padx=10)
button_yel_led.grid(row=2,column=1,columnspan=1)
yel_led_status.grid(row=2,column=2,padx=10)
button_grn_led.grid(row=3,column=1,columnspan=1)
grn_led_status.grid(row=3,column=2,padx=10)

atexit.register(cleanup)
window.mainloop()






1/16/18

Raspberry Pi 3 GUI - Part 6B - LED Control Button with Tkinter

Controlling an LED with RPi.GPIO - Part 6B


This demonstrates one way to control functions of a microphone preamplifier, such as phantom power, using a software interface. Code can be found in Part 6A.

(1) LED CONTROL PANEL "LED = OFF"


(2) LED ON BREADBOARD = "OFF"


(3) LED CONTROL PANEL "LED = ON"


(4) LED ON BREADBOARD "LED = ON" 


NEXT UP - ADDING TWO MORE CONTROLS

Thanks for reading,
Trenton

1/9/18

Raspberry Pi 3 GUI - Part 6A - LED Control Button with Tkinter

Controlling an LED with RPi.GPIO

This program creates a button that accesses the hardware pins on the Raspberry Pi 3 to control an LED. The status of the LED is updated each time the user presses the button.

(1) LED CONTROL CODE
Add this code to your Python 3 editor.

#! /usr/bin/env python

#import Tkinter module, GPIO and atexit
from Tkinter import*
import RPi.GPIO as GPIO
import atexit

# setup GPIO 
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.OUT)

# create window, window title, window size
window = Tk()
window.title( 'tkinter6.PY')
window.geometry('300x200')

# define dynamic properties for led button
def red_led_on():
    GPIO.output(7, True)# set pin 7 to logic "high"
    red_led_status.configure(text='ON')
    button_red_led.configure(command=red_led_off)
    
def red_led_off():
    GPIO.output(7, False)# set pin 7 to logic "low"
    red_led_status.configure(text='OFF')
    button_red_led.configure(command=red_led_on)

def cleanup():
        GPIO.output(7, False);# reset pin 7 to logic low
        GPIO.cleanup()# cleanup GPIO

# create text box 
red_led_status = Label(window, relief='flat',width=4)

# create button
button_red_led = Button(window)

# set button text
button_red_led.configure(text='RED LED',command=red_led_on)

# set status text
red_led_status.configure(text='OFF')                     

# set location of button and text
button_red_led.grid(row=1,column=1,columnspan=1)
red_led_status.grid(row=1,column=2,padx=10)

atexit.register(cleanup)
window.mainloop()


(2) EXAMPLE CODE
My code looks like this:

blizzard tkinter python led control code

(3) SAVE WORK. RUN PROGRAM
Save to your desktop as tkinter6.py
Change directory to your desktop in your terminal editor.
Type chmod 755 tkinter6.py. Press Enter.
Type ./tkinter6.py . Press Enter.

(4) RESULTS - LED "OFF"
Screen Capture of the program window showing button and status text.
Notice that the LED is initially "OFF".



(5) RESULTS - LED  "ON"
Now notice that after pressing the button the LED is "ON". Press the button one more time and the LED will be OFF again.

blizzard tkinter python led control

UP NEXT - HARDWARE HOOKUP - PART 6B

Thanks for reading,
Trenton

Raspberry Pi 3 GUI - Part 5 - 3 Control Buttons and Text with Tkinter

3 Control Buttons with aligned text using Tkinter


This program creates 3 buttons that will be used to control 3 LED's. The text is aligned to the buttons using ".grid" and will eventually display the state of the LED.

(1) Enter this code into your Python 3 window

#! /usr/bin/env python

from Tkinter import*

window = Tk()
window.title( 'tkinter5.py')
window.geometry('500x300')

# create text items
red_led_text = Label(window, relief='sunken', width=15)
yellow_led_text = Label(window, relief='sunken',width=15)
green_led_text = Label(window, relief='sunken', width=15)

# create buttons for controlling LED's
red_led_button = Button(window)
yellow_led_button = Button(window)             
green_led_button = Button(window)

# set initial button text
red_led_button.configure(text='RED (ON / OFF)', command=exit)
yellow_led_button.configure(text='YEL (ON / OFF)', command=exit)
green_led_button.configure(text='GRN (ON / OFF)', command=exit)

# set initial text values
red_led_text.configure(text='RED LED IS OFF')
yellow_led_text.configure(text='YEL LED IS OFF')
green_led_text.configure(text='GRN LED IS OFF')

# set location of buttons
red_led_button.grid(row=1, column=1,padx=10)
yellow_led_button.grid(row=2, column=1,padx=10)
green_led_button.grid(row=3, column=1,padx=10)
       
# location of text                         
red_led_text.grid(row=1, column=3,padx=10 )
yellow_led_text.grid(row=2, column=3,padx=10 )
green_led_text.grid(row=3, column=3,padx=10 )

window.mainloop()

(2)
My code looks like this:
blizzard tkinter python code

(3)
Save your code as tkinter5.py to your desktop.
Change directory in your terminal window using cd Desktop.
Enter chmod 755 tkinter5.py in your terminal window. Press Enter
Enter ./tkinter5.py in your terminal window. Press Enter

(4)
Screen capture showing window with 3 buttons and aligned text.

blizzard tkinter python desktop

(5)
Next Up - Control and LED with a Button using GPIO.

 Thanks for reading,
Trenton

Raspberry Pi 3 GUI - Part 4 - Control Button and Text with Tkinter

Button and Text using Tkinter and Python

In this example the button aligns with the text using the ".grid" commands.
Pressing the ON/OFF button exits the program.

(1) Enter all code into your Python editor window
#! /usr/bin/env python

from Tkinter import*

window = Tk()
window.title( 'tkinter4.py')
window.geometry('500x300')

# create text item
text_box_1=Label(window, relief='sunken', width=15)

# create button
led_button = \
Button(window, text='ON/OFF', command=exit)

# set initial values here
text_box_1.configure(text='RED LED ON')

# set location of window items here
led_button.grid(row=1, column=1,padx=10)
text_box_1.grid(row=1, column=3,padx=10 )


window.mainloop()

(2)
My code looks like this:

trenton blizzard tkinter python
















(3)
Save to your desktop. Name your file tkinter4.py
Enter chmod 755 tkinter4.py into your terminal window. Press Enter.
Enter ./tkinter4.py into your terminal window. Press Enter.

(4)
Desktop screen capture (using scrot) showing button with text aligned with it.
Pressing the "ON/OFF" button closes the program.

blizzard tkinter python screen capture

(5)
UP NEXT? TWO MORE BUTTONS WITH ALIGNED TEXT

Thanks for reading,
Trenton



1/5/18

Raspberry Pi 3 GUI - Part 3 - Window Button using Tkinter

Window Button with TKINTER

This button will close the window when pressed.

(1)
Add code in BOLD to your python editor. Need help? See Part-1.

#! /usr/bin/env python
from Tkinter import*

window = Tk()
window.title( 'tkinter3.py')
window.geometry('500x300')

exit_button = \
Button(window, text='EXIT', command=exit)
exit_button.pack(padx=100, pady=100)
window.mainloop()

(2)
My code looks like this:


(3)
Save to your Desktop. Name your file tkinter3.py.
Change directory with cd Desktop in your terminal editor.

Enter chmod 755 tkinter3.py into your terminal editor.
Enter ./tkinter3.py into your terminal editor.

(4)
Desktop screen capture showing window with "EXIT" button. When you click the button the window will close.


(5)
Button Clicked - Window Closed


NEXT UP? CREATING TEXT IN YOUR WINDOW

Thanks for reading,
Trenton

Raspberry pi 3 GUI - Part 2 - Window Size in Tkinter

Setting Window Size In Tkinter
To set a window to an initial size we will be using ".geometry".

(1) 
Insert the BOLD code into your python editor. (See Part-1 for help with IDLE)

#! /usr/bin/env python
from Tkinter import*

window = Tk()
window.title( 'tkinter2.py')
window.geometry('500x300')

window.mainloop()

My code looks like this:


(2)
Save your work using the same procedure as in Part-1. Only this time name your file tkinter2.py.

(3)
Open your terminal window and change directory to your Desktop.
Enter cd Desktop in your terminal window. Click enter.
Enter chmod 755 tkinter2.py in your terminal editor. Click enter.
Enter ./tkinter2.py in your terminal window. Click enter.
Ignore my scrot commands in my terminal window.



(4)
If everything went OK then your screen should produce a window (below) with a larger size. Note: The window is not currently doing anything except opening.

Throughout these posts I will show you how to build a GUI that will actually do cool audio control stuff.

For now, practice the procedure I just showed you until you can enter all of the code without looking at mine. Also, play around with different window sizes while you are at it.

TKINTER2.PY GUI RESULTS:
Here is the screen capture for my Ras-pi-3 running tkinter2.py.


"Part-1" screen of tkinter1.py: Notice the size difference of the grey window?


NEXT UP? ADDING A SIMPLE BUTTON

Thanks for reading,
Trenton

Raspberry Pi 3 GUI Part 1 - Basic Window using TKinter

My son wanted to learn Python game programming so I bought him a raspberry pi 3. I've been working on getting the pi 3 to communicate to the GPIO pins on the PCB with plans to control audio hardware.

In order to communicate graphically with the outside world we need to create a basic window. Here's how using Python 3 and Tkinter:

First - Here's the current version I am working with on the pi3.




1. Goto Raspberry icon - Programming - Python 3 (IDLE)
and a new Python window opens up and looks like this




2. Goto FILE - NEW to open a clean page that looks like this:




3. Type the following code that is in BOLD only:

#! /usr/bin/env python 
from Tkinter import*
window.title( 'tkinter.py')
window.mainloop()

My code looks like this:



4. File - Save as "tkinter1.py" on your Desktop

5. Goto your Terminal Window - Note: (ignore the "scrot code")
SCROT is the screen capture application I am using




5. Change Directory to your Desktop using "cd Desktop"
Again: (ignore the "scrot line of code" in image below)




6. Type the following code in BOLD only into your terminal editor:
chmod 755 tkinter1.py 
(ignore the "scrot line of code" in image below)



7. Now type in your terminal window:
./tkinter.py



8. RESULTS:
If all goes well you should end up with this:




Common Errors:
(1)
#! /usr/bin/env python

This line of code must be the very first line in the program code otherwise you will get this error message in your terminal window:
from: can't read /var/mail/Tkinter
./tkinter1.py: line 8: syntax error near unexpected token `('
./tkinter1.py: line 8: `window = Tk()'


(2)
LOWER CASE "T" in Tk()
window = tk()
Traceback (most recent call last):
  File "./tkinter1.py", line 8, in
    window = tk()
NameError: name 'tk' is not defined


(3)
OMITTING "*" in from Tkinter import*
from Tkinter import
 File "./tkinter1.py", line 6
    from Tkinter import
                      ^
SyntaxError: invalid syntax


(4)
OMITTING "from" in from Tkinter import*
Tkinter import*
 File "./tkinter1.py", line 6
    Tkinter import*
                 ^
SyntaxError: invalid syntax

(5)
OMITTING "." in window.mainloop()
windowmainloop()
Traceback (most recent call last):
  File "./tkinter1.py", line 11, in
    windowmainloop()

NameError: name 'windowmainloop' is not defined

Next Up: Pre-sizing the window


Thanks for reading,
Trenton