使用 tkinter 创建椭圆的流体运动 [英] Creating fluid movement of an oval using tkinter
问题描述
我正在尝试使用 tkinter 创建 Connect-Four.将圆盘放入某个柱子后,我希望它以流畅的运动下降到柱子的底部.
I'm trying to create Connect-Four using tkinter. Once a disc is placed in a certain column, I want it to descend to the bottom of the column in a fluid movement.
我已经尝试使用 Canvas 类的移动命令,但我不确定我是否使用错误,或者我最好在每次迭代中删除并重新绘制椭圆.目前,光盘确实在移动,但不是以流畅的方式移动.它只是在新位置绘制它.
I've tried using the move command of the Canvas class but I'm unsure if I am using it incorrectly or perhaps I am better off deleting and re-drawing the oval each iteration. Currently, the disc indeed moves but not in a fluid way. It simply draws it in the new location.
这是圆盘移动功能:
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()
允许你重复调用一个函数,在这种情况下,直到满足条件(磁盘到达目的地)
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)
工作代码片段
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()
这篇关于使用 tkinter 创建椭圆的流体运动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!