So I have the following directory setup:
- Project
- Misc Packages and Modules
- resources
- shapes
- Rectangle.py
- Rectangle2D.py
- shapes
Rectangle
is just a conceptual rectangle that isn't ever drawn (using Tkinter
) but is there for collision and bounds. Rectangle2D
is used as a subclass to draw or fill a Rectangle
. The classes before I got the error were as followed:
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
Everything was working fine until I wanted to add a method to Rectangle.py
that would return a Rectangle2D.py
. This record the following line to be added to Rectangle.py
in addition to the method:
from .Rectangle2D import Rectangle2D
This resulted in the following error:
from .Rectangle import Rectangle
ImportError: cannot import name 'Rectangle'
What is causing this error and how do I fix it? Also note I am running Python 3.6