[Tkinter] How can I simplify this code? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [Tkinter] How can I simplify this code? (/thread-24212.html) |
How can I simplify this code? - frednet - Feb-04-2020 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. RE: How can I simplify this code? - Barrowman - Feb-04-2020 As this cannot be tested on anything except a raspberry Pi you might be better off trying the forum there. Also instead of having different functions for each pin why not pass a variable to one function and let it sort out which pin to address? RE: How can I simplify this code? - frednet - Feb-04-2020 Thanks for your suggestion, I did look at using loops and arrays but python doesn't seem much like c and I'm a bit lost. RE: How can I simplify this code? - frednet - Feb-13-2020 Well I've got this far but I'm still stuck as to how read individual checkboxes. from tkinter import * from tkinter.ttk import * root = Tk() root.title("GPIO pin tool") root.geometry('200x540') PinNo = {'Pin 2': 0, 'Pin 3': 0, 'Pin 4': 0, 'Pin 5': 0, 'Pin 6': 0, 'Pin 7': 0, 'Pin 8': 0, 'Pin 9': 0, 'Pin 10': 0, 'Pin 11': 0, 'Pin 12': 0, 'Pin 13': 0, 'Pin 14': 0, 'Pin 15': 0, 'Pin 16': 0, 'Pin 17': 0, 'Pin 18': 0, 'Pin 19': 0, 'Pin 20': 0, 'Pin 21': 0, 'Pin 22': 0, 'Pin 23': 0, 'Pin 24': 0, 'Pin 25': 0, 'Pin 26': 0} def my_upd(): print('Check box value :',PinNo[machine].get()) for machine in PinNo: PinNo[machine] = Variable() l = Checkbutton(root, text=machine, variable=PinNo[machine] ,command=my_upd) l.pack() root.mainloop()I want to be able to read the state of the checkboxes and toggle the output to turn the pins on or off. It doesn't need to be Pi specific, just a way of toggling the variable, I can sort out the pins later. Can anyone explain how I go about ths because everything I've tried has failed! |