wxPython: Using wx.Timers

This past couple of weeks, I’ve seen multiple people ask about timers and how they work either on the wxPython mailing list or on their IRC channel. So I decided it was high time I wrote a couple of example scripts to show how they’re used. I will cover two examples, the first of which will only have one timer and the second will have two. Robin Dunn contacted me with some improvements to the examples, so I have added a refactored example of each to this article.

My first example is super simple. It has only one button that starts and stops a timer. Let’s take a look at the code:

As you can see, I only import two modules: wx and time. I use the time module to post the time that the wx.Timer event fires on. The two main things to pay attention to here is how to bind the timer to an event and the event handler itself. For this example to work, you have to bind the frame to the timer event. I tried binding the timer (i.e. self.timer.Bind) but that didn’t work. So the logical thing to do was ask Robin Dunn what was going on. He said that if the parent of the timer is the frame, then the frame is the only object that will receive the timer’s events unless you derive wx.Timer and override it’s Notify method. Makes sense to me.

Regardless, let’s look at the my event handler. In it I grab the button’s label and then use a conditional IF statement to decide if I want to start or stop the timer as well as what to label the button. In this way, I can have just one function that toggles the button and the timer’s state. The part to take note of are the methods Start and Stop. They are what control the timer.

In one of my real life applications, I have a timer execute every so often to check my email. I discovered that if I shut my program down without stopping the timer, the program would basically become a zombie process. Thus, you need to make sure that you stop all your timers when your program is closed or destroyed.

Before we get to my next example, let’s take a look at refactoring this one. Here’s the code. Can you tell what’s different?

As you can see, I’ve changed the event handler to check if the timer is running or not rather than looking at the button’s label. This saves us one line, but it’s a little cleaner and shows how to accomplish the same thing in a slightly different way.

To be honest, this second example is mostly the same as the first one. The main difference is that I have two buttons and two timer instances. I decided to be geeky and have both buttons bind to the same event handler. This is probably one of my better tricks. To find out which button called the event, you can use the event’s GetEventObject method. Then you can get the label off the button. If you’re a real nerd, you’ll notice that I could combine lines 32 and 33 into this one-liner:

btnLabel = event.GetEventObject().GetLabel()

I split that into two lines to make it easier to follow though. Next, I used some string slicing to grab the button’s label number so I would know which timer to stop or start. Then my program enters my nested IF statements where it checks the button label and then the timer number. Now you know how to start and stop multiple timers too.

Once again, Robin Dunn came up with a better way to do this second example, so let’s see what we came up with:

In the “__init__” I added a dictionary that is keyed on the button objects. The values of the dictionary are the timer number, the timer object, and the number of seconds (technically milliseconds) between timer events. Next, I updated the button event handler to grab the button object from the event’s GetEventObject method and then extract the respective values using said object for the dict’s key. Then I can use the same trick I used in the other refactored example I detailed above, namely the checking of whether or not the timer is running.

This was tested on the following systems:

Windows Vista with wxPython 2.8.9.2 (msw-unicode) and Python 2.5.2.

Windows XP with wxPython 2.8.10.1 (msw-unicode) and Python 2.5.2

You can download my code below. Let me know if you test it on another OS and if you have any questions by posting in the comments section. My deepest gratitude goes out to Robin Dunn for his help with this tutorial and for making wxPython available in the first place.