2 timers, doesnt work?

Kiran Kiran.Karra at gmail.com
Tue Aug 1 16:58:33 EDT 2006


Makes sense Frank, and that seemed to work also, so thanks a lot!


Frank Millman wrote:
> Kiran wrote:
> > I am creating 2 timers inside a GUI, but it seems that only the one
> > declared last (the second timer), gets triggered, but the first one
> > doesnt.
> >
>
> You really should check the archives before posting. Exactly the same
> question was asked less than a week ago.
>
> The original question was answered by Nikie. I have quoted the reply
> verbatim -
>
> ----------------------------------------------------------------
> The problem is not that the first timer is 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.
> ----------------------------------------------------------------
>
> I would suggest one minor change. Create a variable for the id using
> wx.NewId(), and pass the variable as an argument in both places,
> instead of hard-coding the id. That way you are guaranteed to get a
> unique id. In a large project it can be difficult to keep track of
> which id's have been used if they are all hard-coded.
> 
> Frank Millman




More information about the Python-list mailing list