This, of course, is not my code. It's from my object oriented programming course book. During class I ran the program and it worked a good 75% of the time without making any changes. For some reason, when I run it at home(either on python 2 or python 3) it won't work at all. I get error NameError: name 'Monitor' is not defined. Why is it doing that?

# Program: ShapeHandler.py
# Authors: Michael H. Goldwasser
#          David Letscher
#
# This example is discussed in Chapter 15 of the book
# Object-Oriented Programming in Python
#
from cs1graphics import *

class ShapeHandler(EventHandler):
  def __init__(self, monitor):
    EventHandler.__init__(self)
    self._monitor = monitor

  def handle(self, event):
    if event.getDescription() == 'mouse click':
      self._monitor.release()

if __name__ == '__main__':
  paper = Canvas()
  checkpoint = Monitor()
  handler = ShapeHandler()

  cir = Circle(10, Point(50,50))
  cir.setFillColor('blue')
  cir.addHandler(handler)
  paper.add(cir)
  square = Square(20, Point(25,75))
  square.setFillColor('red')
  square.addHandler(handler)
  paper.add(square)

  checkpoint.wait()
  paper.setBackgroundColor('green')

  checkpoint.wait()
  paper.setBackgroundColor('yellow')

I assume you have the module calgraphics. The code example is written poorly and does not assign a namespace. Just hope that Monitor is a class from the module, see if you can find it in the module source code.

When you call/instantiante the class you should be including a monitor value. Someone on Chapter 15 of the book "Object-Oriented Programming in Python" should know how to call a class. Time to go back and review the basics.

Edited 3 Years Ago by woooee

@woooee, thanks for your answer. I tried with the monitor value and still didn't work! I was just curious though since I simply ran a program thats supposed to be credible and it wouldn't work.
Thanks @HiHe :)

This article has been dead for over six months. Start a new discussion instead.