对象无法移动

时间:2017-01-23 10:02:00

标签: python tkinter tkinter-canvas

代码没有错误,但mycar无法移动。我是否错误地定义了for循环?我也尝试将for循环移动到def moveit(self,vx,vy),但没有区别。 这是我的代码:

from tkinter import *
import tkinter as tk
import time
class Example(tk.Frame):

    def __init__(self, parent):
        tk.Frame.__init__(self)

        #create a canvas
        self.canvas = tk.Canvas(width=600, height=250)
        self.canvas.pack()
        self.road()
        self.crossing()

    def road(self):
        self.canvas.create_line(50, 50, 450, 50)
        self.canvas.create_line(50, 100, 450, 100)

    def crossing(self):
        self.canvas.create_line(350, 50, 350, 100)
        self.canvas.create_line(375, 50, 375, 100)


class Car:
    def __init__(self, x1, y1, x2, y2, vx, vy, color, example):
        self.x1 = x1
        self.y1 = y1
        self.x2 = x2
        self.y2 = y2
        self.vx = vx
        self.vy = vy
        self.color = color
        self.example = example

    def drawit(self, x1, y1, x2, y2, color):
        self.example.canvas.create_rectangle(x1, y1, x2, y2, fill=color)

    def moveit(self, vx, vy):
        self.example.canvas.move(self, vx, vy)

if __name__ == "__main__":
    root = tk.Tk()
    my_canvas = Example(root)
    my_canvas.pack(fill="both", expand=True)
    mycar = Car(60, 60, 125, 90, 20, 0, "red", my_canvas)
    mycar.drawit(60, 60, 125, 90, "red")
    mycar.moveit(20, 0)
    print(mycar.x1)
    root.update()
    root.mainloop()

非常感谢任何帮助

1 个答案:

答案 0 :(得分:0)

您没有看到您的车开动,因为您没有为move功能提供正确的参数:

self.example.canvas.move(self, vx, vy):此处selfCar对象,它不是代表画布上汽车的矩形的索引/标记。所以没有任何意外是正常的。在画布上绘制汽车时应该注册汽车的索引:

def drawit(self, x1, y1, x2, y2, color):
    self.index = self.example.canvas.create_rectangle(x1, y1, x2, y2, fill=color)

然后使用self.example.canvas.move(self.index, vx, vy)移动它。

此外,重新考虑x1y1,......作为drawit的参数是没有意义的,因为它们是汽车的属性。当你在moveit移动时,你也忘了增加汽车的位置,我建议你使用after方法而不是for循环来创建动画。所以我用我的建议改写了你的代码:

import tkinter as tk

class Example(tk.Frame):

    def __init__(self, parent):
        tk.Frame.__init__(self)

        #create a canvas
        self.canvas = tk.Canvas(width=600, height=250)
        self.canvas.pack()
        self.road()
        self.crossing()

    def road(self):
        self.canvas.create_line(50, 50, 450, 50)
        self.canvas.create_line(50, 100, 450, 100)

    def crossing(self):
        self.canvas.create_line(350, 50, 350, 100)
        self.canvas.create_line(375, 50, 375, 100)

class Car:
    def __init__(self, x1, y1, x2, y2, vx, vy, color, example):
        self.x1 = x1
        self.y1 = y1
        self.x2 = x2
        self.y2 = y2
        self.vx = vx
        self.vy = vy
        self.color = color
        self.example = example

    def drawit(self):
        self.index = self.example.canvas.create_rectangle(self.x1, self.y1, self.x2, self.y2, fill=self.color)

    def moveit(self):
        self.example.canvas.move(self.index, self.vx, self.vy)
        self.x1 += self.vx
        self.x2 += self.vx
        self.y1 += self.vy
        self.y2 += self.vy
        self.example.after(100, self.moveit)

if __name__ == "__main__":
    root = tk.Tk()
    my_canvas = Example(root)
    my_canvas.pack(fill="both", expand=True)
    mycar = Car(60, 60, 125, 90, 20, 0, "red", my_canvas)
    mycar.drawit()
    mycar.moveit()
    root.mainloop()