diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index 740c01226f7d..f4273bc03919 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -3459,6 +3459,10 @@ def remove_toolitem(self, name): backend-specific code to remove an element from the toolbar; it is called when `.ToolManager` emits a `tool_removed_event`. + Because some tools are present only on the `.ToolManager` but not on + the `ToolContainer`, this method must be a no-op when called on a tool + absent from the container. + .. warning:: This is part of the backend implementation and should not be called by end-users. They should instead call diff --git a/lib/matplotlib/backends/_backend_tk.py b/lib/matplotlib/backends/_backend_tk.py index 693499f4ca01..295f6c41372d 100644 --- a/lib/matplotlib/backends/_backend_tk.py +++ b/lib/matplotlib/backends/_backend_tk.py @@ -1011,9 +1011,8 @@ def toggle_toolitem(self, name, toggled): toolitem.deselect() def remove_toolitem(self, name): - for toolitem in self._toolitems[name]: + for toolitem in self._toolitems.pop(name, []): toolitem.pack_forget() - del self._toolitems[name] def set_message(self, s): self._message.set(s) diff --git a/lib/matplotlib/backends/backend_gtk3.py b/lib/matplotlib/backends/backend_gtk3.py index d6acd5547b85..49d34f5794e4 100644 --- a/lib/matplotlib/backends/backend_gtk3.py +++ b/lib/matplotlib/backends/backend_gtk3.py @@ -446,15 +446,10 @@ def toggle_toolitem(self, name, toggled): toolitem.handler_unblock(signal) def remove_toolitem(self, name): - if name not in self._toolitems: - self.toolmanager.message_event(f'{name} not in toolbar', self) - return - - for group in self._groups: - for toolitem, _signal in self._toolitems[name]: + for toolitem, _signal in self._toolitems.pop(name, []): + for group in self._groups: if toolitem in self._groups[group]: self._groups[group].remove(toolitem) - del self._toolitems[name] def _add_separator(self): sep = Gtk.Separator() diff --git a/lib/matplotlib/backends/backend_gtk4.py b/lib/matplotlib/backends/backend_gtk4.py index 7e73a4863212..256a8ec9e864 100644 --- a/lib/matplotlib/backends/backend_gtk4.py +++ b/lib/matplotlib/backends/backend_gtk4.py @@ -475,15 +475,10 @@ def toggle_toolitem(self, name, toggled): toolitem.handler_unblock(signal) def remove_toolitem(self, name): - if name not in self._toolitems: - self.toolmanager.message_event(f'{name} not in toolbar', self) - return - - for group in self._groups: - for toolitem, _signal in self._toolitems[name]: + for toolitem, _signal in self._toolitems.pop(name, []): + for group in self._groups: if toolitem in self._groups[group]: self._groups[group].remove(toolitem) - del self._toolitems[name] def _add_separator(self): sep = Gtk.Separator() diff --git a/lib/matplotlib/backends/backend_qt.py b/lib/matplotlib/backends/backend_qt.py index db593ae77ded..a93b37799971 100644 --- a/lib/matplotlib/backends/backend_qt.py +++ b/lib/matplotlib/backends/backend_qt.py @@ -1007,9 +1007,8 @@ def toggle_toolitem(self, name, toggled): button.toggled.connect(handler) def remove_toolitem(self, name): - for button, handler in self._toolitems[name]: + for button, handler in self._toolitems.pop(name, []): button.setParent(None) - del self._toolitems[name] def set_message(self, s): self.widgetForAction(self._message_action).setText(s) diff --git a/lib/matplotlib/backends/backend_wx.py b/lib/matplotlib/backends/backend_wx.py index 8064511ac28a..d39edf40f151 100644 --- a/lib/matplotlib/backends/backend_wx.py +++ b/lib/matplotlib/backends/backend_wx.py @@ -1257,9 +1257,8 @@ def toggle_toolitem(self, name, toggled): self.Refresh() def remove_toolitem(self, name): - for tool, handler in self._toolitems[name]: + for tool, handler in self._toolitems.pop(name, []): self.DeleteTool(tool.Id) - del self._toolitems[name] def set_message(self, s): self._label_text.SetLabel(s)