我使用 tkinter 创建了 GUI,它将在 RaspberryPi 上运行并执行各种操作,例如点亮 LED。我遇到的问题是使用 root 来打开和关闭 LED。在调度之后,就像我使用 time.sleep() 一样,GUI 将在此睡眠时冻结。这是我的代码,下面我想用大约 500 毫秒的某种延迟替换 time.sleep()。
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)
谢谢
这是一种解决方案,但看起来很混乱:
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)