From ad5a98b65fb60b91db5582800f2553c6afb67601 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Sat, 11 Mar 2023 03:23:28 -0500 Subject: [PATCH] Release mouse grabs when owning Axes is removed Otherwise, _nothing_ will remove the grab, and no other Axes can grab the mouse (either raising the `RuntimeError` exception, or in the case of widgets, ignoring mouse events altogether.) Fixes #25345 --- lib/matplotlib/figure.py | 1 + lib/matplotlib/tests/test_backend_bases.py | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index d2568fddbf06..84a66d7a2371 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -931,6 +931,7 @@ def _break_share_link(ax, grouper): self._axobservers.process("_axes_change_event", self) self.stale = True self._localaxes.remove(ax) + self.canvas.release_mouse(ax) # Break link between any shared axes for name in ax._axis_names: diff --git a/lib/matplotlib/tests/test_backend_bases.py b/lib/matplotlib/tests/test_backend_bases.py index ba8b30a7c957..a4dd8f277f96 100644 --- a/lib/matplotlib/tests/test_backend_bases.py +++ b/lib/matplotlib/tests/test_backend_bases.py @@ -95,6 +95,16 @@ def test_non_gui_warning(monkeypatch): in str(rec[0].message)) +def test_grab_clear(): + fig, ax = plt.subplots() + + fig.canvas.grab_mouse(ax) + assert fig.canvas.mouse_grabber == ax + + fig.clear() + assert fig.canvas.mouse_grabber is None + + @pytest.mark.parametrize( "x, y", [(42, 24), (None, 42), (None, None), (200, 100.01), (205.75, 2.0)]) def test_location_event_position(x, y):