我试图在mycar(x1)的坐标中找到但是我试图找到解决方案但是我无法理解它是错误的。有人可以向我解释一下吗?谢谢
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
答案 0 :(得分:3)
create_rectangle
获取4个位置参数(x1,y1,x2,y2),然后是一组键值对选项。
您需要以下内容:
self.example.canvas.create_rectangle(x1, y1, x2, y2, fill=color)