-1

I have a Raspberry Pi 2 and I'm trying to a create a GUI to control a LED. I found the solution for this and it's working fine so far. I'm doing this with Python and Tkinter (I'm pretty new to both).

However I've connected a hardware push button to the Raspberry but so far I'm not able to make the hardware push button and the GIU to work together. Here is what I have - a window with 2 buttons - Turn LED On and Quit (it's clear what this one is doing). When I press the the Turn LED on button, it lights the LED on breadboard and the button text is changed to Turn LED Off and when I press it again it's turning off the LED and changes the button text again. What is the role of the hardware push button - when I press it - if the LEDD is off it should turn it on and change the button text and if the LED is on - when pressed the hardware button should turn the LED off and change the button text again.

I've checked other posts and I've tried with the after method and the hardware button is working, but when I click the first button - the application freezes. Here is the code I have so far:

 #! /usr/bin/python

from tkinter import *
import RPi. GPIO as GPIO

GPIO.setwarnings(False)

GPIO.setmode(GPIO.BCM)

GPIO.setup(17, GPIO.OUT)

GPIO.output(17, GPIO.LOW)

GPIO.setup(10, GPIO.IN)


def buttonp():
        while True:
                if GPIO.input(10) == False:
                        if (GPIO.input(17) == True):
                                GPIO.output(17, GPIO.LOW)
                                ledButton["text"] = "Turn LED On"
                        else:
                                GPIO.output(17, GPIO.HIGH)
                                ledButton["text"] = "Turn LED Off"
        root.after(2000, buttonp)

def toggle():

 if GPIO.input(17):
 GPIO.output(17, GPIO.LOW)
 ledButton["text"] = "Turn LED On"
 else:
 GPIO.output(17, GPIO.HIGH)
 ledButton["text"] = "Turn LED Off"

def quit():
        GPIO.output(17, GPIO.LOW)
        exit()



root = Tk()

root.title("LED Controler")
root.minsize(width = 800, height = 600)
ledButton = Button(root, text = "Turn LED on", command = toggle)
ledButton.pack(side = LEFT)

quitButton = Button(root, text = "Quit", command = quit)
quitButton.pack(side = LEFT)

root.after(2000, buttonp)
root.mainloop()

It would be greatly appreciated if someone could help me with this.

Regards,

Ivan

Ivan
  • 31
  • 2
  • 11
  • It freezes because you have an infinite loop. Your while loop never terminates, and doesn't seem to even try to terminate. – Bryan Oakley May 12 '15 at 20:10
  • Well is there a way to check if the hardware button is pushed without using a loop? I'll still need the GUI though.... – Ivan May 12 '15 at 20:13
  • Yes. It's the same as if you were doing animation. Write a function that does the check, then use the tkinter `after` method to call that function on a regular basis. The basic pattern is described here: http://stackoverflow.com/a/11505034/7432. Whether you're drawing a frame of animation or polling a device, the pattern is the same. – Bryan Oakley May 12 '15 at 20:55

1 Answers1

0

Possibly add "root.update()" to your loop.

jjcyalater
  • 49
  • 2
  • 9