“Tkinter函数”根.after()”未运行指定的函数

2024-09-29 23:14:59 发布

您现在位置:Python中文网/ 问答频道 /正文

当我使用Tkinter函数时“根.after()“它只运行一次指定的函数。在我的例子中,这是“move”函数,在代码底部的第二行。总的来说,我只是想做一个椭圆形的基本动画。你知道吗

我正在运行python3.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毫秒运行一次,但它只运行一次。我认为功能根.after(time,func),每隔'time'毫秒运行函数'func'。但在我的代码中似乎没有这样做。它只运行一次。你知道吗


Tags: 函数代码selfmovedefrootwidthcolor
1条回答
网友
1楼 · 发布于 2024-09-29 23:14:59

after计划作业只运行一次。如果您想让它每100毫秒运行一次,一个常见的策略是在返回函数调用之前将函数调用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.

相关问题 更多 >

    热门问题