"update" 和 "update_idletasks" 有什么区别?

What's the difference between "update" and "update_idletasks"?

effbot.org 文档中,我们有以下关于 update 函数的信息:

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.

另一方面,关于 update_idletasks 函数是这样的:

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.

据我了解,调用所有挂起的空闲任务完成任何挂起的几何管理重绘必要的小部件。我看到的唯一区别是 update 处理所有未决事件 调用事件回调 。这就是为什么我们不应该在 even 回调中调用 update,我想。

但是,我看到其中update_idletasksupdate相继使用,我无法理解原因,因为理论上updateupdate_idletasks 做的一切。

这些未决事件和文档所讨论的空闲任务究竟是什么?有什么区别和关系?

回答了这个问题,在什么实际情况下我应该使用 update 而不是 update_idletasks?具体的例子也很受欢迎。

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.

你在这两个方面都是正确的。

什么是未决事件?主要是 after 安排的活动。而且,正如您在问题中也提到的那样,触发重绘的事件。

什么情况下应该使用update而不是update_idletasks?几乎从不。老实说,我务实的回答是 "never call update unless calling update_idletasks doesn't do enough".

要记住的重要一点是 update 阻塞直到 所有 事件被处理。实际上,这意味着您有一个 mainloop 嵌套在 mainloop 中。在无限循环中有无限循环从来都不是一个好主意。

如果您看到一个接一个被调用的示例,那么您正在查看不好的示例。老实说,绝对没有理由这样做。我看到很多代码比以往任何时候都更频繁地调用 update