物体不能移动
The object cannot move
代码没有错误,但我的车无法移动。我是否错误地定义了 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()
如有任何帮助,我们将不胜感激
你没有看到你的车在移动,因为你没有给 move
函数提供正确的参数:
self.example.canvas.move(self, vx, vy)
:这里 self
是 Car
对象,它不是 canvas 上代表汽车的矩形的 index/tag。所以什么都没有发生是很正常的。在canvas:
上画车的时候要注册车的索引
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)
移动它。
此外,再次将x1
、y1
、...作为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()
代码没有错误,但我的车无法移动。我是否错误地定义了 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()
如有任何帮助,我们将不胜感激
你没有看到你的车在移动,因为你没有给 move
函数提供正确的参数:
self.example.canvas.move(self, vx, vy)
:这里 self
是 Car
对象,它不是 canvas 上代表汽车的矩形的 index/tag。所以什么都没有发生是很正常的。在canvas:
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)
移动它。
此外,再次将x1
、y1
、...作为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()