2 timers, doesnt work?

K

Kiran

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.

Here is the code for the timers:
# main timer, which governs when the register watcher get updated
self.mainTimer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.UpdateAll, self.mainTimer)
self.mainTimer.Start(5, False)

# graph timer, which governs when the graph gets updated
self.grphTimer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.upGrphDisplay, self.grphTimer)
self.grphTimer.Start(101, False)

So in this case, only the upGrphDisplay method gets called, the
UpdateAll function doesnt get called.

Any clues as to why this is happening?

thanks a lot!
-- kiran
 
F

Frank Millman

Kiran said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top