easygui icon indicating copy to clipboard operation
easygui copied to clipboard

Can i limit the amount of time a user has to press a button

Open Gilf2121 opened this issue 5 years ago • 1 comments

Can i limit the amount of time a user has to press a button?

Gilf2121 avatar Jan 20 '20 15:01 Gilf2121

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!")

spyoungtech avatar Jan 20 '20 19:01 spyoungtech