Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0375315

Browse files
authored
Merge pull request #18235 from tacaswell/tk_no_back_button
FIX: check we have a back button in tk toolbar before we touch it
2 parents efb93ba + f2b870e commit 0375315

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

lib/matplotlib/backends/_backend_tk.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -637,14 +637,15 @@ def save_figure(self, *args):
637637
tkinter.messagebox.showerror("Error saving file", str(e))
638638

639639
def set_history_buttons(self):
640-
if self._nav_stack._pos > 0:
641-
self._buttons['Back']['state'] = tk.NORMAL
642-
else:
643-
self._buttons['Back']['state'] = tk.DISABLED
644-
if self._nav_stack._pos < len(self._nav_stack._elements) - 1:
645-
self._buttons['Forward']['state'] = tk.NORMAL
646-
else:
647-
self._buttons['Forward']['state'] = tk.DISABLED
640+
state_map = {True: tk.NORMAL, False: tk.DISABLED}
641+
can_back = self._nav_stack._pos > 0
642+
can_forward = self._nav_stack._pos < len(self._nav_stack._elements) - 1
643+
644+
if "Back" in self._buttons:
645+
self._buttons['Back']['state'] = state_map[can_back]
646+
647+
if "Forward" in self._buttons:
648+
self._buttons['Forward']['state'] = state_map[can_forward]
648649

649650

650651
class ToolTip:

lib/matplotlib/tests/test_backend_tk.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,16 @@ def target():
100100
except subprocess.CalledProcessError:
101101
pytest.fail("Subprocess failed to test intended behavior")
102102
assert proc.stdout.count("success") == 1
103+
104+
105+
@pytest.mark.backend('TkAgg', skip_on_importerror=True)
106+
def test_missing_back_button():
107+
from matplotlib.backends.backend_tkagg import NavigationToolbar2Tk
108+
class Toolbar(NavigationToolbar2Tk):
109+
# only display the buttons we need
110+
toolitems = [t for t in NavigationToolbar2Tk.toolitems if
111+
t[0] in ('Home', 'Pan', 'Zoom')]
112+
113+
fig = plt.figure()
114+
# this should not raise
115+
Toolbar(fig.canvas, fig.canvas.manager.window)

0 commit comments

Comments
 (0)