Tkinter & refreshing windows

tre17 at student.canterbury.ac.nz tre17 at student.canterbury.ac.nz
Sun Sep 12 19:08:55 EDT 1999


"Hans Nowak" <hnowak at cuci.nl> writes:

> Hello,
> 
> When working with Tkinter I noticed that on certain occasions, Tk 
> doesn't refresh the screen until it's done with a certain task. Hmm, 
> that doesn't sound very clear. :)  An example: I'm currently working 
> on a program that accepts user input, then filters a lot of data 
> based on this input, then displays the results. Since the filtering 
> took a few seconds, I thought it would be a good idea to display the 
> current search results in a listbox while filtering, i.e. for every 
> "record" that passed the test, a string is added to the listbox. You 
> would be able to see some results while the filtering was still going 
> on.
> 
> However, that doesn't seem to work at all. The program starts 
> executing the filtering, and doesn't display anything until 
> everything is done. In fact, trying to change the text of a label, 
> directly before the filtering starts, doesn't work either. 
> 
> I don't know if this is Windows-related (I'm using Win95 :( ), but 
> this is a well-known problem in Delphi, and there you have the 
> Refresh() method. Is there a similar method for Tkinter, to force an 
> update of the screen display?
> 
> TIA,
> 
> --Hans Nowak (zephyrfalcon at hvision.nl)
> Homepage: http://fly.to/zephyrfalcon
> Python Snippets: http://tor.dhs.org/~zephyrfalcon/snippets/
> The Purple Kookaburra Forum: http://www.delphi.com/kookaburra/

The `update' command is what you're looking for.  For an example,
consider the following code:

=============================
#!/usr/bin/python

from Tkinter import *
import time

def loop():
    for x in range(5):
        print x
        l.insert('end', 'Foo %d' % x)
        root.update()
        time.sleep(1)

root = Tk()

l = Listbox(root)
l.pack(fill='both', expand=1)

b = Button(root, text='Start', command=loop)
b.pack(fill='x', expand=1)

root.mainloop()
=============================

The alternative if you want your interface to remain fully responsize
rather then just updating every now and then, is to use
multi-threading.  I seem to remember seeing something about Tkinter
being thread safe now, but I'vbe never used threads and Tkinter.

--
Tim Evans
tre17 at student.canterbury.ac.nz




More information about the Python-list mailing list