Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

feat: modify Tk backend managers to use ttk buttons and separators #28659

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
Loading
from
Open
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
44 changes: 34 additions & 10 deletions 44 lib/matplotlib/backends/_backend_tk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -685,7 +686,7 @@
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)
Expand Down Expand Up @@ -920,6 +921,29 @@
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
Comment on lines +927 to +931
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation appears outdated compared to the original _Button.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I see what you mean; the original _Button uses the standard tk.Button, which has a rather different implementation and API than ttk.Button, which is used here.


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'])

Check warning on line 942 in lib/matplotlib/backends/_backend_tk.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/backends/_backend_tk.py#L942

Added line #L942 was not covered by tests
else:
self._buttons[text].state(['!pressed'])

Check warning on line 944 in lib/matplotlib/backends/_backend_tk.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/backends/_backend_tk.py#L944

Added line #L944 was not covered by tests


def add_tooltip(widget, text):
tipwindow = None

Expand Down Expand Up @@ -957,11 +981,11 @@
@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(

Check warning on line 984 in lib/matplotlib/backends/_backend_tk.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/backends/_backend_tk.py#L984

Added line #L984 was not covered by tests
self._make_classic_style_pseudo_toolbar(), None, x0, y0, x1, y1)

def remove_rubberband(self):
NavigationToolbar2Tk.remove_rubberband(
NavigationToolbar2Ttk.remove_rubberband(

Check warning on line 988 in lib/matplotlib/backends/_backend_tk.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/backends/_backend_tk.py#L988

Added line #L988 was not covered by tests
self._make_classic_style_pseudo_toolbar())


Expand Down Expand Up @@ -992,7 +1016,7 @@
self._groups = {}

def _rescale(self):
return NavigationToolbar2Tk._rescale(self)
return NavigationToolbar2Ttk._rescale(self)

Check warning on line 1019 in lib/matplotlib/backends/_backend_tk.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/backends/_backend_tk.py#L1019

Added line #L1019 was not covered by tests

def add_toolitem(
self, name, group, position, image_file, description, toggle):
Expand All @@ -1002,8 +1026,8 @@
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)
Expand All @@ -1021,7 +1045,7 @@
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)
Expand All @@ -1046,14 +1070,14 @@
@backend_tools._register_tool_class(FigureCanvasTk)
class SaveFigureTk(backend_tools.SaveFigureBase):
def trigger(self, *args):
NavigationToolbar2Tk.save_figure(
NavigationToolbar2Ttk.save_figure(

Check warning on line 1073 in lib/matplotlib/backends/_backend_tk.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/backends/_backend_tk.py#L1073

Added line #L1073 was not covered by tests
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)

Check warning on line 1080 in lib/matplotlib/backends/_backend_tk.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/backends/_backend_tk.py#L1080

Added line #L1080 was not covered by tests


@backend_tools._register_tool_class(FigureCanvasTk)
Expand All @@ -1065,7 +1089,7 @@


Toolbar = ToolbarTk
FigureManagerTk._toolbar2_class = NavigationToolbar2Tk
FigureManagerTk._toolbar2_class = NavigationToolbar2Ttk
FigureManagerTk._toolmanager_toolbar_class = ToolbarTk


Expand Down
2 changes: 1 addition & 1 deletion 2 lib/matplotlib/backends/backend_tkagg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.