I have created GUI using tkinter, which will run on the RaspberryPi and will perform various actions such as lighting LEDs. The problem I have is toggling an LED on and off using the root.after scheduling as if I use time.sleep(), the GUI will freeze while in this sleep. Here is my code, below I want to replace time.sleep() with some kind of delay roughly 500ms.
def toggleLED(root,period=0):
if (period <15) and (Status is "On"):
GPIO.output(11, True)
time.sleep(0.5) #Needs to be replaced as causing GUI to freeze
GPIO.output(11, False)
root.after(1000, lambda: toggleLED(root, period)) #schedule task every 1 second while condition is true
elif (Status == "Off"):
print("Lights have been switched off")
else:
GPIO.output(11, True)
thanks
This is one solution, but it seems very messy:
def toggleLED(root,period=0):
global Flash
while (period <30) and (Status is "On"):
if (Flash is True):
GPIO.output(11, False)
Flash = False
break
elif (Flash is False):
GPIO.output(11, True)
Flash = True
break
else:
break
if (period <30) and (Status == "On"):
period +=1
print(period)
root.after(500, lambda: toggleLED(root, period))
elif (Status == "Off"):
print("Lights have been switched off")
else:
GPIO.output(11, True)