What’s the difference between “update” and “update_idletasks”?

From the effbot.org documentation, we have the following about the update function:

Processes all pending events, calls event callbacks, completes any
pending geometry management, redraws widgets as necessary, and calls
all pending idle tasks. This method should be used with care, since it
may lead to really nasty race conditions if called from the wrong
place (from within an event callback, for example, or from a function
that can in any way be called from an event callback, etc.). When in
doubt, use update_idletasks instead.

On the other hand, there’s this about the update_idletasks function:

Calls all pending idle tasks, without processing any other events.
This can be used to carry out geometry management and redraw widgets
if necessary, without calling any callbacks.

As far as I understood, both call all pending idle tasks, complete any pending geometry management and redraw widgets as necessary. The only difference I see is that update processes all pending events and calls event callbacks. That’s why we should not call update from within an even callback, I suppose.

However, I have seen examples where update_idletasks and update are used one after the other, and I can’t understand the reason, since theoretically update does everything update_idletasks does.

What exactly are these pending events and the idle tasks the documentation is talking about? What are the differences and relations?

That being answered, in what real circumstances should I use update over update_idletasks? Concrete examples are also appreciated.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The only difference I see is that update processes all pending events
and calls event callbacks. That’s why we should not call update from
within an even callback, I suppose.

You are correct on both accounts.

What are pending events? Events scheduled with after, mostly. And, as you also mentioned in your question, events that trigger a redraw.

The circumstances when you should use update over update_idletasks? Almost never. In all honesty, my pragmatic answer is “never call update unless calling update_idletasks doesn’t do enough”.

The important thing to remember is that update blocks until all events are processed. In effect, that means you have a mainloop nested inside a mainloop. It’s never a good idea to have an infinite loop inside an infinite loop.

If you see examples where one is called after the other, you’re looking at bad examples. Honestly, there’s absolutely no reason whatsoever to do that. A lot of code I see calls update way more often than it ever should.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x