所以我有以下目录设置:
Rectangle
只是一个未被绘制的概念矩形(使用Tkinter
),但它是用于碰撞和边界的。 Rectangle2D
用作绘制或填充Rectangle
的子类。我收到错误之前的课程如下:
Rectangle.py
class Rectangle(object):
_x = -1
_y = -1
_w = -1
_h = -1
def __init__(self, x, y, w, h):
self._x = x
self._y= y
self._w = w
self._h = h
def intersects(self, other):
if isinstance(other, Rectangle):
if (self._x + self._w) > other._x > self._x and (self._y + self._h) > other._y > self._y:
return True
elif (self._x + self._w) > (other._x + other._w) > self._x and (self._y + self._h) > other._y > self._y:
return True
elif (self._x + self._w) > other._x > self._x and (other._y + other._h) > self._y > other._y:
return True
elif (self._x + self._w) > (other._x + other._w) > self._x and (other._y + other._h) > self._y > other._y:
return True
else:
return False
else:
return False
def __eq__(self, other):
return self._x == other._x and self._y == other._y and self._w == other._w and self._h == other._h
Rectangle2D.py
from tkinter import Canvas
from .Rectangle import Rectangle
class Rectangle2D(Rectangle):
def __init__(self, x, y, w, h):
super(Rectangle2D, self).__init__(x, y, w, h)
self.color = 'black'
self.id = None
def draw_rect(self, canvas):
if isinstance(canvas, Canvas):
self.id = canvas.create_rectangle(self._x, self._y, self._x + self._w, self._y + self._h, outline=self.color)
return True
else:
print("Improper Parameter Type")
return False
def fill_rect(self, canvas):
if isinstance(canvas, Canvas):
self.id = canvas.create_rectangle(self._x, self._y, self._x + self._w, self._y + self._h, fill=self.color)
return True
else:
print("Improper Parameter Type")
return False
一切正常,直到我想向Rectangle.py
添加一个返回Rectangle2D.py
的方法。除了方法之外,这还记录了要添加到Rectangle.py
的以下行:
from .Rectangle2D import Rectangle2D
这导致以下错误:
from .Rectangle import Rectangle
ImportError: cannot import name 'Rectangle'
导致此错误的原因是什么?如何解决? 另请注意我正在运行Python 3.6