1

I want to toggle a pushbutton and show its changes on a label using tkinter.

If I press the button it shows "on" on the label and when I press again it shows "off" on the label

So I try these codes and If I'm trying the wrong code please help me write the correct using tkinter.

I have a problem in combining this code

import RPi.GPIO as GPIO

import time

GPIO.setmode(GPIO.BOARD)

GPIO.setup(22,GPIO.IN,up_down=GPIO.PUD_UP)

while(1):

    if GPIO.input(22)==1:

        if bs == False :

            x.set("on")

            bs=True

            sleep(0.5)

        else:

            x.set("off")

            bs=False

            sleep(0.5)  

This works okay but I want to connect it to a GUI label to print on it on or off.

Here is the tkinter code

import tkinter.*

root = tk()
x = StringVar()

s=Label(root,textvariable=x)

s.grid(column=0,row=0)

root.mainloop()

When I try to combine it I make it like this

from Tkinter import *

import RPi.GPIO as GPIO

import time

GPIO.setmode(GPIO.BOARD)

GPIO.setup(7,GPIO.IN)

b=False
def check_button1():

    if GPIO.input(7)== 1:

        if b == False :

            labelText1.set("on")

            print"on"

            b=True

            time.sleep(0.5)
        else:
            labelText1.set("off")

            print"off"

            b=False

            time.sleep(0.5)
    mamdouh.after(10,check_button1)
mamdouh = Tk()

labelText1 = StringVar()


x1 = Label(mamdouh,textvariable=labelText1) 

x1.config(font=('Helvetica',25,'bold'))

x1.grid(row=0,column=0)

mamdouh.title("mamdouh") 

mamdouh.geometry('1200x700')

mamdouh.after(10,check_button1)

mamdouh.mainloop()

but it didn't works it keeps blank every time I press the push button actually If it works well I will put 17 push button So how I can make this toggle push button on an Label?

  • I'm not a python or RPi.GPIO user but you want to make use of a callback with the pin functioning as an interrupt (RPi.GPIO should have that feature but I don't know the details). – goldilocks Aug 27 '16 at 13:04
2

You could use a separate loop that checks the GPIO state. Since it would block the main program from running root.mainloop() you have to create a thread that runs parallel to it and controls the label.

from Tkinter import *
from threading import Thread
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.IN)

class check_button(Thread):

    def __init__(self, labelText):
        Thread.__init__(self)
        self.labelText = labelText
        self.b = False

    def checkloop(self):
        while True:
            if GPIO.input(7) == 1:
                if self.b == False :
                    self.labelText.set("on")
                    print "on"
                    self.b = True
                else:
                    self.labelText.set("off")
                    print "off"
                    self.b = False
                while GPIO.input(7) == 1: pass

mamdouh = Tk()
labelText1 = StringVar()
x1 = Label(mamdouh,textvariable=labelText1) 
x1.config(font=('Helvetica',25,'bold'))
x1.grid(row=0,column=0)
mamdouh.title("mamdouh") 
mamdouh.geometry('1200x700')

chk1 = check_button(labelText1)
c1 = Thread(target=chk1.checkloop)
c1.start()

mamdouh.mainloop()
  • not working I will edit the question to add the code – mamdouh abdelfatah Aug 28 '16 at 12:57
  • please check it again – mamdouh abdelfatah Aug 28 '16 at 15:43
  • @mamdouhabdelfatah I updated the code, try again. – Domme Aug 28 '16 at 17:30
  • it works thanks alot , what if I want to make this for two or more push button with two or more labels ? what I will make ? – mamdouh abdelfatah Aug 28 '16 at 17:47
  • @mamdouhabdelfatah Good to hear, would be really nice if you could accept my answer as correct =) To expand the functionality to other GPIO pins you would simply have to include the GPIO pin's index into the check_button class by adding it to its init method's arguments, then save it as self.gpio, and of course include self.gpio in the if-statement. You can then spawn another instance of the check_button thread in the main program and start it the same way you did before with chk1/c1. Welcome to object oriented programming ;) – Domme Aug 28 '16 at 22:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.