Skip to content

FIX: check we have a back button in tk toolbar before we touch it #18235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions lib/matplotlib/backends/_backend_tk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
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)