Creating fluid movement of an oval using tkinter

时光毁灭记忆、已成空白 提交于 2019-12-02 03:36:33

You must pace the calls to move so that the movement is visible; canvas.after() allows you to call a function repeatedly, in this case until a condition is met (the disk arrived at destination)

working code snippet

import tkinter as tk


def smooth_motion(counter):
     canvas.move(disc, 0, dy)
     counter -= 1
     if counter >= 0:
         canvas.after(10, smooth_motion, counter)

root = tk.Tk()
canvas = tk.Canvas(root, bg='cyan')
canvas.pack()

counter = 100
disc = canvas.create_oval(200, 0, 210, 10, fill='green')
dy = (100 - 0) / counter
smooth_motion(counter)

root.mainloop()

You're missing function which shows changes to canvas - canvas.update(), try writing it after canvas.move().

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!