如何优化 Tkinter 中的线条移动?

How can I optimize my line movement in Tkinter?

Running Code

import tkinter
from tkinter import *
import math

class Template:
    def __init__(self):
        self.window = Tk()
        self.window.title("2D Display")

        self.canvas = self.canvas_display()
        self.line1 = self.line_creation(500 * .3,0,500 * .3, 1000)
        self.line3 = self.line_movement_creation(0, 0,2000, 0)

        self.speedx = 0 # Movement of Line3
        self.speedy = 9 # Movement of line3
        self.active = True

        #self.move_active()  #Code that creates the problem

当前制作网格。一旦我尝试使水平线移动。程序崩溃。没有 self.move_active() 也能工作,所以我把它注释掉了。


        self.canvas.update()

    def canvas_display(self):  #canvas
        canvas = Canvas(self.window, width=500, height=400, 
    background='black')
        canvas.pack(expand=True, fill="both")
        canvas.update()
        return canvas


    def line_creation(self,x,y,x1,y1): #creation of multpie lines
        spacing = 0
        for i in range(11):
            self.canvas.create_line(x + spacing, y ,x1 + spacing ,y1 , 
                                           width=2, fill="white")
            spacing += 100
    
    def line_movement_creation(self,x,y,x1,y1):
        spacing1 = 0
        for i in range(11):
            self.canvas.create_line(x, y+spacing1 ,x1 ,y1 + spacing1 
                                           ,width=2, fill="white")
            spacing1 += 100

这两个函数line_creation和line_movement_creation创建水平线和垂直线。两者都运行良好。

    def line_update(self): #line movement method
        self.canvas.move(self.line3, self.speedx, self.speedy)
        pos = self.canvas.coords(self.line3)
        if pos[3] >= 800:                #o
            self.canvas.move(self.line3, self.speedx,self.speedy-800)

    def move_active(self):
        if self.active:
            self.line_update()
            self.window.after(40, self.move_active)

    def run(self):
        self.window.mainloop()

这两个函数让线条动起来。我认为问题在于制作 10 行并尝试一次移动它们,或者只是这些函数本身,但我不知道。


    if __name__ == '__main__':
        Temp = Template()
        Temp.run()

此代码应该有效:

import time
from tkinter import *

class Template:
    def __init__(self):
        self.window = Tk()
        self.window.title("2D Display")

        self.canvas = self.canvas_display()
        self.line1 = self.line_creation(500 * .3,0,500 * .3, 1000)
        self.line3 = self.line_movement_creation(0, 0,2000, 0)

        self.speedx = 0 # x movement of line3
        self.speedy = 9 # y movement of line3
        self.active = True

        self.move_active()  #Code that creates the problem
        self.canvas.update()

    def canvas_display(self):  #canvas
        canvas = Canvas(self.window, width=500, height=400, background='black')
        canvas.pack(expand=True, fill="both")
        canvas.update()
        return canvas

    def line_creation(self,x,y,x1,y1): #creation of multple lines
        spacing = 0
        lines = []
        for i in range(11):
            id = self.canvas.create_line(
                x + spacing,
                y,
                x1 + spacing,
                y1, 
                width=2,
                fill="white",
            )
            lines.append(id)
            spacing += 100
        return lines

    def line_movement_creation(self,x,y,x1,y1):
        spacing1 = 0
        lines = []
        for i in range(11):
            id = self.canvas.create_line(
                x,
                y+spacing1,
                x1,
                y1 + spacing1,
                width=2,
                fill="white"
            )
            lines.append(id)
            spacing1 += 100
        return lines

    def line_update(self): #line movement method
        for line in self.line3:
            self.canvas.move(line, self.speedx, self.speedy)
            pos = self.canvas.coords(line)
            if pos[3] >= 800:
                self.canvas.move(line, self.speedx, self.speedy - 800)

    def move_active(self):
        if self.active:
            self.line_update()
            self.window.after(40, self.move_active)

    def run(self):
        self.window.mainloop()

if __name__ == '__main__':
    Temp = Template()
    Temp.run()

区别:我将 line_creation()line_movement_creation() 每个 return 列为他们创建的行的 ID 列表。 line_creation() 的列表保存到 self.line1line_movement_creation() 的列表保存到 self.line3line_update() 然后使用 self.line3 移动水平线。

这是旧的 line_update() 方法:

def line_update(self): #line movement method
    self.canvas.move(self.line3, self.speedx, self.speedy)
    pos = self.canvas.coords(self.line3)
    if pos[3] >= 800:                #o
        self.canvas.move(self.line3, self.speedx,self.speedy-800)

...这是新的 line_update() 方法:

def line_update(self): #line movement method
    for line in self.line3:
        self.canvas.move(line, self.speedx, self.speedy)
        pos = self.canvas.coords(line)
        if pos[3] >= 800:
            self.canvas.move(line, self.speedx, self.speedy - 800)

如您所见,这里唯一的区别是它遍历 self.line3 中的所有行并单独移动每一行。移动坐标一模一样