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.
Python Tkinter Mainloop Update Update() method in mainloop in Python Tkinter is used to show the updated screen. It reflects the changes when an event occurs.
Use the ttk. Progressbar(container, orient, length, mode) to create a progressbar. Use the indeterminate mode when the program cannot accurately know the relative progress to display. Use the determinate mode if you know how to measure the progress accurately.
The after() method calls the callback function once after a delay milliseconds (ms) within Tkinter's main loop. If you don't provide the callback , the after() method behaves like the time. sleep() function. However, the after() method uses the millisecond instead of the second as the unit.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With