[Tutor] Tkinter idiosyncracies?

Alan Gauld alan.gauld at btinternet.com
Sat Jun 20 00:13:51 CEST 2009


"Wayne" <srilyk at gmail.com> wrote 

> Am I missing something essential here? 
> Or is this a bug that nobody has encountered yet?

Its not a bug its the way GUIs and event driven programs work.

You need to buuld the time delay as an event so the after() 
approach is nearly right. But you need the first event to 
make it say Foo then the after event to change it to Bar.
You can use a single event handler with a parameter 
then use a lambda to pass the value in. Something like:

import Tkinter as tk

def funky(value=None):
  if value:
     lbl.config(text=value)
  else: 
     lbl['text'] = 'Foo'
     lbl.after(3000, lambda : funky('Bar') )
  
root = tk.Tk()
lbl = tk.Label(text = "Click the button to see weirdness")
btn = tk.Button(text='Click me', command=funky)
lbl.pack()
btn.pack()
root.mainloop()


Otherwise the GUI doesn;t get redrawn until after the 
handler exits which means both updates get done at 
the same time.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list