Tkinter: Any way to flush events without blocking?

Noah noah at noah.org
Tue Feb 24 13:37:40 EST 2004


I have a long running application with a Tkinter GUI.
Is there a way to see how many events are pending in the queue?
I want my application to occasionally process events (so that the
output window will update; the root window will refresh if
the user moves it; etc.). 

I tried using Tk.tk.dooneevent(), but it blocks if no events are
available. What I thought of doing is calling a method that
does something like this:
        # event_queue_empty() does not exist
        while not root.tk.event_queue_empty(): 
                root.tk.dooneevent(0)

The problem is that I'm calling an imported Python method 
that runs for a long time. I can't easily retrofit it into an 
event-oriented application. What I would like to do is at least 
ensure that the root window is up to date before I start the process.
What happens now is that I update the GUI then start the process, but
the root window does not get painted before it freezes while
the imported process runs (about 20 seconds -- a long time for a 
GUI to be frozen).

        def StartButton (self):
                self.label1_text.set ("Processing document")
                self.label2_text.set ("Estimated time to complete is
%d seconds." % self.time_estimate)
                doc_generator.process (self.document) # do the long
process()

I suppose that my other option is to use a thread, but that
seems overkill when all I want is a updated window.

At this point I suspect that I'm thinking about the problem wrong.

Is there a good source for documentation of the hidden stuff in
Tkinter?
The dooneevent() method seems to be poorly documented. Most docs don't
event mention it (or the tk attribute of a Tk instance). I had to look
up TCL docs to find that dooneevent() is a blocking call.

Yours,
Noah



More information about the Python-list mailing list