Skip to content

Commit 3a2c8f3

Browse files
authored
Merge pull request matplotlib#17102 from anntzer/tkactions
Switch tk pan/zoom to use togglable buttons.
2 parents e841b51 + 9ce05e6 commit 3a2c8f3

File tree

1 file changed

+43
-33
lines changed

1 file changed

+43
-33
lines changed

lib/matplotlib/backends/_backend_tk.py

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -521,12 +521,23 @@ def set_cursor(self, cursor):
521521
window.configure(cursor=cursord[cursor])
522522
window.update_idletasks()
523523

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
530541
b.pack(side=tk.LEFT)
531542
return b
532543

@@ -546,20 +557,42 @@ def _init_toolbar(self):
546557

547558
self.update() # Make axes menu
548559

560+
self._buttons = {}
549561
for text, tooltip_text, image_file, callback in self.toolitems:
550562
if text is None:
551563
# Add a spacer; return value is unused.
552564
self._Spacer()
553565
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+
)
556572
if tooltip_text is not None:
557573
ToolTip.createToolTip(button, tooltip_text)
558574

559575
self.message = tk.StringVar(master=self)
560576
self._message_label = tk.Label(master=self, textvariable=self.message)
561577
self._message_label.pack(side=tk.RIGHT)
562578

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+
563596
def configure_subplots(self):
564597
toolfig = Figure(figsize=(6, 3))
565598
window = tk.Toplevel()
@@ -700,7 +733,8 @@ def __init__(self, toolmanager, window):
700733
def add_toolitem(
701734
self, name, group, position, image_file, description, toggle):
702735
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))
704738
if description is not None:
705739
ToolTip.createToolTip(button, description)
706740
self._toolitems.setdefault(name, [])
@@ -719,30 +753,6 @@ def _add_separator(self):
719753
separator = tk.Frame(master=self, bd=5, width=1, bg='black')
720754
separator.pack(side=tk.LEFT, fill=tk.Y, padx=2)
721755

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-
746756
def _button_click(self, name):
747757
self.trigger_tool(name)
748758

0 commit comments

Comments
 (0)