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