![](/img/trans.png)
[英]root.after is not recursing to the same function - Tkinter
[英]Tkinter function “root.after()” doesn't run the function specified
当我使用Tkinter函数“ root.after()”时,它仅运行一次指定的函数。 在我的情况下,这是“移动”功能,位于代码底部的第二行。 总的来说,我试图将基本动画制作成椭圆形。
我正在运行Python 3.7.1。
from tkinter import *
class shape:
def __init__(self, canvas):
self.canvas = canvas
self.EdgeThickness = 1
self.color="#ffffff"
def animation(self,xposgiven):
self.shape = self.canvas.create_oval(xposgiven,250,250,400,fill=self.color,width=self.EdgeThickness)
print('runninganimation')
root=Tk()
c=Canvas(root, width=1000, height=500)
c.pack()
c.configure(background="#000000")
s=shape(c)
s.xpos=5
def move():
print('runningmove')
s.xpos+=5
s.animation(s.xpos)
root.after(100,move)
root.mainloop()
我希望函数move()每100毫秒运行一次,但它只能运行一次。 我认为函数root.after(time,func)每隔“时间”毫秒运行一次函数“ func”。 但是在我的代码中似乎没有这样做。 它只运行一次。
after
安排工作正好运行一次。 如果你希望它运行每100ms,常见的策略是让你的函数调用after
以自己为返回前一个参数。
> import tkinter
> help(tkinter.Tk.after)
after(self, ms, func=None, *args)
Call function once after given time.
MS specifies the time in milliseconds. FUNC gives the
function which shall be called. Additional parameters
are given as parameters to the function call. Return
identifier to cancel scheduling with after_cancel.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.