how to find window that generated an event

I have a matplotlib script that generates several figures.
When a figure receives an event, how do I know which figure
it did it? For example, the key event 'w' is meant to close
the figure, but so far, I have no way to control which figure
gets closed.

In the pick_event_demo.py example, each figure is connected
to an event handler specific to it. I think this wouldn't
work in my case because I don't know how many figures I'd
have until I process some data. In any case, all my event
handlers can be independent of the figure generating the
event. They just need to know which figure to refer to.

My work-around right now is to save the canvas attributes
from all the figure handle. I compare this canvas to the
canvas attribute of the event. When I found a match, I know
I have the right figure. This seems kludgey, especially
after most of the figures have been closed.

I'd appreciate any help or pointers.

Brian

I have a matplotlib script that generates several figures.
When a figure receives an event, how do I know which figure
it did it? For example, the key event 'w' is meant to close
the figure, but so far, I have no way to control which figure
gets closed.

All events are connected to figures by definition -- the figure is
placed inside a GUI canvas and that is what manages the events. The
pylab connect function is just a thing wrapper to the canvas
mpl_connection function. pylab's connect just grabs the current
figure with "gcf" and makes the call fig.canvas.mpl_connect

So in matplotlib, events do not make since w/o reference to specific figures.

In the pick_event_demo.py example, each figure is connected
to an event handler specific to it. I think this wouldn't
work in my case because I don't know how many figures I'd
have until I process some data.

I don't see why this is a problem -- just set up the connections when
you create the figure as you process the data. You'll probably need
to post an example if you have further troubles, because it is hard to
advise in the abstract.

JDH

···

On 6/26/07, Brian T.N. Gunney <gunneyb@...99...> wrote:

>I have a matplotlib script that generates several figures.
>When a figure receives an event, how do I know which figure
>it did it? For example, the key event 'w' is meant to close
>the figure, but so far, I have no way to control which figure
>gets closed.

All events are connected to figures by definition -- the figure is
placed inside a GUI canvas and that is what manages the events. The
pylab connect function is just a thing wrapper to the canvas
mpl_connection function. pylab's connect just grabs the current
figure with "gcf" and makes the call fig.canvas.mpl_connect

So in matplotlib, events do not make since w/o reference to specific
figures.

That makes sense. However, it's not clear to me which figure
is to be the "current" figure. It does not appear to be the
figure where the event occured.

>In the pick_event_demo.py example, each figure is connected
>to an event handler specific to it. I think this wouldn't
>work in my case because I don't know how many figures I'd
>have until I process some data.

I don't see why this is a problem -- just set up the connections when

It's a problem because the number of figures is variable. I don't
know how many call-back functions to define when I write the script.

you create the figure as you process the data. You'll probably need
to post an example if you have further troubles, because it is hard to
advise in the abstract.

Ok. Here is an example. Run this and type 'w' in window 0.
You should expect figure 0 to be closed, but instead, figure
2 is closed.

#!/usr/bin/env python

import sys
from pylab import *

# This is meant to illustrate that the number of figures is
# unknown until run time.
if len(sys.argv) > 1:
    nfigs = int(sys.argv[1])
else:
    nfigs = 3

# Process key presses.
def on_key(event):
        if event.key=='w':
                # Close current window.
                # How do we control which window gets closed?
                close()
        elif event.key=='q':
                # Exit.
                sys.exit(0)

# Generate as many figures as specified on command line.
for i in range(nfigs):
    figure(i)
    title( str(i) )
    plot(range(i+2))
    connect('key_press_event', on_key)

show()

Thanks for helping.
Brian

···

On Wed, Jun 27, 2007 at 09:16:09AM -0500, John Hunter wrote:

On 6/26/07, Brian T.N. Gunney <gunneyb@...99...> wrote:

The event has a canvas attribute, which is the figure canvas that
contains the figure, so you should be able to do

close(event.canvas.figure)

···

On 6/27/07, Brian T.N. Gunney <gunneyb@...99...> wrote:

# Process key presses.
def on_key(event):
        if event.key=='w':
                # Close current window.
                # How do we control which window gets closed?
                close()

That is exactly what I need. Thank you.
Brian

···

On Wed, Jun 27, 2007 at 11:40:39AM -0500, John Hunter wrote:

On 6/27/07, Brian T.N. Gunney <gunneyb@...99...> wrote:

># Process key presses.
>def on_key(event):
> if event.key=='w':
> # Close current window.
> # How do we control which window gets closed?
> close()

The event has a canvas attribute, which is the figure canvas that
contains the figure, so you should be able to do

close(event.canvas.figure)