Skip to content
Merged
Changes from 1 commit
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
Next Next commit
tk: Make toolbar sizes DPI-independent.
  • Loading branch information
QuLogic committed May 11, 2021
commit de4526ab4879a54e782a5f4c3e4ae97d2fb1611b
27 changes: 14 additions & 13 deletions lib/matplotlib/backends/_backend_tk.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import tkinter.messagebox

import numpy as np
from PIL import Image, ImageTk

import matplotlib as mpl
from matplotlib import _api, backend_tools, cbook, _c_internal_utils
Expand Down Expand Up @@ -572,14 +573,8 @@ def set_cursor(self, cursor):
pass

def _Button(self, text, image_file, toggle, command):
if tk.TkVersion >= 8.6:
PhotoImage = tk.PhotoImage
else:
from PIL.ImageTk import PhotoImage
image = (PhotoImage(master=self, file=image_file)
if image_file is not None else None)
if not toggle:
b = tk.Button(master=self, text=text, image=image, command=command)
b = tk.Button(master=self, text=text, command=command)
else:
# There is a bug in tkinter included in some python 3.6 versions
# that without this variable, produces a "visual" toggling of
Expand All @@ -588,18 +583,24 @@ def _Button(self, text, image_file, toggle, command):
# https://bugs.python.org/issue25684
var = tk.IntVar(master=self)
b = tk.Checkbutton(
master=self, text=text, image=image, command=command,
master=self, text=text, command=command,
indicatoron=False, variable=var)
b.var = var
b._ntimage = image
if image_file is not None:
size = b.winfo_pixels('18p')
with Image.open(image_file.replace('.png', '_large.png')
if size > 24 else image_file) as im:
image = ImageTk.PhotoImage(im.resize((size, size)),
master=self)
b.config(image=image, height='18p', width='18p')
b._ntimage = image # Prevent garbage collection.
b.pack(side=tk.LEFT)
return b

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

def save_figure(self, *args):
Expand Down