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
@@ -559,14 +560,8 @@ def set_cursor(self, cursor):
559
560
pass
560
561
561
562
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 )
568
563
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 )
570
565
else :
571
566
# There is a bug in tkinter included in some python 3.6 versions
572
567
# that without this variable, produces a "visual" toggling of
@@ -575,18 +570,25 @@ def _Button(self, text, image_file, toggle, command):
575
570
# https://bugs.python.org/issue25684
576
571
var = tk .IntVar (master = self )
577
572
b = tk .Checkbutton (
578
- master = self , text = text , image = image , command = command ,
573
+ master = self , text = text , command = command ,
579
574
indicatoron = False , variable = var )
580
575
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.
582
584
b .pack (side = tk .LEFT )
583
585
return b
584
586
585
587
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' )
590
592
return s
591
593
592
594
def save_figure (self , * args ):
0 commit comments