From f7ae56e7ec7caff1634cb090c3da7248a00fee56 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Tue, 10 Nov 2020 10:47:42 +0100 Subject: [PATCH] Use lambdas to prevent gc'ing and deduplication of widget callbacks. This makes the behavior of widget callbacks more consistent with the old behavior (of using strong refs), and also more consistent with other widgets which don't rely on CallbackRegistry at all, such as SpanSelector and Lasso. --- doc/api/next_api_changes/behavior/18226-ES.rst | 10 ---------- lib/matplotlib/widgets.py | 10 +++++----- 2 files changed, 5 insertions(+), 15 deletions(-) delete mode 100644 doc/api/next_api_changes/behavior/18226-ES.rst diff --git a/doc/api/next_api_changes/behavior/18226-ES.rst b/doc/api/next_api_changes/behavior/18226-ES.rst deleted file mode 100644 index c6247737f662..000000000000 --- a/doc/api/next_api_changes/behavior/18226-ES.rst +++ /dev/null @@ -1,10 +0,0 @@ -Widgets use ``CallbackRegistry`` to save callbacks -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -`.Widget`\s with event observers now use a `.CallbackRegistry` for storing -callbacks. This is consistent with canvas event callbacks, and fixes some bugs -in widget callback handling. - -Due to this change, callback methods are now stored as weak references, which -means you must keep a reference to the associated object. Otherwise it may be -garbage collected. diff --git a/lib/matplotlib/widgets.py b/lib/matplotlib/widgets.py index 967ef30f1771..788865d98d86 100644 --- a/lib/matplotlib/widgets.py +++ b/lib/matplotlib/widgets.py @@ -222,7 +222,7 @@ def on_clicked(self, func): Returns a connection id, which can be used to disconnect the callback. """ - return self._observers.connect('clicked', func) + return self._observers.connect('clicked', lambda event: func(event)) def disconnect(self, cid): """Remove the callback function with connection id *cid*.""" @@ -483,7 +483,7 @@ def on_changed(self, func): int Connection id (which can be used to disconnect *func*) """ - return self._observers.connect('changed', func) + return self._observers.connect('changed', lambda val: func(val)) def disconnect(self, cid): """ @@ -646,7 +646,7 @@ def on_clicked(self, func): Returns a connection id, which can be used to disconnect the callback. """ - return self._observers.connect('clicked', func) + return self._observers.connect('clicked', lambda text: func(text)) def disconnect(self, cid): """Remove the observer with connection id *cid*.""" @@ -897,7 +897,7 @@ def on_text_change(self, func): A connection id is returned which can be used to disconnect. """ - return self._observers.connect('change', func) + return self._observers.connect('change', lambda text: func(text)) def on_submit(self, func): """ @@ -906,7 +906,7 @@ def on_submit(self, func): A connection id is returned which can be used to disconnect. """ - return self._observers.connect('submit', func) + return self._observers.connect('submit', lambda text: func(text)) def disconnect(self, cid): """Remove the observer with connection id *cid*."""