Wxpython, using more than 1 timer?

J

janama

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
 
N

nikie

janama said:
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.
 
J

janama

Thanks for that, cheers

Regards

janama said:
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.
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top