我正在使用 after() 进行 Tkinter 动画循环:
from tkinter import Tk, Canvas
from time import time
root = Tk()
root.configure(width=1920, height=1080)
root.resizable(True, True)
main = Canvas(root, width=1600, height=900)
ball = main.create_oval(80, 80, 120, 120, fill="#FF0000")
main.pack()
frametarget = 17
lasttime = time()
frametimes = []
def move():
global lasttime
frametimes.append(time() - lasttime)
lasttime = time()
main.move(ball, 1, 1)
root.after(frametarget, move)
root.after(0, move)
root.mainloop()
print(sum(frametimes[60:]) / len(frametimes[60:]) * 1000)
Run Code Online (Sandbox Code Playgroud)
一般情况下,更新时间约为 30 毫秒,在无人看管的情况下,视觉上看起来很慢,但当鼠标在窗口上连续移动时,它会持续达到 17 毫秒,运行平稳,但一旦停止就会恢复回来。
我使用的是 Python 3.7、Windows 10
测试:
平均帧时间,其中frametarget = 17(约60fps)
Without mouse movement = 29.28ms
With mouse movement = 17.08ms
Run Code Online (Sandbox Code Playgroud)
平均帧时间,其中frametarget = 50(20fps)
Without mouse movement = 62.50ms
With mouse movement = 50.04ms
Run Code Online (Sandbox Code Playgroud)
更新: Linux 上不存在同样的问题。
| 归档时间: |
|
| 查看次数: |
265 次 |
| 最近记录: |