diff --git a/lib/matplotlib/backends/_backend_tk.py b/lib/matplotlib/backends/_backend_tk.py index f8516584187c..0017a50b9a2c 100644 --- a/lib/matplotlib/backends/_backend_tk.py +++ b/lib/matplotlib/backends/_backend_tk.py @@ -635,14 +635,15 @@ def save_figure(self, *args): tkinter.messagebox.showerror("Error saving file", str(e)) def set_history_buttons(self): - if self._nav_stack._pos > 0: - self._buttons['Back']['state'] = tk.NORMAL - else: - self._buttons['Back']['state'] = tk.DISABLED - if self._nav_stack._pos < len(self._nav_stack._elements) - 1: - self._buttons['Forward']['state'] = tk.NORMAL - else: - self._buttons['Forward']['state'] = tk.DISABLED + state_map = {True: tk.NORMAL, False: tk.DISABLED} + can_back = self._nav_stack._pos > 0 + can_forward = self._nav_stack._pos < len(self._nav_stack._elements) - 1 + + if "Back" in self._buttons: + self._buttons['Back']['state'] = state_map[can_back] + + if "Forward" in self._buttons: + self._buttons['Forward']['state'] = state_map[can_forward] class ToolTip: diff --git a/lib/matplotlib/tests/test_backend_tk.py b/lib/matplotlib/tests/test_backend_tk.py index b56b25a717fb..70eed6d75a81 100644 --- a/lib/matplotlib/tests/test_backend_tk.py +++ b/lib/matplotlib/tests/test_backend_tk.py @@ -100,3 +100,16 @@ def target(): except subprocess.CalledProcessError: pytest.fail("Subprocess failed to test intended behavior") assert proc.stdout.count("success") == 1 + + +@pytest.mark.backend('TkAgg', skip_on_importerror=True) +def test_missing_back_button(): + from matplotlib.backends.backend_tkagg import NavigationToolbar2Tk + class Toolbar(NavigationToolbar2Tk): + # only display the buttons we need + toolitems = [t for t in NavigationToolbar2Tk.toolitems if + t[0] in ('Home', 'Pan', 'Zoom')] + + fig = plt.figure() + # this should not raise + Toolbar(fig.canvas, fig.canvas.manager.window) pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy