我什么时候应该在 tkInter 中使用 root.update() for python [英] When should I use root.update() in tkInter for python

查看:225
本文介绍了我什么时候应该在 tkInter 中使用 root.update() for python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 tkinter 的新手.有时当我从github读取代码时,它包含像root.update()这样的代码.

I am new to tkinter. Sometimes when I read the code from github, it contains code like root.update().

当我为 tkinter 搜索文档时,我通常会找到每个小部件的方法列表,但不会找到 Tk() 根对象本身的方法列表.此外,通常当我为每个小部件设置配置时,它会自动更新视图.所以我对什么时候应该使用 root.update() 有点困惑?

When I search for documents for tkinter, I usually find the methods list for each widget, but not for the Tk() root object itself. Also, usually when I set config for each widget, it automatically updates the view. So I am a bit confused by when should I be using root.update()?

推荐答案

答案应该是从不.即使您的代码与 .update() 一起工作,也不会有任何保证.快速回答是,你永远不会知道此刻你在用这个命令做什么.改用 .update_idletasks().

The answer should be never. Even if your code works with .update() there will be no garentee that it will ever do so. The quick answer is, you will never know what you are doing with this command at the very moment. Use .update_idletasks() instead.

出自文档:

更新服务所有未完成的事件,包括那些到期的事件在更新运行时.提供它是为了允许诸如刷新 GUI 立即发生.协程提供了另一种方式构建代码以便事件可以在战略点得到服务在控制流中.甚至在协程可用之前,许多 Tcl程序员发现,如果出现需要使用update,就是一个线索应该重新构造脚本.

update services all outstanding events, including those that come due while update is operating. It was provided to allow tasks such as refreshing a GUI to happen immediately. coroutine provides another way to structure code so that events can be serviced at strategic points in the control flow. Even before coroutine was available, many Tcl programmers found that if the need to use update arises, it's a clue that the script should be restructured.

update 通过在循环中执行以下步骤来工作,直到没有事件在一次迭代中得到服务:

update works by performing the following steps in a loop until no events are serviced in one iteration:

为预定时间已到的第一个事件提供服务.如果没有这样的找到事件,服务当前在空闲队列中的所有事件,但是不是这一步开始后添加的那些.

Service the first event whose scheduled time has come. If no such events are found, service all events currently in the idle queue, but not those added once this step starts.

update idletasks 跳过第一步,只处理空闲队列.

update idletasks skips the first step, processing only events in the idle queue.

为什么有害?

问题在于更新几乎具有不受约束的副作用.调用 update 的代码不知道任何随机事件处理程序可能在更新过程中对其数据进行了处理.这个事实是潜在错误的来源.

The issue is that update has virtually unconstrained side effects. Code that calls update doesn't know what any random event handler might have done to its data while the update was in progress. This fact is the source of insidious bugs.

这篇关于我什么时候应该在 tkInter 中使用 root.update() for python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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