problems with tkinter updates

Dave Angel d at
Mon Jan 23 22:57:34 EST 2012

On 01/23/2012 08:09 PM, yves at wrote:
> I'm missing something about tkinter updates. How can I give tkinter a 
> chance to run?
> Here's some code:
> import time
> import tkinter
> import tkinter.scrolledtext
> tk = tkinter.Tk()
> f = tkinter.Toplevel(tk)
> st = tkinter.scrolledtext.ScrolledText(f)
> st.pack()
> def update():
>     print('updating')
>     st.see(tkinter.END)
>     tk.after(1000, update)
> input('hit enter to start')
> update()
> f = open('/etc/services')
> for line in f:
>   st.insert(tkinter.END, line + '\n')
>   print('got it')
>   #time.sleep(5)
>   input('more?')
> input('finished?')
> When I do this (input('more?'), it works as expected. If I comment 
> that line out, then the program reads the entire file, then update the 
> window right at the end, even if I put a sleep in there. What can I do 
> inside the loop to give tk a chance?
You have it backward.  The question is not what you do inside your loop 
to give tk a chance, but rather what do you do to make tk give you a 
chance.  tk doesn't "start" till you make the mainloop() method call, 
and once you call that method, it won't return till the program is exiting.

So, forget about input statements inside some loop.  Input isn't a gui 
concept, it's for console apps.  Gui apps use dialog boxes and such.  
Similarly sleep().  mainloop() will sleep, when there are no events in 
its queue.  If you want to do work, break it into manageable chunks, and 
attach each chunk to some event that tk will fire.

Beyond that, I cannot help, for I don't know tkinter.  But all gui's are 
similar at this level of detail.



More information about the Python-list mailing list