python - Tkinter Canvas 不在无限循环中更新

标签 python animation canvas tkinter

我创建了一些代码来显示 2 个球在移动,但是当我运行它时,它没有显示球的移动。此外,它停止运行并忽略了无限循环这是我到目前为止的代码:

import tkinter as tk

class ObjectHolder:
    def __init__(self, pos, velocity, radius, id):
        self.id = id                # the id of the canvas shape
        self.pos = pos              # the position of the object
        self.r = radius             # the radius of incluence of the object
        self.velocity = velocity    # the velocity of the object

    def moveobject(object):
        x = object.pos[0] + object.velocity[0]  # moves the object where
        y = object.pos[1] + object.velocity[1]  # 0=x and 1=y
        object.pos = (x, y)
        canvas.move(object, x, y)

class App():
    def __init__(self, canvas):
        self.canvas = canvas
        self.objects = []
        for i in range(0, 2):
            position = ((i+1)*100, (i+1)*100)
            velocity = (-(i+1)*10, -(i+1)*10)
            radius = (i + 1) * 20

            x1 = position[0]-radius
            y1 = position[1]-radius
            x2 = position[0]+radius
            y2 = position[1]+radius

            id = canvas.create_oval(x1, y1, x2, y2)
            self.objects.append(ObjectHolder(position, velocity, radius, id))
        self.symulation(self.objects)

    def symulation(self, objects):
        for object in objects:             # this moves each object
            ObjectHolder.moveobject(object)

        # This part doesn't work. It is supposed to update the canvas
        # and repeat forever.
        self.canvas.update()
        root.update()
        self.canvas.after(50, self.symulation, objects)

root = tk.Tk()
canvas = tk.Canvas(root, width=800, height=600, bg="light blue")
canvas.pack()
App(canvas)

最佳答案

您的代码存在许多问题。一大问题是您更新现有 Canvas 对象位置的方式。 move() 方法需要知道移动量(x 和 y 值的变化),而不是新的绝对位置。

当我修复后发现速度太大,所以我将它们降低到只有您所拥有的值的 10%。

另一个问题是 ObjectHolder 类的实现方式。一方面,moveobject() 方法没有 self 参数,它应该使用它而不是有一个 object 参数。您可能还应该将方法重命名为 move()

下面的代码运行并为运动设置动画。

import tkinter as tk


class ObjectHolder:
    def __init__(self, pos, velocity, radius, id):
        self.id = id                # the id of the canvas shape
        self.pos = pos              # the position of the object
        self.r = radius             # the radius of incluence of the object
        self.velocity = velocity    # the velocity of the object

    def moveobject(self):
        x, y = self.pos
        dx, dy = self.velocity
        self.pos = (x + dx, y + dy)
        canvas.move(self.id, dx, dy)  # Amount of movement, not new position.


class App():
    def __init__(self, canvas):
        self.canvas = canvas
        self.objects = []
        for i in range(0, 2):
            position = ((i+1)*100, (i+1)*100)
#            velocity = (-(i+1)*10, -(i+1)*10)
            velocity = (-(i+1), -(i+1))  # Much slower speed...
            radius = (i + 1) * 20

            x1 = position[0]-radius
            y1 = position[1]-radius
            x2 = position[0]+radius
            y2 = position[1]+radius

            id = canvas.create_oval(x1, y1, x2, y2)
            self.objects.append(ObjectHolder(position, velocity, radius, id))

        self.simulation(self.objects)

    def simulation(self, objects):
        for object in objects:             # this moves each object
            object.moveobject()

        # This part doesn't work. It is supposed to update the canvas
        # and repeat forever.
#        self.canvas.update()  # Not needed.
#        root.update()  # Not needed.
        self.canvas.after(50, self.simulation, objects)

root = tk.Tk()
canvas = tk.Canvas(root, width=800, height=600, bg="light blue")
canvas.pack()
app = App(canvas)
root.mainloop()  # Added.

关于python - Tkinter Canvas 不在无限循环中更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54844710/

相关文章:

javascript - Typeit.js 从数组中输入随机字符串

html - 如何使用 animate.css shake with less shakes

javascript - Canvas 上的自定义字体仅适用于 safari

python - 如何将 2d libreoffice calc 命名范围分配给 python 变量。可以在 Libreoffice Basic 中完成

python - 在 numpy 数组中找到仅符号不同的行对

python - 我如何知道 Pygame 中的圆和矩形是否被触摸?

javascript - HTML5 Canvas 能够在绘制图像后放大/缩小吗?

python - 使用 mpi4py 在脚本中调用子进程

c++ - 简单的动画导致c++中的屏幕闪烁

javascript - Canvas 配色范围