diff --git a/lib/matplotlib/backends/_backend_tk.py b/lib/matplotlib/backends/_backend_tk.py index 0bbff1379ffa..1be2f340cb92 100644 --- a/lib/matplotlib/backends/_backend_tk.py +++ b/lib/matplotlib/backends/_backend_tk.py @@ -11,6 +11,7 @@ import tkinter.font import tkinter.messagebox from tkinter.simpledialog import SimpleDialog +from tkinter import ttk import numpy as np from PIL import Image, ImageTk @@ -685,7 +686,7 @@ def _rescale(self): scale correctly to pixels. """ for widget in self.winfo_children(): - if isinstance(widget, (tk.Button, tk.Checkbutton)): + if isinstance(widget, (tk.Button, tk.Checkbutton, ttk.Button)): if hasattr(widget, '_image_file'): # Explicit class because ToolbarTk calls _rescale. NavigationToolbar2Tk._set_image_for_button(self, widget) @@ -920,6 +921,29 @@ def set_history_buttons(self): self._buttons['Forward']['state'] = state_map[can_forward] +class NavigationToolbar2Ttk(NavigationToolbar2Tk): + """Subclass of NavigationToolbar2Tk that implements ttk widgets""" + def _Button(self, text, image_file, toggle, command): + im = tk.PhotoImage(master=self, file=image_file) + b = ttk.Button(self, text=text, padding=(2, 2), image=im, command=command) + b._ntimage = im + b.pack(side='left') + return b + + def _Spacer(self): + s = ttk.Separator(master=self, orient='vertical') + s.pack(side='left', padx='3p', pady='5p', fill='y') + return s + + def _update_buttons_checked(self): + for text in ['Zoom', 'Pan']: + if text in self._buttons: + if self.mode.name == text.upper(): + self._buttons[text].state(['pressed']) + else: + self._buttons[text].state(['!pressed']) + + def add_tooltip(widget, text): tipwindow = None @@ -957,11 +981,11 @@ def hidetip(event): @backend_tools._register_tool_class(FigureCanvasTk) class RubberbandTk(backend_tools.RubberbandBase): def draw_rubberband(self, x0, y0, x1, y1): - NavigationToolbar2Tk.draw_rubberband( + NavigationToolbar2Ttk.draw_rubberband( self._make_classic_style_pseudo_toolbar(), None, x0, y0, x1, y1) def remove_rubberband(self): - NavigationToolbar2Tk.remove_rubberband( + NavigationToolbar2Ttk.remove_rubberband( self._make_classic_style_pseudo_toolbar()) @@ -992,7 +1016,7 @@ def __init__(self, toolmanager, window=None): self._groups = {} def _rescale(self): - return NavigationToolbar2Tk._rescale(self) + return NavigationToolbar2Ttk._rescale(self) def add_toolitem( self, name, group, position, image_file, description, toggle): @@ -1002,8 +1026,8 @@ def add_toolitem( before = None else: before = buttons[position] - button = NavigationToolbar2Tk._Button(frame, name, image_file, toggle, - lambda: self._button_click(name)) + button = NavigationToolbar2Ttk._Button(frame, name, image_file, toggle, + lambda: self._button_click(name)) button.pack_configure(before=before) if description is not None: add_tooltip(button, description) @@ -1021,7 +1045,7 @@ def _get_groupframe(self, group): return self._groups[group] def _add_separator(self): - return NavigationToolbar2Tk._Spacer(self) + return NavigationToolbar2Ttk._Spacer(self) def _button_click(self, name): self.trigger_tool(name) @@ -1046,14 +1070,14 @@ def set_message(self, s): @backend_tools._register_tool_class(FigureCanvasTk) class SaveFigureTk(backend_tools.SaveFigureBase): def trigger(self, *args): - NavigationToolbar2Tk.save_figure( + NavigationToolbar2Ttk.save_figure( self._make_classic_style_pseudo_toolbar()) @backend_tools._register_tool_class(FigureCanvasTk) class ConfigureSubplotsTk(backend_tools.ConfigureSubplotsBase): def trigger(self, *args): - NavigationToolbar2Tk.configure_subplots(self) + NavigationToolbar2Ttk.configure_subplots(self) @backend_tools._register_tool_class(FigureCanvasTk) @@ -1065,7 +1089,7 @@ def trigger(self, *args): Toolbar = ToolbarTk -FigureManagerTk._toolbar2_class = NavigationToolbar2Tk +FigureManagerTk._toolbar2_class = NavigationToolbar2Ttk FigureManagerTk._toolmanager_toolbar_class = ToolbarTk diff --git a/lib/matplotlib/backends/backend_tkagg.py b/lib/matplotlib/backends/backend_tkagg.py index f95b6011eadf..6356cee7785e 100644 --- a/lib/matplotlib/backends/backend_tkagg.py +++ b/lib/matplotlib/backends/backend_tkagg.py @@ -2,7 +2,7 @@ from .backend_agg import FigureCanvasAgg from ._backend_tk import _BackendTk, FigureCanvasTk from ._backend_tk import ( # noqa: F401 # pylint: disable=W0611 - FigureManagerTk, NavigationToolbar2Tk) + FigureManagerTk, NavigationToolbar2Tk, NavigationToolbar2Ttk) class FigureCanvasTkAgg(FigureCanvasAgg, FigureCanvasTk):