@@ -533,11 +533,26 @@ def set_cursor(self, cursor):
533
533
window .configure (cursor = cursord [cursor ])
534
534
window .update_idletasks ()
535
535
536
- def _Button (self ,text ,file ,command ,extension = '.gif' ):
537
- img_file = str (cbook ._get_data_path ('images' ,file + extension ))
538
- im = tk .PhotoImage (master = self ,file = img_file )
539
- b = tk .Button (
540
- master = self ,text = text ,padx = 2 ,pady = 2 ,image = im ,command = command )
536
+ def _Button (self ,text ,image_file ,toggle ,command ):
537
+ if image_file is not None :
538
+ im = tk .PhotoImage (master = self ,file = image_file )
539
+ else :
540
+ im = None
541
+ if not toggle :
542
+ b = tk .Button (master = self ,text = text ,padx = 2 ,pady = 2 ,image = im ,
543
+ command = command )
544
+ else :
545
+ # There is a bug in tkinter included in some python 3.6 versions
546
+ # that without this variable, produces a "visual" toggling of
547
+ # other near checkbuttons
548
+ # https://bugs.python.org/issue29402
549
+ # https://bugs.python.org/issue25684
550
+ var = tk .IntVar ()
551
+ b = tk .Checkbutton (master = self ,text = text ,padx = 2 ,pady = 2 ,
552
+ image = im ,indicatoron = False ,
553
+ command = command ,
554
+ variable = var )
555
+ b .var = var
541
556
b ._ntimage = im
542
557
b .pack (side = tk .LEFT )
543
558
return b
@@ -558,20 +573,42 @@ def _init_toolbar(self):
558
573
559
574
self .update ()# Make axes menu
560
575
576
+ self ._buttons = {}
561
577
for text ,tooltip_text ,image_file ,callback in self .toolitems :
562
578
if text is None :
563
579
# Add a spacer; return value is unused.
564
580
self ._Spacer ()
565
581
else :
566
- button = self ._Button (text = text ,file = image_file ,
567
- command = getattr (self ,callback ))
582
+ self ._buttons [text ]= button = self ._Button (
583
+ text ,
584
+ str (cbook ._get_data_path (f"images/{ image_file } .gif" )),
585
+ toggle = callback in ["zoom" ,"pan" ],
586
+ command = getattr (self ,callback ),
587
+ )
568
588
if tooltip_text is not None :
569
589
ToolTip .createToolTip (button ,tooltip_text )
570
590
571
591
self .message = tk .StringVar (master = self )
572
592
self ._message_label = tk .Label (master = self ,textvariable = self .message )
573
593
self ._message_label .pack (side = tk .RIGHT )
574
594
595
+ def _update_buttons_checked (self ):
596
+ for name ,active in [("Pan" ,"PAN" ), ("Zoom" ,"ZOOM" )]:
597
+ button = self ._buttons .get (name )
598
+ if button :
599
+ if self ._active == active and not button .var .get ():
600
+ button .select ()
601
+ elif self ._active != active and button .var .get ():
602
+ button .deselect ()
603
+
604
+ def pan (self ,* args ):
605
+ super ().pan (* args )
606
+ self ._update_buttons_checked ()
607
+
608
+ def zoom (self ,* args ):
609
+ super ().zoom (* args )
610
+ self ._update_buttons_checked ()
611
+
575
612
def configure_subplots (self ):
576
613
toolfig = Figure (figsize = (6 ,3 ))
577
614
window = tk .Toplevel ()
@@ -717,7 +754,8 @@ def __init__(self, toolmanager, window):
717
754
def add_toolitem (
718
755
self ,name ,group ,position ,image_file ,description ,toggle ):
719
756
frame = self ._get_groupframe (group )
720
- button = self ._Button (name ,image_file ,toggle ,frame )
757
+ button = NavigationToolbar2Tk ._Button (self ,name ,image_file ,toggle ,
758
+ lambda :self ._button_click (name ))
721
759
if description is not None :
722
760
ToolTip .createToolTip (button ,description )
723
761
self ._toolitems .setdefault (name , [])
@@ -736,30 +774,6 @@ def _add_separator(self):
736
774
separator = tk .Frame (master = self ,bd = 5 ,width = 1 ,bg = 'black' )
737
775
separator .pack (side = tk .LEFT ,fill = tk .Y ,padx = 2 )
738
776
739
- def _Button (self ,text ,image_file ,toggle ,frame ):
740
- if image_file is not None :
741
- im = tk .PhotoImage (master = self ,file = image_file )
742
- else :
743
- im = None
744
-
745
- if not toggle :
746
- b = tk .Button (master = frame ,text = text ,padx = 2 ,pady = 2 ,image = im ,
747
- command = lambda :self ._button_click (text ))
748
- else :
749
- # There is a bug in tkinter included in some python 3.6 versions
750
- # that without this variable, produces a "visual" toggling of
751
- # other near checkbuttons
752
- # https://bugs.python.org/issue29402
753
- # https://bugs.python.org/issue25684
754
- var = tk .IntVar ()
755
- b = tk .Checkbutton (master = frame ,text = text ,padx = 2 ,pady = 2 ,
756
- image = im ,indicatoron = False ,
757
- command = lambda :self ._button_click (text ),
758
- variable = var )
759
- b ._ntimage = im
760
- b .pack (side = tk .LEFT )
761
- return b
762
-
763
777
def _button_click (self ,name ):
764
778
self .trigger_tool (name )
765
779