8000 Merge pull request #25442 from anntzer/rd · matplotlib/matplotlib@370547e · GitHub
[go: up one dir, main page]

Skip to content

Commit 370547e

Browse files
authored
Merge pull request #25442 from anntzer/rd
Fix disconnection of callbacks when draggable artist is deparented.
2 parents 3237401 + 3ab8308 commit 370547e

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

lib/matplotlib/offsetbox.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,16 +1500,23 @@ def __init__(self, ref_artist, use_blit=False):
15001500
ref_artist.set_picker(True)
15011501
self.got_artist = False
15021502
self._use_blit = use_blit and self.canvas.supports_blit
1503-
self.cids = [
1504-
self.canvas.callbacks._connect_picklable(
1505-
'pick_event', self.on_pick),
1506-
self.canvas.callbacks._connect_picklable(
1507-
'button_release_event', self.on_release),
1503+
callbacks = ref_artist.figure._canvas_callbacks
1504+
self._disconnectors = [
1505+
functools.partial(
1506+
callbacks.disconnect, callbacks._connect_picklable(name, func))
1507+
for name, func in [
1508+
("pick_event", self.on_pick),
1509+
("button_release_event", self.on_release),
1510+
("motion_notify_event", self.on_motion),
1511+
]
15081512
]
15091513

15101514
# A property, not an attribute, to maintain picklability.
15111515
c 10000 anvas = property(lambda self: self.ref_artist.figure.canvas)
15121516

1517+
cids = property(lambda self: [
1518+
disconnect.args[0] for disconnect in self._disconnectors[:2]])
1519+
15131520
def on_motion(self, evt):
15141521
if self._check_still_parented() and self.got_artist:
15151522
dx = evt.x - self.mouse_x
@@ -1536,16 +1543,12 @@ def on_pick(self, evt):
15361543
self.ref_artist.draw(
15371544
self.ref_artist.figure._get_renderer())
15381545
self.canvas.blit()
1539-
self._c1 = self.canvas.callbacks._connect_picklable(
1540-
"motion_notify_event", self.on_motion)
15411546
self.save_offset()
15421547

15431548
def on_release(self, event):
15441549
if self._check_still_parented() and self.got_artist:
15451550
self.finalize_offset()
15461551
self.got_artist = False
1547-
self.canvas.mpl_disconnect(self._c1)
1548-
15491552
if self._use_blit:
15501553
self.ref_artist.set_animated(False)
15511554

@@ -1558,14 +1561,8 @@ def _check_still_parented(self):
15581561

15591562
def disconnect(self):
15601563
"""Disconnect the callbacks."""
1561-
for cid in self.cids:
1562-
self.canvas.mpl_disconnect(cid)
1563-
try:
1564-
c1 = self._c1
1565-
except AttributeError:
1566-
pass
1567-
else:
1568-
self.canvas.mpl_disconnect(c1)
1564+
for disconnector in self._disconnectors:
1565+
disconnector()
15691566

15701567
def save_offset(self):
15711568
pass

lib/matplotlib/tests/test_offsetbox.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,3 +450,11 @@ def test_paddedbox():
450450
pb = PaddedBox(ta, pad=15, draw_frame=True)
451451
ab = AnchoredOffsetbox('lower right', child=pb)
452452
ax.add_artist(ab)
453+
454+
455+
def test_remove_draggable():
456+
fig, ax = plt.subplots()
457+
an = ax.annotate("foo", (.5, .5))
458+
an.draggable(True)
459+
an.remove()
460+
MouseEvent("button_release_event", fig.canvas, 1, 1)._process()

0 commit comments

Comments
 (0)
0