[Matplotlib-devel] Event handling callbacks call order

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

[Matplotlib-devel] Event handling callbacks call order

Daniele Nicolodi
Hello,

I have an application where I let the user place Patches on the canvas
via mouse events. To do so I have a "button_press_event" callback
registered to the canvas. I now would like to allow to drag the drawn
patches to change their position. To do so I register other callbacks on
the canvas like in the examples at
https://matplotlib.org/users/event_handling.html. The problem is that
the callbacks are called in the order they are registered and I haven't
found a way to inhibit execution of callbacks down the stack from
previously called callback. This results in the fact that patches are
drawn on top of the old ones when the user tries to drag those around.

I can see (quite convoluted) ways to avoid this in my code, but I would
think that correctly handling this is a common occurrence and that there
could be a solution built into matplotlib. However, I haven't found it.

I am missing something?

Thank you!

Cheers,
Dan
_______________________________________________
Matplotlib-devel mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-devel
Reply | Threaded
Open this post in threaded view
|

Re: Event handling callbacks call order

Antony Lee-3
Hi,
A simple way to do this would be for the first handler to attach e.g. a "handled" ("accepted", in Qt terminology) attribute to the event and for later handlers to first check whether that attribute exists.  Note that this can be done independently of anything provided by Matplotlib itself (Matplotlib could automatically stop propagating events with that attribute set, but I'm not sure it's really worth it).
Cheers,
Antony

On Thu, Feb 14, 2019 at 1:56 AM Daniele Nicolodi <[hidden email]> wrote:
Hello,

I have an application where I let the user place Patches on the canvas
via mouse events. To do so I have a "button_press_event" callback
registered to the canvas. I now would like to allow to drag the drawn
patches to change their position. To do so I register other callbacks on
the canvas like in the examples at
https://matplotlib.org/users/event_handling.html. The problem is that
the callbacks are called in the order they are registered and I haven't
found a way to inhibit execution of callbacks down the stack from
previously called callback. This results in the fact that patches are
drawn on top of the old ones when the user tries to drag those around.

I can see (quite convoluted) ways to avoid this in my code, but I would
think that correctly handling this is a common occurrence and that there
could be a solution built into matplotlib. However, I haven't found it.

I am missing something?

Thank you!

Cheers,
Dan
_______________________________________________
Matplotlib-devel mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-devel

_______________________________________________
Matplotlib-devel mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-devel
Reply | Threaded
Open this post in threaded view
|

Re: Event handling callbacks call order

matplotlib - devel mailing list
In reply to this post by Daniele Nicolodi
I used the same Matplotlib examples to implement something similar in widgets.py (https://github.com/rayosborn/nexpy/blob/edit-options/src/nexpy/gui/widgets.py). There is an NXpatch class, with a variety of subclassed shapes. Once they are created and connected to the plot, they are independently draggable and resizable as promised.

>>> plotview.deactivate()
>>> r=NXrectangle(1650,2000,200,1000,color='g’)
>>> r.connect()
>>> plotview.activate()

I do have to deactivate zoom and pan modes when adding the shapes, but they can be reactivated afterwards and the shapes will still be movable provided the cursor is within them.  I don’t know if that is the problem you are encountering. In the code, ‘plotview’ is the active plotting window - you can find the activate and deactivate functions in https://github.com/rayosborn/nexpy/blob/edit-options/src/nexpy/gui/plotview.py.
 
Ray

On Feb 13, 2019, at 6:46 PM, Daniele Nicolodi <[hidden email]> wrote:

Hello,

I have an application where I let the user place Patches on the canvas
via mouse events. To do so I have a "button_press_event" callback
registered to the canvas. I now would like to allow to drag the drawn
patches to change their position. To do so I register other callbacks on
the canvas like in the examples at
https://matplotlib.org/users/event_handling.html. The problem is that
the callbacks are called in the order they are registered and I haven't
found a way to inhibit execution of callbacks down the stack from
previously called callback. This results in the fact that patches are
drawn on top of the old ones when the user tries to drag those around.

I can see (quite convoluted) ways to avoid this in my code, but I would
think that correctly handling this is a common occurrence and that there
could be a solution built into matplotlib. However, I haven't found it.

I am missing something?

Thank you!

Cheers,
Dan
_______________________________________________
Matplotlib-devel mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-devel


_______________________________________________
Matplotlib-devel mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-devel