[Tutor] Tkinter mainloop()

Alan Gauld alan.gauld at btinternet.com
Wed Jul 7 09:11:40 CEST 2010

"Francesco Loffredo" <ilcomputertrasparente at gmail.com> wrote

>> You can ask the canvas to repaint itself by calling 
>> update_idle_tasks()
>> method.
> Thank you, Alan. As many answers, this poses me a new question: why 
> should I call update_idle_tasks() instead of update() ? What's the 
> difference between the two methods?

Its a little bit subtle but I believbe update() updates all widgets
whereas update_idle_tasks will only update those widgets that
have changed since the last update. In a complex GUI this can
be notably faster. Most of the books I've seen recommend not
using update() as it can cause race conditions but I have no 
of that - because I use update_idle_tasks! :-)

>> But in general you shouldn't need to. It's usually better to keep
>> your event handlers short enough that they return to the mainloop 
>> which
>> will then redraw as necessary for you.
> In general, I do agree. But I explained rather verbosely the 
> peculiar situation I'm facing in my practice project: where would 
> you put the automatic move call, if not where I did? I need an 
> automatic move be performed at the proper moment, when it's the 
> computer player turn.

Create the auto move in an event handler of its own and associate
with an event. Then raise that event when your players move is 
Tkinter will then call the automove for you, updating the screeen
automatically. In an event driven environment control is passed
around by means of events.

How do you raise an event in Tkinter?
Use the event_generate() method.

Alternatively use the after() method with a short delay - say 10ms...


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list