Python Forum
[Tkinter] How can I simplify this code?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] How can I simplify this code?
#1
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.
Reply
#2
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?
Reply
#3
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.
Reply
#4
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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to simplify widget creation? flash77 6 868 Jul-21-2023, 08:03 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020