为什么我的简单 tkinter 动画滞后?

Why is my simple tkinter animation lagging?

我正在试验 tkinter 模块,想生成 2 个女巫。所以我在 python 中查看了一些关于线程的文档并实现了它。对于这样一个简单的程序来说,它的运行速度如此之慢,我感到很惊讶。

有什么建议吗?我做错了什么?我错过了什么重要的事情吗?

import tkinter
from threading import Thread
from random import randint
canvas = tkinter.Canvas(width=1000,height=500)
canvas.pack()

class Witch:
    def __init__(self, x=0, y=0):
        self.x = x
        self.y = y
        self.tag = "witch-%d" % id(self)
        self.tags = ("witch", self.tag)
        self.draw_witch(self.x, self.y)

    def draw_witch(self, x, y):
        canvas.create_oval(x,y-20,x+20,y, tags=self.tags)
        canvas.create_rectangle(x,y,x+20,y+40, tags=self.tags)
        canvas.create_line(x+40,y+20,x-20,y+20, tags=self.tags)
        for i in range(-4,4):
            canvas.create_line(x-40,y+20+i*5,x-20,y+20, tags=self.tags)
    
    def move(self, speed):
        canvas.move(self.tag, speed, speed)


def landing(x, y):
    entity = Witch(x, y)
    speed = randint(1,10)
    while canvas.coords(entity.tag)[2]+40 < 1000 and canvas.coords(entity.tag)[3]+40 < 500:
        entity.move(speed)
        canvas.after(10)
        canvas.update()

for _ in range(2):
    t = Thread(target=landing, args=(randint(50, 400), randint(50, 200)))
    t.start()

canvas.mainloop()

如果没有进程需要它,请不要使用线程。 after 方法工作得很好。

import tkinter
from random import randint


canvas = tkinter.Canvas(width=1000,height=500)
canvas.pack()

class Witch:
    def __init__(self, x=0, y=0):
        self.x = x
        self.y = y
        self.tag = "witch-%d" % id(self)
        self.tags = ("witch", self.tag)
        self.draw_witch(self.x, self.y)

    def draw_witch(self, x, y):
        canvas.create_oval(x,y-20,x+20,y, tags=self.tags)
        canvas.create_rectangle(x,y,x+20,y+40, tags=self.tags)
        canvas.create_line(x+40,y+20,x-20,y+20, tags=self.tags)
        for i in range(-4,4):
            canvas.create_line(x-40,y+20+i*5,x-20,y+20, tags=self.tags)
    
    def move(self, speed):
        canvas.move(self.tag, speed, speed)


def landing(x, y):
    entity = Witch(x, y)
    speed = randint(1,10)
    animate(entity,speed)
def animate(entity,speed):
    if canvas.coords(entity.tag)[2]+40 < 1000 and canvas.coords(entity.tag)[3]+40 < 500:
        entity.move(speed)
        canvas.after(10,animate,entity,speed)

landing(randint(50, 400), randint(50, 200))
landing(randint(50, 400), randint(50, 200))

canvas.mainloop()