- My results showed "circle object has no attribute 'canvas'" Is there any way that I can solve it? My results showed "circle object has no attribute 'canvas'" Is there any way that I can solve it? thank you.
class EnlargeShrinkCircle:
def __init__(self, x0, y0, r):
self.x0 = x0
self.y0 = y0
self.r = r
def enlargeCircle(self, r):
self.r = r
c1 = circle( 4, 5, 1)
c1_rdelta = 3
if c1.r == 1:
c1.r = 2
c1.canvas.create_oval(
0.5 - self.r, 0.5 - self.r,
0.5 self.r, 0.5 self.r )
print(c1.x0, c1.y0, c1.r)
---------------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_10552/2081812378.py in <module>
14 if c1.r == 1:
15 c1.r = 2
---> 16 c1.canvas.create_oval(
17 0.5 - self.r, 0.5 - self.r,
18 0.5 self.r, 0.5 self.r )
AttributeError: 'circle' object has no attribute 'canvas'
CodePudding user response:
There is no variable called self
outside the definition of your class. When you create the circle object c1
it becomes what you were referring to as self
inside the class. self
basically refers to an object of the class. So you would call your methods and properties like c1.r
c1 = circle( 4, 5, 1)
c1_rdelta = 3
if c1.r == 1:
c1.r = 2
c1.canvas.create_oval(
0.5 - self.r, 0.5 - self.r,
0.5 self.r, 0.5 self.r )
print(c1.x0, c1.y0, c1.r)