easygui
easygui copied to clipboard
Can i limit the amount of time a user has to press a button
Can i limit the amount of time a user has to press a button?
Per the documentation, EasyGUI is NOT event-driven, so this kind of functionality is not directly exposed by EasyGUI, but you can access some underlying Tkinter functionality to do this.
Namely, you could use the after callback for the underlying tkinter elements
Here is a complete example of a simple timed 'quiz' application.
'''Quiz with a 10 second timeout'''
import easygui as eg
import time
BOX = eg.buttonbox(msg="", title="QUIZ!", choices=['1','2','3'], run=False)
def timeout_poll(start, timeout=10):
expires = start + timeout
time_remaining = expires - time.time()
if time_remaining <= 0:
try:
BOX.ui.boxRoot.destroy()
except:
pass
eg.msgbox("TIME IS UP!")
return
message = f"What is 1 + 1? You have {timeout} seconds to choose!\n{time_remaining:0.0f} seconds remaining"
BOX.msg = message
BOX.ui.boxRoot.after(100, timeout_poll, start)
start = time.time()
BOX.ui.boxRoot.after(0, timeout_poll, start)
try:
choice = BOX.run()
except:
#if box was destroyed
choice = None
if choice == '2':
eg.msgbox(msg="Correct!", title="QUIZ")
else:
eg.msgbox(msg="Sorry, try again!", title="QUIZ!")