[Tutor] using a Timer

seanf at email.arizona.edu seanf at email.arizona.edu
Sat Oct 23 22:13:11 CEST 2004


Quoting Kent Johnson <kent_johnson at skillsoft.com>:


> Looking at the rest of your code, you are overusing self (abusing your
> self? ;). Normally you only use self for variables that need to be shared
> between two methods or that need to persist between method calls. Temporary
> variables don't have to be stored in the instance.

ahh, thank you. It seemed excessive. I just had a general misconception about
scope in Python, but I seem to have it straight now. Also, the text box is
working great now.

Here's the new problem. I want to create a button that will change the model
every 1 second or so, and then another button to stop it. I thought maybe a
timer would work, but it only seems to run once.

Here is the code:

class cFrame(Frame):

    def __init__(self, aModel, parent = None):
        Frame.__init__(self, parent)
        self.pack()

        self.model = aModel
        self.view = cView(self)
        self.view.pack(side=TOP)
        self.model.addObserver(self.view)
        self.model.notifyObservers()

        t = Timer(1, self.tick)
        start = Button(self, text='Start', command = t.start)
        start.pack(side=LEFT)

        stop = Button(self, text='Stop', command = t.cancel)
        stop.pack(side=LEFT)

        step = Button(self, text='Step', command = self.tick)
        step.pack(side=LEFT)

        quit = Button(self, text='Quit', command=self.quit)
        quit.pack(side=RIGHT)


    def tick(self):
        self.model.tick()

if __name__ == '__main__': cFrame().mainloop()

With my new knowledge, I guess view doesn't have to be self.view. Basically I
just want to call model.tick() over and over.

Another problem: for some reason when I press the quit button, it freezes the
program: any ideas?

There are no errors or anything, I just am not sure how to implement this in
Python.

thanks for all the help,
Sean


--
Albert Einstein, when asked to describe radio, replied:
"You see, wire telegraph is a kind of a very, very long cat. You pull his tail
in New York and his head is meowing in Los Angeles. Do you understand this?
And radio operates exactly the same way: you send signals here, they receive
them there. The only difference is that there is no cat."




More information about the Tutor mailing list