@@ -521,12 +521,23 @@ def set_cursor(self, cursor):
521
521
window .configure (cursor = cursord [cursor ])
522
522
window .update_idletasks ()
523
523
524
- def _Button (self , text , file , command , extension = '.gif' ):
525
- img_file = str (cbook ._get_data_path ('images' , file + extension ))
526
- im = tk .PhotoImage (master = self , file = img_file )
527
- b = tk .Button (
528
- master = self , text = text , padx = 2 , pady = 2 , image = im , command = command )
529
- b ._ntimage = im
524
+ def _Button (self , text , image_file , toggle , command ):
525
+ image = (tk .PhotoImage (master = self , file = image_file )
526
+ if image_file is not None else None )
527
+ if not toggle :
528
+ b = tk .Button (master = self , text = text , image = image , command = command )
529
+ else :
530
+ # There is a bug in tkinter included in some python 3.6 versions
531
+ # that without this variable, produces a "visual" toggling of
532
+ # other near checkbuttons
533
+ # https://bugs.python.org/issue29402
534
+ # https://bugs.python.org/issue25684
535
+ var = tk .IntVar ()
536
+ b = tk .Checkbutton (
537
+ master = self , text = text , image = image , command = command ,
538
+ indicatoron = False , variable = var )
539
+ b .var = var
540
+ b ._ntimage = image
530
541
b .pack (side = tk .LEFT )
531
542
return b
532
543
@@ -546,20 +557,42 @@ def _init_toolbar(self):
546
557
547
558
self .update () # Make axes menu
548
559
560
+ self ._buttons = {}
549
561
for text , tooltip_text , image_file , callback in self .toolitems :
550
562
if text is None :
551
563
# Add a spacer; return value is unused.
552
564
self ._Spacer ()
553
565
else :
554
- button = self ._Button (text = text , file = image_file ,
555
- command = getattr (self , callback ))
566
+ self ._buttons [text ] = button = self ._Button (
567
+ text ,
568
+ str (cbook ._get_data_path (f"images/{ image_file } .gif" )),
569
+ toggle = callback in ["zoom" , "pan" ],
570
+ command = getattr (self , callback ),
571
+ )
556
572
if tooltip_text is not None :
557
573
ToolTip .createToolTip (button , tooltip_text )
558
574
559
575
self .message = tk .StringVar (master = self )
560
576
self ._message_label = tk .Label (master = self , textvariable = self .message )
561
577
self ._message_label .pack (side = tk .RIGHT )
562
578
579
+ def _update_buttons_checked (self ):
580
+ for name , mode in [("Pan" , "PAN" ), ("Zoom" , "ZOOM" )]:
581
+ button = self ._buttons .get (name )
582
+ if button :
583
+ if self .mode .name == mode and not button .var .get ():
584
+ button .select ()
585
+ elif self .mode .name != mode and button .var .get ():
586
+ button .deselect ()
587
+
588
+ def pan (self , * args ):
589
+ super ().pan (* args )
590
+ self ._update_buttons_checked ()
591
+
592
+ def zoom (self , * args ):
593
+ super ().zoom (* args )
594
+ self ._update_buttons_checked ()
595
+
563
596
def configure_subplots (self ):
564
597
toolfig = Figure (figsize = (6 , 3 ))
565
598
window = tk .Toplevel ()
@@ -700,7 +733,8 @@ def __init__(self, toolmanager, window):
700
733
def add_toolitem (
701
734
self , name , group , position , image_file , description , toggle ):
702
735
frame = self ._get_groupframe (group )
703
- button = self ._Button (name , image_file , toggle , frame )
736
+ button = NavigationToolbar2Tk ._Button (self , name , image_file , toggle ,
737
+ lambda : self ._button_click (name ))
704
738
if description is not None :
705
739
ToolTip .createToolTip (button , description )
706
740
self ._toolitems .setdefault (name , [])
@@ -719,30 +753,6 @@ def _add_separator(self):
719
753
separator = tk .Frame (master = self , bd = 5 , width = 1 , bg = 'black' )
720
754
separator .pack (side = tk .LEFT , fill = tk .Y , padx = 2 )
721
755
722
- def _Button (self , text , image_file , toggle , frame ):
723
- if image_file is not None :
724
- im = tk .PhotoImage (master = self , file = image_file )
725
- else :
726
- im = None
727
-
728
- if not toggle :
729
- b = tk .Button (master = frame , text = text , padx = 2 , pady = 2 , image = im ,
730
- command = lambda : self ._button_click (text ))
731
- else :
732
- # There is a bug in tkinter included in some python 3.6 versions
733
- # that without this variable, produces a "visual" toggling of
734
- # other near checkbuttons
735
- # https://bugs.python.org/issue29402
736
- # https://bugs.python.org/issue25684
737
- var = tk .IntVar ()
738
- b = tk .Checkbutton (master = frame , text = text , padx = 2 , pady = 2 ,
739
- image = im , indicatoron = False ,
740
- command = lambda : self ._button_click (text ),
741
- variable = var )
742
- b ._ntimage = im
743
- b .pack (side = tk .LEFT )
744
- return b
745
-
746
756
def _button_click (self , name ):
747
757
self .trigger_tool (name )
748
758
0 commit comments