[Tutor] NameError?

Brad Desautels outsideme99 at live.com
Thu Apr 28 03:25:28 CEST 2011


Hello, I am working on a problem for my computer science class. This program
is supposed to change the expression of the face when the appropriate button
is clicked. It seems logical that it should work, however, when I click one
of the buttons, I get "NameError: global name 'window' is not defined".
 
# face.py
 
from graphics import *
 
class Face:
    """A class to display a face in a graphics window and change
expression"""
    def __init__(self, window, center, size):
        """Defines the method for creating a face"""
        eyeSize = .15 * size
        eyeOff = size/3
        mouthSize = .8 * size
        mouthOff = size/2
        self.head = Circle(center, size)
        self.head.draw(window)
        self.leftEye = Circle(center, eyeSize)
        self.leftEye.move(-eyeOff, -eyeOff)
        self.rightEye = Circle(center, eyeSize)
        self.rightEye.move(eyeOff, -eyeOff)
        self.leftEye.draw(window)
        self.rightEye.draw(window)
        p1 = center.clone()
        p1.move(-mouthSize/2, mouthOff)
        p2 = center.clone()
        p2.move(mouthSize/2, mouthOff)
        self.mouth = Line(p1,p2)
        self.mouth.draw(window)
    def makeOpen(self):
        """Defines the method for making the mouth open"""
        self.mouth.undraw()
        self.mouth = Oval(Point(170,375),Point(330,325))
        self.mouth.draw(window)
    def makeWow(self):
        """Defines the method for making the face frown"""
        self.mouth.undraw()
        self.mouth = Circle(Point(250,350),50)
        self.mouth.draw(window)
    def makeWink(self):
        """Defines the method for making the face wink"""
        self.rightEye.undraw()
        self.rightEye=Line(Point(290,190),Point(334,190))
        self.rightEye.draw(window)
    def makeDefault(self):
        """Defines the methof for returning the face to default position"""
        self.mouth.undraw()
        self.rightEye.undraw()
        self.mouth = Line(Point(170,350),Point(330,350))
        self.rightEye = Circle(Point((250+(200/3)),(250-(200/3))), 30)
        self.mouth.draw(window)
        self.rightEye.draw(window)
 
class Button:
    """Creates buttons to activate methods"""
    def __init__(self,window,center,width,height,label):
        """Creates button"""
        w,h = width/2.0, height/2.0
        x,y = center.getX(), center.getY()
        self.xmax, self.xmin = x+w,x-w
        self.ymax, self.ymin = y+h,y-h
        p1 = Point(self.xmin, self.ymin)
        p2 = Point(self.xmax, self.ymax)
        self.rect = Rectangle(p1,p2)
        self.rect.setFill('gray')
        self.rect.draw(window)
        self.label = Text(center,label)
        self.label.draw(window)
    def clicked(self, p):
        return(self.xmin<=p.getX()<=self.xmax and
               self.ymin<=p.getY()<=self.ymax)
 
def main():
    window = GraphWin("Face Window",500,500)
    face = Face(window, Point(250,250), 200)
    button1 = Button(window,Point(50,400),90,25,"Open Mouth")
    button2 = Button(window,Point(50,425),90,25,"Wow")
    button3 = Button(window,Point(50,450),90,25,"Wink")
    button4 = Button(window,Point(50,475),90,25,"Default")
    button5 = Button(window,Point(50,25),90,25,"Close")
    click = window.getMouse()
    while not button5.clicked(click):
        if button1.clicked(click):
            face.makeOpen()
        elif button2.clicked(click):
            face.makeWow()
        elif button3.clicked(click):
            face.makeWink()
        elif button4.clicked(click):
            face.makeDefault()
        elif button5.clicked(click):
            break
    window.close()
main()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110427/fa4d27da/attachment-0001.html>


More information about the Tutor mailing list