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

Skip to content

Commit 40cee1b

Browse files
committed
tk: Make toolbar sizes DPI-independent.
1 parent a61f208 commit 40cee1b

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

lib/matplotlib/backends/_backend_tk.py

Lines changed: 15 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
@@ -559,14 +560,8 @@ def set_cursor(self, cursor):
559560
pass
560561

561562
def _Button(self, text, image_file, toggle, command):
562-
if tk.TkVersion >= 8.6:
563-
PhotoImage = tk.PhotoImage
564-
else:
565-
from PIL.ImageTk import PhotoImage
566-
image = (PhotoImage(master=self, file=image_file)
567-
if image_file is not None else None)
568563
if not toggle:
569-
b = tk.Button(master=self, text=text, image=image, command=command)
564+
b = tk.Button(master=self, text=text, command=command)
570565
else:
571566
# There is a bug in tkinter included in some python 3.6 versions
572567
# that without this variable, produces a "visual" toggling of
@@ -575,18 +570,25 @@ def _Button(self, text, image_file, toggle, command):
575570
# https://bugs.python.org/issue25684
576571
var = tk.IntVar(master=self)
577572
b = tk.Checkbutton(
578-
master=self, text=text, image=image, command=command,
573+
master=self, text=text, command=command,
579574
indicatoron=False, variable=var)
580575
b.var = var
581-
b._ntimage = image
576+
if image_file is not None:
577+
size = b.winfo_pixels('24p')
578+
with Image.open(image_file.replace('.png', '_large.png')
579+
if size > 24 else image_file) as im:
580+
image = ImageTk.PhotoImage(im.resize((size, size)),
581+
master=self)
582+
b.config(image=image, height='24p', width='24p')
583+
b._ntimage = image # Prevent garbage collection.
582584
b.pack(side=tk.LEFT)
583585
return b
584586

585587
def _Spacer(self):
586-
# Buttons are 30px high. Make this 26px tall +2px padding to center it.
587-
s = tk.Frame(
588-
master=self, height=26, relief=tk.RIDGE, pady=2, bg="DarkGray")
589-
s.pack(side=tk.LEFT, padx=5)
588+
# Buttons are 24pt high. Make this 22pt tall +1pt padding to center it.
589+
s = tk.Frame(master=self, height='22p', pady='1p',
590+
relief=tk.RIDGE, bg='DarkGray')
591+
s.pack(side=tk.LEFT, padx='4p')
590592
return s
591593

592594
def save_figure(self, *args):

0 commit comments

Comments
 (0)
0