In this example we will create a gui to switch on and off the LED that we used in the PWM example – Controlling the Brightness of an LED
Here is a reminder of the schematics
Code
Save the following as gpiogui.py
from Tkinter import *
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
class App:
def __init__(self, master):
frame = Frame(master)
frame.pack()
self.check_var = BooleanVar()
check = Checkbutton(frame, text='Pin 18',
command=self.update,
variable=self.check_var, onvalue=True, offvalue=False)
check.grid(row=1)
def update(self):
GPIO.output(18, self.check_var.get())
root = Tk()
root.wm_title('GPIO GUI Example')
app = App(root)
root.geometry("200x50+0+0")
root.mainloop()
Testing
Run from the command line by typing in
sudo python gpiogui.py