Feb-04-2020, 03:57 PM
I'm trying to write a simple bit of code with tkinter to enable me to switch on and off all the gpio pins with checkbuttons. I've got a basic example working but I'm sure there is a way of simplifying it rather than creating one checkbox at a time! I'm a beginner and don't really understand classes, I had a go and created all the checkboxes with a loop but I couldn't get the tick boxes to work. Here's my code so far, can anyone show me how to do it more efficiently? It's only got 3 gpios to save on space but I'm sure you'll get the gist of it!
from tkinter import *
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
for x in range(0, 27):
GPIO.setup(x, GPIO.OUT)
#GPIOPins=["GP14", "GP15", "GP18"]
root = Tk()
#Give window title
root.title("GPIO Control")
#Set up window geometry
root.geometry('200x650')
#Set up first label
label1 = Label(root, text="Select pin")
label1.grid(column=0, row=0)
def activatePin14():
if var14.get() == 1:
GPIO.output(14, TRUE)
elif var14.get() == 0:
GPIO.output(14, FALSE)
def activatePin15():
if var15.get() == 1:
GPIO.output(15, TRUE)
elif var15.get() == 0:
GPIO.output(15, FALSE)
def activatePin18():
if var18.get() == 1:
GPIO.output(18, TRUE)
elif var18.get() == 0:
GPIO.output(18, FALSE)
var14 = IntVar()
Checkbutton(root, text="Pin 14" , command=activatePin14, variable=var14).grid(row=1, sticky=W)
var15 = IntVar()
Checkbutton(root, text="Pin 15" , command=activatePin15, variable=var15).grid(row=2, sticky=W)
var18 = IntVar()
Checkbutton(root, text="Pin 18" , command=activatePin18, variable=var18).grid(row=3, sticky=W)
Button(root, text="QUIT", command=root.destroy).grid(column=0, row=4)
root.mainloop()
Many thanks.
