Building a Chaco/TraitsUI application with pyface timer

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Building a Chaco/TraitsUI application with pyface timer

Tiago Pereira
Hi all,

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
failed.

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().



Thanks!

Tiago

_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev

view_image.py (5K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Building a Chaco/TraitsUI application with pyface timer

Tiago Pereira
Sorry, I sent the wrong attachment in my last email. Here a simpler
version that will actually work.

Cheers,

Tiago

_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev

view_image_simple.py (2K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Building a Chaco/TraitsUI application with pyface timer

Tiago Pereira
In reply to this post by Tiago Pereira
Hi,

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):
       if self.isrunning:
           self.im_number += 1

And also another method everytime the isrunning bool Trait has changed,
that starts and stops the timer:

def _isrunning_changed(self):
     if self.isrunning:
         self.timer.Start()
     else:
         self.timer.Stop()

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.

Cheers,

Tiago



_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev

view_image_simple.py (2K) Download Attachment