简体   繁体   English

“update”和“update_idletasks”之间有什么区别?

[英]What's the difference between “update” and “update_idletasks”?

From the effbot.org documentation, we have the following about the update function: 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

On the other hand, there's this about the update_idletasks function: 另一方面,这是关于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. 这可用于在必要时执行几何管理和重绘小部件,而无需调用任何回调。

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 . 我看到的唯一区别是update 处理所有挂起事件调用事件回调 That's why we should not call update from within an even callback, I suppose. 这就是为什么我们不应该在偶数回调中调用update ,我想。

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. 但是,我已经看到了一个接一个地使用update_idletasksupdate 例子 ,我无法理解其中的原因,因为理论上update执行update_idletasks所做的一切。

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 ? 有人回答说,我应该在什么情况下使用update_idletasks update 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. after安排的事件,主要是。 And, as you also mentioned in your question, events that trigger a redraw. 而且,正如您在问题中提到的那样,触发重绘的事件。

The circumstances when you should use update over update_idletasks ? 您应该使用update_idletasks update的情况? Almost never. 几乎从不。 In all honesty, my pragmatic answer is "never call update unless calling update_idletasks doesn't do enough". 老实说,我务实的答案是“永远不要调用update除非调用update_idletasks做得不够”。

The important thing to remember is that update blocks until all events are processed. 要记住的重要一点是update阻塞,直到处理完所有事件。 In effect, that means you have a mainloop nested inside a mainloop . 实际上,这意味着你有一个mainloop嵌套在一个内部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. 大量的代码我看到来电update往往比以往任何时候都应该这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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