创建一个在Python中超时的对话框

时间:2015-03-24 16:10:52

标签: python user-interface timeout

想象一下,我有一个可能成功或可能失败的操作,我可以重试 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不支持超时。我怎样才能获得超时,理想情况下隐藏了很多这样做的复杂性?

1 个答案:

答案 0 :(得分:0)

凭借@martineau's pointer,事实证明这很容易。你只需要添加

boxRoot.after(timeout_ms, boxRoot.quit)

之前

boxRoot.mainloop()
easygui.buttonbox中的