python - Tkinter 函数“root.after()”不运行指定的函数
问题描述
当我使用 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) 每隔 'time' ms 运行一次函数 'func'。但在我的代码中似乎没有这样做。它只运行一次。
解决方案
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.
推荐阅读
- java - 登录成功时,Spring Oauth 和安全重定向到 /login
- python - 如何编码流量套接字?
- java - Big-O 复杂度 -> 查找数组元素总和是否为目标
- javascript - 输入类型:提交在 javascript 中不起作用?
- julia - 按另一个数组的顺序对数组进行排序
- java - 如何检查 Web App 使用了哪些服务配额
- javascript - Javascript - 使用括号属性检索对象属性并获得未定义
- angularjs - AngularJS ngtable中的过滤不起作用
- sql - 返回具有替换值和游标的值列表
- powershell - Powershell Bash/Zsh 命令中的多个参数