Skip to content

Commit

Permalink
Merge pull request #18235 from tacaswell/tk_no_back_button
Browse files Browse the repository at this point in the history
FIX: check we have a back button in tk toolbar before we touch it
  • Loading branch information
QuLogic committed Aug 13, 2020
2 parents efb93ba + f2b870e commit 0375315
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
17 changes: 9 additions & 8 deletions lib/matplotlib/backends/_backend_tk.py
Original file line number Diff line number Diff line change
Expand Up @@ -637,14 +637,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:
Expand Down
13 changes: 13 additions & 0 deletions lib/matplotlib/tests/test_backend_tk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 0375315

Please sign in to comment.