Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wxpython Timer Event Interval

Tags:

wxpython

I am trying to write a gui application with wx python and I need to control the interval of the timer event. Here is my code currently:

self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.on_timer, self.timer)        
self.timer.Start(750) # start timer after a delay

This is the right framework but I cannot control the interval or how often the EVT_TIMER occurs. I have been trying to figure out using the wx TimerEvent class but without any luck. I feel like this should be what I need but it isn't working:

self.timer = wx.Timer(self)
self.timerEvent = wx.TimerEvent(self.timer.GetId(),10)
self.Bind(wx.EVT_TIMER, self.on_timer, self.timer) 

Thanks!

like image 952
Kevin Avatar asked Feb 21 '23 14:02

Kevin


2 Answers

I wrote a tutorial on timers a while back that might help you figure this out. Basically you do as you mentioned in the first code snippet. You have to Start the timer and pass it a value in milliseconds. So 1000 would = 1 second. You don't need that bit with the wx.TimerEvent. At least, I've never needed that.

Anyway, the timer event fires every 750 milliseconds in your example, or a little less than a second. I think if you machine's CPU gets pegged, it can interrupt or delay timer events, but otherwise they're very reliable.

like image 108
Mike Driscoll Avatar answered Apr 25 '23 20:04

Mike Driscoll


In addition, if you want to control how often the EVT_TIMER occurs, you must set up the second parameter, the boolean oneShot. By default, it is set to False, but you can specify something like self.timer.Start(milliseconds = 750, oneShot = True), and the timer will still shoting untill the self.timer.Stop() bit appears.

All the best.

like image 39
Mauro Avatar answered Apr 25 '23 20:04

Mauro