【发布时间】:2019-04-26 20:52:35
【问题描述】:
所以我正在尝试编写一个程序,其中用户在 Tkinter GUI 中的屏幕上,如果没有按下物理按钮,则希望系统从头开始。
def start():
#creating instance
window = Tk()
def btnclicked(): #ignore this name as its for another button
def endgame():
lbltitleend = Label(window, text="Press the button within 5 seconds to quit or the game will restart.", font=("Arial bold", 15), fg="white", bg="black", pady= 15)
lbltitleend.pack()
for objectx in objects:
objectx.destroy()
def buttonpress():
while True:
time.sleep(5)
if (GPIO.input(22) == True):
window.destroy()
print("Thanks for playing!")
del objects[:]
else:
start()
window.after(500, buttonpress) # Need this so that the program doesnt stall waiting for button to be pressed, and executes everything before the button can be pressed
所以这只是最终运行,所以在 time.sleep 5 秒后它最终只是再次启动程序并打开一个新的 gui 窗口,我不介意,但这不是我想要的。
我想要的是让程序等待按钮被按下,如果在 5 秒内没有按下,那么我希望它重新启动。
有没有办法做到这一点?
【问题讨论】:
标签: python-3.x tkinter