I want to toggle a real pushbutton and display it on tkinter GUI
<p>I want to toggle a pushbutton and show its changes on a label using tkinter.</p>

<p>If I press the button it shows "on" on the label and when I press again it shows "off" on the label</p>

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

<p>I have a problem in combining this code</p>



<pre class="lang-py prettyprint-override"><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)
</code></pre>

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

<p>Here is the tkinter code</p>

<pre class="lang-py prettyprint-override"><code>import tkinter.*

root = tk()
x = StringVar()

s=Label(root,textvariable=x)

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

root.mainloop()
</code></pre>

<p>When I try to combine it I make it like this</p>

<pre class="lang-py prettyprint-override"><code>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()
</code></pre>

<p>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</p>

<p>I think that the problem is in placing this if statment on the right place and placing the b variable in it's right place and I think also there is a problem between this if statment and tkinter because I tried this code wich works perfect but it is not toggling the push button so I want to change this lets add this code here also :</p>

<pre class="lang-py prettyprint-override"><code>from Tkinter import *
import RPi.GPIO as GPIO

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

def check_button1():
    if(GPIO.input(7) == GPIO.LOW):
        labelText1.set("on")
    else:
        labelText1.set("off")
    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()
</code></pre>

<p>So how I can make this toggle push button on an Label?</p>

<p>Your problem is recognizing button down and button up events.  Your OS mouse driver does this for your mouse buttons. If your GPIO module does not do this for you, you will have to detect these events by comparing the current state to the previous state.  (I am ignoring here any possible need to 'de-bounce' the button.)  You are sort of trying to do this with the time.sleep(.5) calls, but do not use time.sleep in gui code.</p>

<p>Your driver should be self-contained and independent of any tk widgets other than the root needed for .after.  For multiple buttons, you will need your own GPIOButton class.  Your code that works is a starting point.  Tkinter allows you to tie a command to button-up events.  Your class init should similarly take up and or down event commands (callbacks).</p>

<p>Here is something untested that might get you started.</p>

<pre><code>class GPIOButton:
    def __init__(self, master, buttons, command_down=None, command_up=None):
        self.master = master
        self.buttons = buttons
        self.command_down = command_down
        self.command_up = command_up
        GPIO.setmode(GPIO.BOARD)
        for button in buttons:
            GPIO.setup(button, GPIO.IN)
        self.state = [GPIO.HIGH] * len(buttons)  # best initial value?
        self.check_buttons()  # or call this elsewhere
    def check_buttons(self):
        for i, button in enumerate(self.buttons):
            oldstate = self.state[i]
            newstate = GPIO.input(button)
            if oldstate != newstate:
                self.state[i] = newstate
                command = (self.command_down if newstate==GPIO.LOW
                           else self.command_up)
                command(button)
        self.master.after(10, self.check_button)
</code></pre>

python python-2.7 tkinter raspberry-pi raspberry-pi2