471,594 Members | 1,695 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,594 software developers and data experts.

Wxpython, using more than 1 timer?

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

Jul 22 '06 #1
2 4440
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.

Jul 22 '06 #2
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.
Jul 22 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by jblazi | last post: by
8 posts views Thread by Erik Johnson | last post: by
8 posts views Thread by Jan Danielsson | last post: by
reply views Thread by Robin Dunn | last post: by
reply views Thread by Robin Dunn | last post: by
6 posts views Thread by sbernste | last post: by
7 posts views Thread by Terry Carroll | last post: by
2 posts views Thread by whatazor | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.