Python- Displaying a message box that can be closed in the code (no user intervention)

后端 未结 2 1413
太阳男子
太阳男子 2021-01-22 07:54

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

2条回答
  •  佛祖请我去吃肉
    2021-01-22 08:26

    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()
    

    Example

    import easygui
    
    with tk(timeout=1.5):
        easygui.msgbox('message') # it blocks for at most `timeout` seconds
    

    easygui is not very suitable for your use case. Consider unittestgui.py or Jenkins.

提交回复
热议问题