I've been working on some python scripts accessing the gpio pins on my rpi to light an led and I ran into a little problem I'm not sure how to solve.
My code looks something like this minus some setup statements
def ledblink():
while True:
GPIO.output(13, True)
time.sleep(.5)
GPIO.output(13, False)
time.sleep(.5)
def ledoff():
GPIO.output(13, False)
button = Button(root, text = 'LED ON', command = ledblink)
button.pack()
offbutton = Button(root, text = 'LED OFF', command = ledoff)
offbutton.pack()
As you can probably tell using tkinter for my gui. Also, as you can probably tell once I click the on button, that is all this program is going to let me do, the led blinks and continues because True is always True. How can I keep an indefinite loop running so that I can have a blinking led and still be able to break out of the loop when I want to turn things off? This was easy when I wanted to simply turn the led on and off, but not so easy with the loop I have to make it blink.
Just an FYI I was able to break out of the loop in my script version by using a try/except in which I used KeyboardInterrupt to call GPIO.cleanup() but I'm not sure how to do this in a gui.




