Error on graphics object

from graphics import *

class Face:

def __init__(self, win, center, size):
    eyeSize = 0.15 * size
    eyeOff = size / 3.0
    mouthSize = 0.8 * size
    mouthOff = size / 2.0
    self.head = Circle(center, size)
    self.head.draw(win)
    self.leftEye = Circle(center, eyeSize)
    self.leftEye.move(-eyeOff, -eyeOff)
    self.rightEye = Circle(center, eyeSize)
    self.rightEye.move(eyeOff, -eyeOff)
    self.leftEye.draw(win)
    self.rightEye.draw(win)
    p1 = center.clone()
    p1.move(-mouthSize/2, mouthOff)
    p2 = center.clone()
    p2.move(mouthSize/2, mouthOff)
    self.mouth = Line(p1,p2)
    self.mouth.draw(win)
    self.parts = [self.head, self.leftEye, self.rightEye, p1, p2, self.mouth]
    
def ReturnCenter(self):
    return self.head.center

def move(self, dx, dy):
    for part in self.parts:
        part.move(dx,dy)

def main():

win = GraphWin("Animação Cara", 500, 500)
win.setBackground("peachpuff")
win.setCoords(100, 100, -100, -100)

face = Face(win, Point(0,0), 15)

dx = 0.5
dy = 0.5

for i in range(10000):
    
    c = face.ReturnCenter()
    
    if c.getX() > 85:
        dx = -0.5
    elif c.getX() < -85:
        dx = 0.5
    elif c.getY() > 85:
        dy = -0.5
    elif c.getY() < -85:
        dy = 0.5
    face.move(dx,dy)
    update(50)

main()

Can you please help me with the following error?

File “(…)”, line 34, in ReturnCenter
return self.head.center

AttributeError: ‘Circle’ object has no attribute ‘center’

You will need to check the documentation for this “graphics” library you

are using. I expect it is a third-party library. Where did you get it

from? It’s not a standard part of Python.

In particular, you need to check the documentation for the Circle class.

If it has no attribute “center”, maybe it uses British spelling

“centre”, or some other word like “origin” or “middle”, or maybe it has

no centre attribute at all.

If you open an interactive interpreter, then you can do this:

inport graphics

help(graphics.Circle)

and read what that says.

I’m learning Python at my university and the teachers gave us this library to install for Spyder to use with Python.

I tried to do what you suggested and here’s what it displays:

Help on class Circle in module graphics:

class Circle(Oval)
| Circle(center, radius)
|
| Generic base class for all of the drawable objects
|
| Method resolution order:
| Circle
| Oval
| _BBox
| GraphicsObject
| builtins.object
|
| Methods defined here:
|
| init(self, center, radius)
| Initialize self. See help(type(self)) for accurate signature.
|
| repr(self)
| Return repr(self).
|
| clone(self)
|
| getRadius(self)

Methods inherited from _BBox:
getCenter(self)
getP1(self)
getP2(self)
----------------------------------------------------------------------
Methods inherited from GraphicsObject:
draw(self, graphwin)
Draw the object in graphwin, which should be a GraphWin
object. A GraphicsObject may only be drawn into one
window. Raises an error if attempt made to draw an object that
is already visible.
move(self, dx, dy)
move object dx units in x direction and dy units in y
direction
setFill(self, color)
Set interior color to color
setOutline(self, color)
Set outline color to color
setWidth(self, width)
Set line weight to width
undraw(self)
Undraw the object (i.e. hide it). Returns silently if the
object is not currently drawn.
----------------------------------------------------------------------
Data descriptors inherited from GraphicsObject:
dict
dictionary for instance variables (if defined)
weakref
list of weak references to the object (if defined)

Thank’s for your help!

Hi Bernado,

Looking at the output of help, there doesn’t seem to be anything like a
direct attribute to access the centre, by either spelling.

It looks like you have to call the getCenter method. Don’t forget to
put parentheses after it:

some_circle.getCenter    # No
some_circle.getCenter()  # Yes

Good luck!