制作一个在 Python 内超时的对话框
Making a dialog box that times out in Python
假设我有一个可能成功或失败的操作,我可以重试 ad nauseam(在这种情况下,锁定某个文件)。我想反复重试该操作,但给用户一个选项,表明他想放弃。
考虑这段代码:
import easygui
def get_lock():
# ...imagine something meaningful happens here, but fails...
return None
while True:
lock = get_lock()
if lock:
break
answer = easygui.ynbox("Do you want to keep trying?")
# Would like to just assume the answer is "yes" if the user doesn't click no within 30s
if 0 == answer:
break
此代码对我来说不太适用,因为 easygui.ynbox 不支持超时。我怎样才能获得超时,理想情况下隐藏这样做的很多复杂性?
有了 @martineau's pointer,事实证明很容易得到这个。你只需要添加
boxRoot.after(timeout_ms, boxRoot.quit)
就在
之前
boxRoot.mainloop()
在 easygui.buttonbox
.
假设我有一个可能成功或失败的操作,我可以重试 ad nauseam(在这种情况下,锁定某个文件)。我想反复重试该操作,但给用户一个选项,表明他想放弃。
考虑这段代码:
import easygui
def get_lock():
# ...imagine something meaningful happens here, but fails...
return None
while True:
lock = get_lock()
if lock:
break
answer = easygui.ynbox("Do you want to keep trying?")
# Would like to just assume the answer is "yes" if the user doesn't click no within 30s
if 0 == answer:
break
此代码对我来说不太适用,因为 easygui.ynbox 不支持超时。我怎样才能获得超时,理想情况下隐藏这样做的很多复杂性?
有了 @martineau's pointer,事实证明很容易得到这个。你只需要添加
boxRoot.after(timeout_ms, boxRoot.quit)
就在
之前boxRoot.mainloop()
在 easygui.buttonbox
.