2

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.

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.

Kavitha

greut
  • 4,305
  • 1
  • 30
  • 49
user1145494
  • 21
  • 1
  • 2

2 Answers2

1

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.

jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • @user1145494: you've missed `@contextmanager` line. `from Tkinter import *` has nothing to do with the error; don't use wildcard imports; only `Tk` name is used from `Tkinter` module in this case. – jfs Jan 16 '12 at 09:24
  • The previous comment I left was incomplete and hence I deleted it. In the above code, in the line "with tk(timeout=1.5)" , I got the following error: AttributeError: 'generator' object has no attribute '__exit__'. However, I found what I was looking for in http://stackoverflow.com/questions/1917198/how-to-launch-a-python-tkinter-dialog-box-that-self-destructs. ( first example code ). Thanks a lot for your time and effort. – user1145494 Jan 16 '12 at 09:42
0

If you have started to create a GUI, you should be able to use the textbox() function. A text box could be used as a place for your status messages, rather than making a separate dialog window appear.

I got the following description of textbox() here:

textbox(msg='', title=' ', text='', codebox=0)

Display some text in a proportional font with line wrapping at word breaks. This function is suitable for displaying general written text. The text parameter should be a string, or a list or tuple of lines to be displayed in the textbox.

gary
  • 4,227
  • 3
  • 31
  • 58