Home Learning Basic Python GPIO GUI example

Basic Python GPIO GUI example

by shedboy71

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

pi-pwm-led_schem

Code

Save the following as gpiogui.py

[codesyntax lang=”python”]

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()

[/codesyntax]

 

Testing

Run from the command line by typing in

sudo python gpiogui.py

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More