一些看起来应该如此简单的事情却给我带来了很大的问题。我有一个 Tkinter GUI,我试图定期更新它。具体来说,我正在从 Canvas 上删除项目并替换它们。作为这里的一个例子,虽然我只是想打印一个证明 after 函数正常工作的声明。当我放置一个按钮并单击它时,一切正常,但我想使用 after() 函数自动执行此操作。不过,我运气不太好。
class app():
def __init__(self, frame):
self.pad = tk.Canvas(frame)
self.pad.create_window(10, 10, window=tk.Button(self.pad,command=update)
self.pack(fill="both")
#More Stuff
#Neither one worked
frame.after(1000,update)
#self.pad.after(1000,update)
def update(self):
print "Updating"
#More Stuff
if __name__=="__main__":
root = tk.TK()
app(root)
root.mainLoop()
当然,这不是完整的代码,但希望它对我正在尝试做的事情有足够的意义。因此,当我单击该按钮时,我会看到“正在更新”字样出现。但是当我使用 after funciton 时,它在开始时出现一次,再也不会出现。我也在用Python 2.4.4版本,别说我没发言权哈哈。感谢您提供的所有帮助!
最佳答案
在 update
结束时调用 after
:
def __init__(self, frame):
self.frame = frame
...
self.frame.after(1000, self.update)
def update(self):
...
self.frame.after(1000, self.update)
这是路after作品。它只对回调(例如 self.update
)进行一次排队。每the docs :
The callback is only called once for each call to this method. To keep calling the callback, you need to reregister the callback inside itself
关于Python Tkinter after() 只执行一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22581432/