Coding a Graphical User Interface (GUI) in Python
Coding a Graphical User Interface (GUI) in Python
Coding a graphical user interface in Python will load a user window upon running your script. This may prove beneficial when using the RPi for robotics and wish to create a graphical interface for controls. The following section will offer an amazing tutorial video that will allow you to program an RPi interface for a simple LED using Python.
The following code has been copied from the 'Raspberry Pi Workshop for Beginners' page posted by CORE ELECTRONICS and can be seen in its original formal by visiting their website HERE.
## Toggle an LED when the GUI button is pressed ##
from tkinter import *
import tkinter.font
from gpiozero import LED
import RPi.GPIO
RPi.GPIO.setmode(RPi.GPIO.BCM)
### HARDWARE DEFINITIONS ###
led=LED(14)
### GUI DEFINITIONS ###
win = Tk()
win.title("LED Toggler")
myFont = tkinter.font.Font(family = 'Helvetica', size = 12, weight = "bold")
### Event Functions ###
def ledToggle():
if led.is_lit:
led.off()
ledButton["text"]="Turn LED on" # Change only the button text property
else:
led.on()
ledButton["text"]="Turn LED off"
def close():
RPi.GPIO.cleanup()
win.destroy()
### WIDGETS ###
# Button, triggers the connected command when it is pressed
ledButton = Button(win, text='Turn LED on', font=myFont, command=ledToggle, bg='bisque2', height=1, width=24)
ledButton.grid(row=0,column=1)
exitButton = Button(win, text='Exit', font=myFont, command=close, bg='red', height=1, width=6)
exitButton.grid(row=2, column=1)
win.protocol("WM_DELETE_WINDOW", close) # cleanup GPIO when user closes window
win.mainloop() # Loops forever