Wxpython, using more than 1 timer?

janama austuff at gmail.com
Sat Jul 22 08:50:53 EDT 2006


Thanks for that, cheers

Regards


nikie wrote:
> janama wrote:
>
> > Hi all,
> >
> > Using wx
> > When adding a second timer as i have the first,  the second timer
> > adding  stops the first timer (updating or stops?) . In this example im
> > updating a uptime and localtime label. It works fine for displaying the
> > last "self.startTimer2()" called. But prevents the previous
> > self.startTimer1() from running . Im doing something fundamentally
> > wrong i guess?
> >
> >     def __init__(self, parent):
> >             self._init_ctrls(parent)
> >
> >             #Start timers
> >             self.startTimer1()
> >             self.startTimer2()
> >
> >     def startTimer1(self):
> >         self.t1 = wx.Timer(self)
> >         self.t1.Start(360) # 360000 ms = 1/10 hour
> >         self.Bind(wx.EVT_TIMER, self.OnUpTime)
> >
> >     def startTimer2(self):
> >         self.t2 = wx.Timer(self)
> >         self.t2.Start(1000) # run every second
> >         self.Bind(wx.EVT_TIMER, self.OnTime)
> >
> >     def OnTime(self,evt):
> >         self.lblTime.SetLabel(str(time.localtime()))
> >
> >     def OnUpTime(self, evt):
> >         self.lblUptime.SetLabel('Running ' + (str(myTimerText[0])) + '
> > hours') # 1/10  hour count
> >         myTimerText[0] = myTimerText[0] + .1
> >
> > Any help appreciated, ta
>
> The problem is not that the first timer ist stopped, the problem is
> that both timers happen to call the same method in the end.
>
> Think of the "Bind" method as an assignment: it assigns a handler
> function to an event source. If you call it twice for the same event
> source, the second call will overwrite the first event handler. That's
> what happens in your code.
>
> The easiest way to change this is by using different ids for the
> timers:
>
>     def startTimer1(self):
>         self.t1 = wx.Timer(self, id=1)
>         self.t1.Start(2000)
>         self.Bind(wx.EVT_TIMER, self.OnUpTime, id=1)
>
>     def startTimer2(self):
>         self.t2 = wx.Timer(self, id=2)
>         self.t2.Start(1000)
>         self.Bind(wx.EVT_TIMER, self.OnTime, id=2)
>
> This way, the timers launch two different events, which are bound to
> two different methods.




More information about the Python-list mailing list