Python-显示一个可以在代码中关闭的消息框(无需用户干预) [英] Python- Displaying a message box that can be closed in the code (no user intervention)
问题描述
我正在使用Python创建测试脚本.在脚本继续运行的同时,我需要向用户显示一条消息.这将具有一些状态更新,例如:保存测试结果",它不应等待用户单击确定".从本质上讲,我需要创建一条无需用户执行即可弹出和关闭的消息.
I am creating test scripts using Python. I need to have a message displayed to the user while the script continues to run. This is to have some status update , for eg: "Saving test results" which should not wait for the user to click "Ok". Essentially , I need to create a message that pops up and closes without the user having to do it.
当前,我正在使用easygui模块添加GUI.Easygui可用于创建此类消息框,但是无法在代码中将其关闭,需要等待用户关闭它们才能继续运行脚本.
Currently,I am using easygui module for adding GUI.Easygui can be used for creating such message boxes but they cannot be closed in the code and need to wait for the user to close them for the script to continue running.
提前感谢您的时间和帮助.
Thanks in advance for your time and help.
卡维莎
推荐答案
要在超时时强行删除使用easygui创建的消息框,可以使用.after()
方法:
To forcibly remove on timeout a message box created with easygui you could use .after()
method:
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 或詹金斯.
easygui
is not very suitable for your use case. Consider
unittestgui.py or Jenkins.
这篇关于Python-显示一个可以在代码中关闭的消息框(无需用户干预)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!