[Tutor] NameError?

Dave Angel davea at ieee.org
Thu Apr 28 04:49:26 CEST 2011


On 01/-10/-28163 02:59 PM, Brad Desautels wrote:
> 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()
>

To begin with, you should actually post the error message, including its 
traceback.  But in this case, you have many instances of the same error, 
so it doesn't really matter.

Next, you should mention where you get this foreign import, "graphics". 
  It's not standard Python, nor one of the common ones discussed here 
all the time.

Anyway, the error will occur when you call any method on the Face class, 
since they all refer to window, without being passed such a name as a 
parameter.  I can only guess what your graphics library wants, but 
assuming it's the same as the argument passed to __init__(), then you 
could save it there, and use the saved instance attribute whenever your 
other methods need it.

To the __init__() method, add
           self.window = window

and in the other methods, when you want to refer to it, use
     self.window, rather than window.

For example, the last line in makeOpen might become

       self.mouth.draw(self.window)


HTH
DaveA


More information about the Tutor mailing list