I'm using cs1graphics module. I'm like really stuck as to what do next. What I'm attempting to do is create a handler that counts the number of clicks on the canvas and when the exit button is clicked it exits the canvas. I originally added the exit canvas outside of the class before I realized that my class had no attribute to the exit button so I added the exit button in the class but it doesnt come out, nor does my program count the clicks. I've read countlessly on the topic of event handling and I am seriously just stuck. I really don't need anyone to do it for me, I just need to understand what it is that I'm doing wrong. Its driving me crazy!

from cs1graphics import*
class MouseHandler(EventHandler):
    def __init__ (self, textObj):
        EventHandler.__init__(self)
        self._text = textObj
        self._count = 0
        self._text.setMessage(str(self._count))

    def handle(self, event):
        if event.getDescription() == 'mouse click':
            self._counter >= 1
            self._text.setMessage(str(self._count))




class ExitButtonHandler(EventHandler):
    def __init__(self, message='', centerPt=None):
        EventHandler.__init__(self)
        self._exitButton=Rectangle(60, 30)
        self._exitButton.setFillColor('grey')
        self._message='Exit'
        self.centerPt=Point(600,555)



    def handle(self, event):

        if event.getDescription() == 'mouse click':
            if event.getTrigger()in self._exitButton:
                exit()


def main():
    paper=Canvas(700, 600, 'white', 'Number of clicks')



    text1=Text('Times clicked:', 12, Point(140, 550))
    paper.add(text1)




    text2=Text('', 12, Point(300, 550))
    paper.add(text2)
    counter=MouseHandler(text2)
    paper.addHandler(counter)


    exitEvent = ExitButtonHandler()
    paper.addHandler(exitEvent)



main()

Standard, no, but it is freely available and appears to be reasonably well documented. Unfortunately, I'm short on time right now, or else I would dig into it a bit deeper; as it is, I'll try to get back to it later tonight, if I have the opportunity (though I doubt I will).

unfortunately I was required to used cs1graphics. I have been taking off and putting on for days with no avail!

The reason your instructor is using cs1graphics module is so that outside people can not help you as easily as with common Tkinter GUI toolkit. Most likely your module is limited wrapper of Tkinter.

A number of these Tkinter wrappers have appeared over the years, usually from folks that want to sell you their book. These books are then used by somewhat myopic instructors that are too lazy to teach the full Tkinter code.

Edited 3 Years Ago by bumsfeld

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