Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
140 views
in Technique[技术] by (71.8m points)

python - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...