10
10
import tkinter .messagebox
11
11
12
12
import numpy as np
13
+ from PIL import Image , ImageTk
13
14
14
15
import matplotlib as mpl
15
16
from matplotlib import _api , backend_tools , cbook , _c_internal_utils
@@ -572,14 +573,8 @@ def set_cursor(self, cursor):
572
573
pass
573
574
574
575
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 )
581
576
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 )
583
578
else :
584
579
# There is a bug in tkinter included in some python 3.6 versions
585
580
# that without this variable, produces a "visual" toggling of
@@ -588,18 +583,24 @@ def _Button(self, text, image_file, toggle, command):
588
583
# https://bugs.python.org/issue25684
589
584
var = tk .IntVar (master = self )
590
585
b = tk .Checkbutton (
591
- master = self , text = text , image = image , command = command ,
586
+ master = self , text = text , command = command ,
592
587
indicatoron = False , variable = var )
593
588
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.
595
597
b .pack (side = tk .LEFT )
596
598
return b
597
599
598
600
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' )
603
604
return s
604
605
605
606
def save_figure (self , * args ):
0 commit comments