-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
[Bug]: Tool Manager example broken #22088
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
Comments
Good catch, this is likely a regression from #21681 (3.5.1, so labeling the fix as 3.5.2), something like diff --git i/lib/matplotlib/backends/backend_qt.py w/lib/matplotlib/backends/backend_qt.py
index c55937a019..1e0856adec 100644
--- i/lib/matplotlib/backends/backend_qt.py
+++ w/lib/matplotlib/backends/backend_qt.py
@@ -3,6 +3,7 @@ import operator
import os
import sys
import traceback
+from weakref import WeakKeyDictionary
import matplotlib as mpl
from matplotlib import _api, backend_tools, cbook
@@ -646,7 +647,6 @@ class NavigationToolbar2QT(NavigationToolbar2, QtWidgets.QToolBar):
self.coordinates = coordinates
self._actions = {} # mapping of toolitem method names to QActions.
- self._subplot_dialog = None
for text, tooltip_text, image_file, callback in self.toolitems:
if text is None:
@@ -755,14 +755,11 @@ class NavigationToolbar2QT(NavigationToolbar2, QtWidgets.QToolBar):
self.canvas.drawRectangle(None)
def configure_subplots(self):
- if self._subplot_dialog is None:
- self._subplot_dialog = SubplotToolQt(
- self.canvas.figure, self.canvas.parent())
- self.canvas.mpl_connect(
- "close_event", lambda e: self._subplot_dialog.reject())
- self._subplot_dialog.update_from_current_subplotpars()
- self._subplot_dialog.show()
- return self._subplot_dialog
+ subplot_dialog = SubplotToolQt(
+ self.canvas.figure, self.canvas.parent())
+ subplot_dialog.update_from_current_subplotpars()
+ subplot_dialog.show()
+ return subplot_dialog
def save_figure(self, *args):
filetypes = self.canvas.get_supported_filetypes_grouped()
@@ -805,8 +802,13 @@ class NavigationToolbar2QT(NavigationToolbar2, QtWidgets.QToolBar):
class SubplotToolQt(QtWidgets.QDialog):
- def __init__(self, targetfig, parent):
- super().__init__()
+ _keep_alive = WeakKeyDictionary()
+
+ def __new__(cls, targetfig, parent):
+ if targetfig in cls._keep_alive:
+ return cls._keep_alive[targetfig]
+ self = cls._keep_alive[targetfig] = super().__new__(cls, parent)
+ QtWidgets.QDialog.__init__(self)
self.setWindowIcon(QtGui.QIcon(
str(cbook._get_data_path("images/matplotlib.png"))))
self.setObjectName("SubplotTool")
@@ -848,6 +850,11 @@ class SubplotToolQt(QtWidgets.QDialog):
self._defaults = {}
self._export_values_dialog = None
self.update_from_current_subplotpars()
+ targetfig.canvas.mpl_connect("close_event", lambda e: self.reject())
+ return self
+
+ def __init__(self, targetfig, parent):
+ pass
def update_from_current_subplotpars(self):
self._defaults = {spinbox: getattr(self._figure.subplotpars, name) (moving instance tracking to SubplotToolQt itself) would be one way to fix this. |
@anntzer Thanks for the super fast reply 👍 . When using the Tk backend (added |
Please do (I don't have a solution for that off the top of my head). |
I have the same issue and can confirm that the patch fixed it. |
`configure_subplots` requires a) an object from which it can get the `canvas` property, and b) somewhere it can store its handle for re-use. The pseudo-toolbar that is used for most `Tool` classes is temporary and does not provide the latter. However, tools inherit from `ToolBase` which contains a `canvas` property and they exist for the lifetime of the toolbar, so can be used instead. Fixes matplotlib#22088
Bug summary
The Tool Manager example doesn't work.
Code for reproduction
Actual outcome
Tools are not added:
matplotlib/examples/user_interfaces/toolmanager_sgskip.py
Lines 77 to 78 in 0c1aa86
Clicking on "Configure subplots" icon
crashes the program:
Expected outcome
Additional information
No response
Operating system
No response
Matplotlib Version
3.5.1
Matplotlib Backend
No response
Python version
No response
Jupyter version
No response
Installation
No response
The text was updated successfully, but these errors were encountered: