我正在使用 Python 创建测试脚本。当脚本继续运行时,我需要向用户显示一条消息。这是为了进行一些状态更新,例如:“保存测试结果”不应等待用户单击“确定”。本质上,我需要创建一条无需用户操作即可弹出和关闭的消息。
目前,我正在使用easygui模块来添加GUI。Easygui可以用来创建这样的消息框,但它们无法在代码中关闭,需要等待用户关闭它们,脚本才能继续运行。
提前感谢您的时间和帮助。
卡维莎
请您参考如下方法:
要在超时时强制删除使用 easygui 创建的消息框,您可以使用 .after() 方法:
from Tkinter import Tk
from contextlib import contextmanager
@contextmanager
def tk(timeout=5):
root = Tk() # default root
root.withdraw() # remove from the screen
# destroy all widgets in `timeout` seconds
func_id = root.after(int(1000*timeout), root.quit)
try:
yield root
finally: # cleanup
root.after_cancel(func_id) # cancel callback
root.destroy()
例子
import easygui
with tk(timeout=1.5):
easygui.msgbox('message') # it blocks for at most `timeout` seconds
easygui 不太适合您的用例。考虑 unittestgui.py或 Jenkins .
