Skip to content

[Bug]: Tool Manager example broken #22088

Closed

Description

@StefRe

Bug summary

The Tool Manager example doesn't work.

Code for reproduction

python toolmanager_sgskip.py

Actual outcome

Traceback (most recent call last):
  File "C:\Programs\Python38\lib\site-packages\matplotlib\backends\backend_qt.py", line 930, in handler
    self.trigger_tool(name)
  File "C:\Programs\Python38\lib\site-packages\matplotlib\backend_bases.py", line 3391, in trigger_tool
    self.toolmanager.trigger_tool(name, sender=self)
  File "C:\Programs\Python38\lib\site-packages\matplotlib\backend_managers.py", line 379, in trigger_tool
    self._trigger_tool(name, sender, canvasevent, data)
  File "C:\Programs\Python38\lib\site-packages\matplotlib\backend_managers.py", line 394, in _trigger_tool
    tool.trigger(sender, canvasevent, data)
  File "C:\Programs\Python38\lib\site-packages\matplotlib\backends\backend_qt.py", line 970, in trigger
    NavigationToolbar2QT.configure_subplots(
  File "C:\Programs\Python38\lib\site-packages\matplotlib\backends\backend_qt.py", line 753, in configure_subplots
    if self._subplot_dialog is None:
AttributeError: 'types.SimpleNamespace' object has no attribute '_subplot_dialog'

Expected outcome

  • tools being added
  • no crash

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

Activity

  1. anntzer commented on Jan 3, 2022

    @anntzer
    Contributor

    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.

  2. added this to the v3.5.2 milestone on Jan 3, 2022
  3. StefRe commented on Jan 3, 2022

    @StefRe
    ContributorAuthor

    @anntzer Thanks for the super fast reply 👍 .

    When using the Tk backend (added plt.matplotlib.use('tkagg')) there's another issue: the "Configure subplots" dialog works at first but then freezes: open the dialog and move some sliders - after a short while the dialog freezes (python 3.8.2 64bit Windows). I guess I should open a separate issue for this, shouldn't I?

  4. anntzer commented on Jan 3, 2022

    @anntzer
    Contributor

    Please do (I don't have a solution for that off the top of my head).

  5. TheRikke commented on Mar 4, 2022

    @TheRikke

    I have the same issue and can confirm that the patch fixed it.

  6. added a commit that references this issue on Mar 31, 2022
    631e9f1
  7. added a commit that references this issue on Apr 19, 2026
    f7bd345
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions