Building a Chaco/TraitsUI application with pyface timer
I am getting started in GUI programming with TraitsUI and Chaco. I have
followed several of the tutorials and I'm amazed at how powerful and
simple it is.
Right now I am trying to write a simple application that shows a
sequence of images. These are shown using chaco's Plot().img_plot. There
is a trait (im_number) that keeps track of the frame number being
displayed, and I created a _im_number_changed method that makes sure to
change the plot every time it im_number changes. Currently I have a
simple slider to control the value of this trait, and every time I
change the slider the image is updated.
My next step is to include the option to animate this sequence of images
by pressing a start/stop button, which will increment im_number. By
looking at the examples it seems that this should be done with a timer.
Unfortunately I could not find a simple example that does this.
I looked at the updating_plot?.py examples and they do a similar thing
to what I want, but they make use of wx and its timers directly. I would
prefer my application to be more abstract and not depend on wx or Qt,
hence I thought that the pyface timer is the way to go. The
scalar_image_function_inspector.py from the chaco examples does an
animation and uses pyface's timer. However, this example is far too
complex for my current knowledge and my attempts at making it simpler
Could someone give me some hints on how to employ a timer (and a
start/stop button) on a Chaco/TraitsUI application? Or perhaps point me
toward some relevant examples. I am attaching a simple version of the
code I have. (For simplicity I use a numpy.roll on a given image instead
of a real sequence of images.)
A related question: in several of the examples I have seen that the
onTimer methods usually have a call to plot.request_redraw(). Is this
always necessary? Using my example with the trait slider, I didn't have
to call request_redraw() after calling set_data().
Found the answer to my question. Here it goes, for future reference.
On 11/07/12 3:26 PM, Tiago Pereira wrote:
> Could someone give me some hints on how to employ a timer (and a
> start/stop button) on a Chaco/TraitsUI application?
First import Timer from pyface:
from pyface.timer.api import Timer
Then, on the main application create a timer instance:
timer = Instance(Timer)
On the __init__ method of the main application, define this timer:
self.timer = Timer(40, self.onTimer)
And also define the onTimer method to do something everytime the timer
changes. In my case:
def onTimer(self, *args):
self.im_number += 1
And also another method everytime the isrunning bool Trait has changed,
that starts and stops the timer:
The updated version of the script is attached. It will get an ugly end
when im_number goes beyond the values specified, but then it's up to the
user to set appropriate controls when invalid values are reached.
It would be nice to have a chaco example showing a timer application. I
can create a more polished version of this script if someone is interested.