Tkinter: update_idletasks

Russell E. Owen no at spam.invalid
Thu May 20 14:24:27 EDT 2004


In article <mailman.110.1085067858.6949.python-list at python.org>,
 Jeffrey Barish <jeffbarish at starband.net> wrote:

>I'm confused about how to use the update_idletasks method.  In my
>program, I have a handler for a button in which execution will linger. 
>During that time, I would like for the GUI to continue to show signs of
>life.  I have a Pmw MessageBar in which I display a status message.  I
>figured out that if I run update_idletasks on that MessageBar, then the
>MessageBar will update the display as I update the message.  However,
>if I cover the GUI with some other window and then expose it again, the
>GUI does not refresh until the handler finishes (except for the
>MessageBar).  Do I have to run the update_idletasks method for every
>widget in the GUI?  for all the frames? for just the root frame?  Or is
>it impossible to get the GUI to refresh in this situation?

To make your GUI responsive, call update_idletasks occasionally from the 
task that is taking a long time. Like most GUI systems, Tkinter is 
basically a single threaded system. It'll run the current task until 
finished, then process the next event. update_idletasks gives it a 
chance to handle other events.

There are other ways to handle this sort of thing. Typically a 
long-running task should be run as a separate background thread (or even 
a separate process). The difficulty is that background threads cannot 
safely interact with GUI elements, so how does the thread communicate?

The most straightforward technique is to transfer data from the 
background thread to the main thread via a Queue object (as usual), then 
poll the background thread's Queue (by repeatedly calling after to do a 
nonblocking read on the Queue object).

However, a clever trick posted fairly recently is to have the background 
thread generate an event. Apparently that is safe. Then have a handler 
listen for that event. The handler will run in the main thread, and so 
can safely update the GUI.

-- Russell



More information about the Python-list mailing list