8000 tk: Make toolbar sizes DPI-independent. · matplotlib/matplotlib@de4526a · GitHub
[go: up one dir, main page]

Skip to content

Commit de4526a

Browse files
committed
tk: Make toolbar sizes DPI-independent.
1 parent 4013e50 commit de4526a

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

lib/matplotlib/backends/_backend_tk.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import tkinter.messagebox
1111

1212
import numpy as np
13+
from PIL import Image, ImageTk
1314

1415
import matplotlib as mpl
1516
from matplotlib import _api, backend_tools, cbook, _c_internal_utils
@@ -572,14 +573,8 @@ def set_cursor(self, cursor):
572573
pass
573574

574575
def _Button(self, text, image_file, toggle, command):
575-
if tk.TkVersion >= 8.6:
576-
PhotoImage = tk.PhotoImage
577-
else:
578-
from PIL.ImageTk import PhotoImage
579-
image = (PhotoImage(master=self, file=image_file)
580-
if image_file is not None else None)
581576
if not toggle:
582-
b = tk.Button(master=self, text=text, image=image, command=command)
577+
b = tk.Button(master=self, text=text, command=command)
583578
else:
584579
# There is a bug in tkinter included in some python 3.6 versions
585580
# that without this variable, produces a "visual" toggling of
@@ -588,18 +583,24 @@ def _Button(self, text, image_file, toggle, command):
588583
# https://bugs.python.org/issue25684
589584
var = tk.IntVar(master=self)
590585
b = tk.Checkbutton(
591-
master=self, text=text, image=image, command=command,
586+
master=self, text=text, command=command,
592587
indicatoron=False, variable=var)
593588
b.var = var
594-
b._ntimage = image
589+
if image_file is not None:
590+
size = b.winfo_pixels('18p')
591+
with Image.open(image_file.replace('.png', '_large.png')
592+
if size > 24 else image_file) as im:
593+
image = ImageTk.PhotoImage(im.resize((size, size)),
594+
master=self)
595+
b.config(image=image, height='18p', width='18p')
596+
b._ntimage = image # Prevent garbage collection.
595597
b.pack(side=tk.LEFT)
596598
return b
597599

598600
def _Spacer(self):
599-
# Buttons are 30px high. Make this 26px tall +2px padding to center it.
600-
s = tk.Frame(
601-
master=self, height=26, relief=tk.RIDGE, pady=2, bg="DarkGray")
602-
s.pack(side=tk.LEFT, padx=5)
601+
# Buttons are also 18pt high.
602+
s = tk.Frame(master=self, height='18p', relief=tk.RIDGE, bg='DarkGray')
603+
s.pack(side=tk.LEFT, padx='3p')
603604
return s
604605

605606
def save_figure(self, *args):

0 commit comments

Comments
 (0)
0