简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM