Skip to content

FIX: be very paranoid about checking what the current canvas is #25573

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion lib/matplotlib/tests/test_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import operator
from unittest import mock

from matplotlib.backend_bases import MouseEvent
from matplotlib.backend_bases import MouseEvent, DrawEvent
import matplotlib.colors as mcolors
import matplotlib.widgets as widgets
import matplotlib.pyplot as plt
Expand Down Expand Up @@ -1757,3 +1757,26 @@ def test_MultiCursor(horizOn, vertOn):
assert l.get_xdata() == (.5, .5)
for l in multi.hlines:
assert l.get_ydata() == (.25, .25)


def test_parent_axes_removal():

fig, (ax_radio, ax_checks) = plt.subplots(1, 2)

radio = widgets.RadioButtons(ax_radio, ['1', '2'], 0)
checks = widgets.CheckButtons(ax_checks, ['1', '2'], [True, False])

ax_checks.remove()
ax_radio.remove()
with io.BytesIO() as out:
# verify that saving does not raise
fig.savefig(out, format='raw')

# verify that this method which is triggered by a draw_event callback when
# blitting is enabled does not raise. Calling private methods is simpler
# than trying to force blitting to be enabled with Agg or use a GUI
# framework.
renderer = fig._get_renderer()
evt = DrawEvent('draw_event', fig.canvas, renderer)
radio._clear(evt)
checks._clear(evt)
12 changes: 10 additions & 2 deletions lib/matplotlib/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ def __init__(self, ax):
self.ax = ax
self._cids = []

canvas = property(lambda self: self.ax.get_figure(root=True).canvas)
canvas = property(
lambda self: getattr(self.ax.get_figure(root=True), 'canvas', None)
)

def connect_event(self, event, callback):
"""
Expand All @@ -144,6 +146,10 @@ def _get_data_coords(self, event):
return ((event.xdata, event.ydata) if event.inaxes is self.ax
else self.ax.transData.inverted().transform((event.x, event.y)))

def ignore(self, event):
# docstring inherited
return super().ignore(event) or self.canvas is None


class Button(AxesWidget):
"""
Expand Down Expand Up @@ -2181,7 +2187,9 @@ def connect_default_events(self):

def ignore(self, event):
# docstring inherited
if not self.active or not self.ax.get_visible():
if super().ignore(event):
return True
if not self.ax.get_visible():
return True
# If canvas was locked
if not self.canvas.widgetlock.available(self):
Expand Down
Loading