I have a program that requires input to a module while it is running, so I am implementing a simple Dialog box to get input from the user to use in my Tkinter program. But I also need it to timeout when running the module as a console program, to pass over it or timeout after so many seconds when the user is not interacting with it. And not to have it just sit there and wait forever until the user interacts with it. How do I terminate the window after a timeout? Here is what I have now ...
def loginTimeout(timeout=300):
root = tkinter.Tk()
root.withdraw()
start_time = time.time()
input1 = simpledialog.askinteger('Sample','Enter a Number')
while True:
if msvcrt.kbhit():
chr = msvcrt.getche()
if ord(chr) == 13: # enter_key
break
elif ord(chr) >= 32: #space_char
input1 += chr
if len(input1) == 0 and (time.time() - start_time) > timeout:
break
print('') # needed to move to next line
if len(input1) > 0:
return input1
else:
return input1
Not entirely sure what the question is, but you could use the tkinter
after()
method in a similar way to this:So, if the User enters something they can hit confirm or launch an event binded to the entry, or after the delay the program runs get_entry anyway. Maybe this will give you an idea, you can also check out the other widget methods: https://effbot.org/tkinterbook/widget.htm
EDIT: I'm not sure how this is arranged in your program, but
root.mainloop()
blocks, so once it's running, code after it won't run until the mainloop() exits. If you're function is part of a toplevel() window, you could usewait_window()
to prevent the function from advancing until your timeout destroys the toplevel window or the function is called before by the User. The example, although probably not the best solution, would be: