0
我試圖找到mycar(x1)的座標,但它是錯誤,我試圖找到解決方案,但我無法理解它。有人可以向我解釋嗎?謝謝Tkinter錯誤#座標:預計0或4,得到5
from tkinter import *
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, x1, y1, x2, y2, color):
self.example.canvas.create_rectangle(x1, y1, x2, y2, color)
def moveit(self, vx, vy):
self.example.canvas.move(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, 3, 0, "red", my_canvas)
mycar.drawit(60, 60, 125, 90, "red")
mycar.moveit(3, 0)
print (mycar.x1)
root.mainloop()
該錯誤消息
Traceback (most recent call last): File
"D:/Users/USER/PycharmProjects/untitled/2001.py", line 55, in <module>
mycar.drawit(60, 60, 125, 90, "red") File "D:/Users/USER/PycharmProjects/untitled/2001.py", line 45, in drawit
self.example.canvas.create_rectangle(x1, y1, x2, y2, color) File "D:\Users\USER\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py",
line 2495, in create_rectangle
return self._create('rectangle', args, kw) File "D:\Users\USER\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py",
line 2474, in _create
*(args + self._options(cnf, kw))))
_tkinter.TclError: wrong # coordinates: expected 0 or 4, got 5
很顯然,我忘了這個詞 「補」。謝謝 – CPG