faqts : Computers : Programming : Languages : Python : Snippets : Tkinter

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

8 of 9 people (89%) answered Yes
Recently 5 of 6 people (83%) answered Yes

Entry

Refreshing windows in Tkinter

Jul 5th, 2000 10:03
Nathan Wallace, unknown unknown, Hans Nowak, Snippet 307, Tim Evans


"""
Packages: gui.tkinter
"""
"""
> 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?
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.
"""