Why i use .after() to delay call a function in a loop not working properly.
The first loop in .after() delayed call function show_letter() . But after the first loop, it just call the function without any delay.
为什么我使用.After()来延迟调用循环中不能正常工作的函数。After()中的第一个循环延迟了调用函数show_Letter()。但在第一次循环之后,它只会毫无延迟地调用该函数。
from tkinter import *
import random
import string
window = Tk()
# window.attributes('-fullscreen', True)
#
def show_letter():
randomletter = random.choice(string.ascii_letters)
print(randomletter)
label.config(text=randomletter)
def delay_show():
for i in range(200):
window.after(2000, show_letter)
label = Label(window,
text='',
font=('arial', 40),
)
label.place(relx=0.5, rely=.4, )
delay_show()
window.mainloop()
更多回答
We need to see a minimal reproducible example that demonstrates the problem.
我们需要看到一个最小的可重现的例子来说明这个问题。
window.after(i, show_letter)
should do what you like.
Window.After(i,Show_Letter)应该可以执行您喜欢的操作。
@Thingamabobs: window.after(i, show_letter)
the each letter for only a few milliseconds. I don't think that's what the OP wants. For example, the first will show for 1ms, the second for 2ms, etc. It will be imperceptible.
@thingamabobs:window.After(i,show_etter),每个字母只有几毫秒。我不认为那是行动想要的。例如,第一个将显示1毫秒,第二个显示2毫秒,依此类推。它将是不可察觉的。
@BryanOakley True, I must admit I didn't got the idea of this script.
@BryanOakley True,我必须承认我不知道这个剧本的想法。
优秀答案推荐
Consider this code:
请考虑以下代码:
def delay_show():
for i in range(200):
window.after(2000, show_letter)
What this does is schedule the first call to show_letter
in 2 seconds. Then it also schedules the second call in two seconds. Not two seconds after the first call but two seconds after the start of the loop. Then the third call is also two seconds after the start of the loop.
这样做的目的是将第一个调用调度为2秒后的show_letter。然后,它还将第二次呼叫安排在两秒内。不是在第一次调用后两秒,而是在循环开始后两秒。那么第三次调用也是在循环开始后两秒。
In other words, all of the calls are all scheduled to start at the same time, in 2 seconds.
换句话说,所有呼叫都被安排在同一时间开始,即2秒后。
The fix is simple: multiply 2000 times the loop increment so that the first call is in 2 seconds, the next in 4, the next in 6, and so on.
修复方法很简单:将循环增量乘以2000,这样第一次调用在2秒内完成,下一次在4秒内完成,下一次在6秒内完成,依此类推。
def delay_show():
for i in range(200):
window.after(2000*i, show_letter)
更多回答
It works. Thank you so much. Now i understand . I also went googled more information about the after() function. This is how it counts the time. I post it here in case anyone also encounter this problem.The Tkinter after() is one of the methods for the Tkinter package, and this method is used to calculate the time intervals for the functions in the application parallel. It calculates the time using milliseconds format whether the widget seems delayed since the Tkinter is a GUI-based library. If we have no particular values of the explicit statement, it goes to the default state behavior.
有时灵太感谢了现在我明白了我还在谷歌上搜索了更多关于after()函数的信息。这就是它计算时间的方式。Tkinter after()是Tkinter包的方法之一,这个方法用来计算应用程序中函数的时间间隔。由于Tkinter是一个基于GUI的库,它使用毫秒格式计算小部件是否延迟的时间。如果我们没有显式语句的特定值,它将进入默认状态行为。
@bingru: "this method is used to calculate the time intervals for the functions in the application parallel. " - that is incorrect. after
doesn't run code in parallel.
@Bingru:“此方法用于计算应用程序并行中函数的时间间隔。”-这是不正确的。After不并行运行代码。
我是一名优秀的程序员,十分优秀!