.after()方法在tkinter中只被调用一次



我试图使用Tkinter root.after()方法进行无限循环。理想情况下,循环函数应该每秒调用一次。但是after的第二个参数中的函数只被调用一次。我是否遗漏了一些关于"之后"或"之后"的错误方法?

import tkinter as tk
root=tk.Tk()
def loop():
print("hello World")
root.after(1000, loop)
root.mainloop()

这段代码只打印"hello World"每秒一次而不是期望的调用

import tkinter as tk
root=tk.Tk()

def loop():
print("hello World")
# once 'loop()' is called from root, it will get called again here
root.after(1000, loop)

root.after(1000, loop)  # initial call to start 'loop()'
# EDIT to clarify that you could just call 'loop' to start things up; no need for 'after'
# loop()
root.mainloop()

最新更新