“更新"和“更新"有什么区别?和“update_idletasks"? [英] What's the difference between "update" and "update_idletasks"?

查看:89
本文介绍了“更新"和“更新"有什么区别?和“update_idletasks"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

effbot.org 文档中,我们有关于 update 函数:

From the effbot.org documentation, we have the following about the update function:

处理所有挂起的事件,调用事件回调,完成任何待定几何管理,根据需要重绘小部件,并调用所有挂起的空闲任务.此方法应谨慎使用,因为它如果从错误的调用中调用,可能会导致非常糟糕的竞争条件地方(例如,从事件回调中,或从函数可以以任何方式从事件回调等中调用).当在有疑问,请改用 update_idletasks.

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:

调用所有挂起的空闲任务,不处理任何其他事件.这可用于执行几何管理和重绘小部件如有必要,不调用任何回调.

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 处理所有未决事件调用事件回调.这就是为什么我们不应该在偶数回调中调用 update,我想.

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.

但是,我看到了示例,其中 update_idletasksupdate 一个接一个地使用,我无法理解原因,因为理论上 update 做了 update_idletasks 所做的一切.

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?

得到回答,在什么实际情况下我应该使用 update 而不是 update_idletasks?具体的例子也值得赞赏.

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.

什么是待处理事件?主要是 after 安排的事件.而且,正如您在问题中也提到的,触发重绘的事件.

What are pending events? Events scheduled with after, mostly. And, as you also mentioned in your question, events that trigger a redraw.

在什么情况下应该使用 update 而不是 update_idletasks?几乎从不.老实说,我务实的回答是永远不要调用 update,除非调用 update_idletasks 还不够".

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

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

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.

如果您看到一个接一个调用的示例,那么您就是在查看不好的示例.老实说,完全没有理由这样做.我看到很多代码调用 update 的频率比以往任何时候都高.

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_idletasks"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆