[Tutor] time.sleep?

Liam Clarke cyresse at gmail.com
Thu Dec 2 06:56:17 CET 2004


Timer's in wxPython have to be attached to a widget, don't know why,
some quirk of wx.

 def on_openBackground(self, event):
    self.myTimer = wx.wxTimer(self.components.field1, -1) # create a timer
    self.myTimer.Start(5000) # launch timer, to fire every 5000ms (5 seconds)

 def on_field1_timer(self, event):
        print this

Or, if you wanted something to activate every 10 seconds, and then
stop after the fifteenth activation -

def on_init(self, event);
    self.timerFires = 0

def on_startTimer_mouseClick(self, event):
    self.myTimer = wx.wxTimer(self.components.field1, -1)
    self.myTimer.Start(10000)

def on_field1_timer(self, event):
       print "Bang!"
       self.timerFires += 1
        if self.timerFires == 10:
             self.myTimer.Stop()


On Wed, 01 Dec 2004 17:51:10 -0800, Jeff Shannon <jeff at ccvcorp.com> wrote:
> Christian Wyglendowski wrote:
> 
> >>-----Original Message-----
> >>[...] So nothing actually
> >>appears until the for loop and sleep are finished.
> >
> > I think that is because wxPython is waiting for the function to exit
> > before updating the GUI.  Since your OnGoButton() function sleeps for
> > some amount of time, it will take a while before the GUI gets updated.
> 
> Yep, exactly.
> 
> The key here is that, when using a GUI or other event-driven
> framework, you need to allow the framework some execution time too.
> What has happened is that you've told the framework to add a message
> to the text field, but the framework doesn't update the screen until
> *after* you're done handling all the events in your event queue.
> Obviously, if you're still in the same event handler, you haven't
> finished handling all events... ;)
> 
> Given that you're (apparently) using wxPython, you should look into
> using wx.Timer.  In essence, instead of sleeping in a loop and then
> updating the message every X seconds, your button handler will simply
> toggle a flag variable saying "I'm running".  You will also have a
> handler for the timer's event, and in that handler, you check the flag
> variable.  If the flag is set, then you check the file and update the
> message as needed.  If the flag is *not* set, then you do nothing.
> (It's also possible to start and stop the timer events from happening
> in the first place, rather than just ignoring them if the flag isn't
> set.  But I don't remember how to do that off the top of my head, so
> I'm not going into detail here. ;) )
> 
> Jeff Shannon
> Technician/Programmer
> Credit International
> 
> 
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.


More information about the Tutor mailing list