我有一个程序在运行时需要对模块进行输入,所以我正在实现一个简单的对话框来从用户那里获取输入以在我的 Tkinter 程序中使用。但是我也需要它在将模块作为控制台程序运行时超时,在用户不与它交互的几秒钟后通过它或超时。而不是让它坐在那里永远等到用户与之交互。超时后如何终止窗口?这是我现在所拥有的......
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