python中的after() vs update() [英] after() vs update() in python

查看:27
本文介绍了python中的after() vs update()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Python 新手,开始使用 tkinter 作为画布.

i am new to python and started working with tkinter as canvas.

到目前为止,我使用 .update() 来更新我的画布.但也有一个 .after() 方法.任何人都可以向我解释这个函数(请举个例子:))并且两者之间有区别吗:

up to now i used .update() to update my canvas. but there is also a .after() method. could anyone explain me this function (with an example please :) ) and is there a difference between:

 root.after(integer,call_me)

while(True):
    time.sleep(integer)
    root.update()
    call_me

我已经搜索过了,但找不到好的解释(我的 .after 示例也不起作用:().

i have searched already and couldn't find a good explanation (and my .after examples wont work neither :( ).

推荐答案

update() 屈服于事件循环 (mainloop),允许它处理挂起的事件.

update() yields to the event loop (mainloop), allowing it to process pending events.

after,当给定一个或多个参数时,只需将一个带有时间戳的事件放在事件队列中.在给定的时间过去并且事件循环有机会处理它之前,不会处理该事件.

after, when given one or more arguments, simply places an event on the event queue with a timestamp. The event won't be processed until the given time has passed and the event loop has a chance to process it.

要知道的重要一点是,事件循环需要能够不断响应事件.事件不仅仅用于按钮点击和键盘按键,它还用于响应重绘窗口、滚动数据、在您将鼠标悬停在小部件上时更改边框和颜色等请求.

The important thing to know is that the event loop needs to be able to constantly respond to events. Events aren't just used for button clicks and keyboard keys, it is used to respond to requests to redraw the windows, scroll data, change borders and colors when you hover over widgets, etc.

当你调用 sleep() 时,程序就是这样做的——它会休眠.当它处于睡眠状态时,它无法处理任何事件.任何睡眠都会导致您的 GUI 卡顿.睡眠时间越长,口吃就越明显.

When you call sleep(), the program does exactly that - it sleeps. While it is sleeping it is unable to process any events. Any sleeping will cause your GUI to stutter. The longer the sleep, the more noticeable the stutter.

这篇关于python中的after() vs update()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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