使用tkinter创建椭圆形的流体运动
作者:互联网
我正在尝试使用tkinter创建Connect-Four.一旦将光盘放置在特定的列中,我希望它以流畅的运动下降到列的底部.
我尝试过使用Canvas类的move命令,但是不确定是否使用不正确,或者最好在每次迭代时删除并重新绘制椭圆形.目前,光盘确实在移动,但动作不流畅.它只是将其绘制在新位置.
这是光盘移动功能:
counter = 0
self.__canvas.create_oval(100,200,0,100, fill='yellow')
self.__canvas.create_oval(100,300,0,200, fill='yellow')
self.__canvas.create_oval(100,400,0,300, fill='brown')
disc = self.__canvas.create_oval(200,400,100,300, fill='green') # trying to move this specific disc
while counter < 10:
self.__canvas.move(disc, 0, counter)
counter += 1
解决方法:
您必须调整移动步调,以使运动可见. canvas.after()允许您重复调用函数,在这种情况下,直到满足条件为止(磁盘到达目标位置)
工作代码段
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()
标签:python,python-3-x,tkinter,tkinter-canvas 来源: https://codeday.me/bug/20191013/1905427.html