Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 48a6971

Browse files
committed
tkagg updated for toolmanager and tool groups
1 parent a7640ef commit 48a6971

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

examples/user_interfaces/toolmanager.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ class allows to:
88

99
from __future__ import print_function
1010
import matplotlib
11-
# matplotlib.use('GTK3Cairo')
12-
matplotlib.use('Tkagg')
11+
matplotlib.use('GTK3Cairo')
1312
matplotlib.rcParams['toolbar'] = 'toolmanager'
1413
import matplotlib.pyplot as plt
1514
from matplotlib.backend_tools import ToolBase
@@ -63,7 +62,7 @@ def trigger(self, *args, **kwargs):
6362

6463
# Add the custom tools that we created
6564
fig.canvas.manager.toolmanager.add_tool('List', ListTools)
66-
# fig.canvas.manager.toolmanager.add_tool('copy', CopyToolGTK3)
65+
fig.canvas.manager.toolmanager.add_tool('copy', CopyToolGTK3)
6766

6867
# Add an existing tool to new group `foo`.
6968
# It can be added as many times as we want

lib/matplotlib/backends/backend_tkagg.py

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -939,25 +939,41 @@ def __init__(self, navigation, window):
939939
borderwidth=2)
940940
self._toolitems = {}
941941
self.pack(side=Tk.TOP, fill=Tk.X)
942+
self._groups = {}
942943

943944
def add_toolitem(self, name, group, position, image_file, description,
944945
toggle):
945-
button = self._Button(name, image_file, toggle)
946+
frame = self._get_groupframe(group)
947+
button = self._Button(name, image_file, toggle, frame)
946948
if description is not None:
947949
ToolTip.createToolTip(button, description)
948-
self._toolitems[name] = button
949-
950-
def _Button(self, text, image_file, toggle):
950+
self._toolitems.setdefault(name, [])
951+
self._toolitems[name].append(button)
952+
953+
def _get_groupframe(self, group):
954+
if group not in self._groups:
955+
if self._groups:
956+
self._add_separator()
957+
frame = Tk.Frame(master=self, borderwidth=0)
958+
frame.pack(side=Tk.LEFT, fill=Tk.Y)
959+
self._groups[group] = frame
960+
return self._groups[group]
961+
962+
def _add_separator(self):
963+
separator = Tk.Frame(master=self, bd=5, width=1, bg='black')
964+
separator.pack(side=Tk.LEFT, fill=Tk.Y, padx=2)
965+
966+
def _Button(self, text, image_file, toggle, frame):
951967
if image_file is not None:
952968
im = Tk.PhotoImage(master=self, file=image_file)
953969
else:
954970
im = None
955971

956972
if not toggle:
957-
b = Tk.Button(master=self, text=text, padx=2, pady=2, image=im,
973+
b = Tk.Button(master=frame, text=text, padx=2, pady=2, image=im,
958974
command=lambda: self._button_click(text))
959975
else:
960-
b = Tk.Checkbutton(master=self, text=text, padx=2, pady=2,
976+
b = Tk.Checkbutton(master=frame, text=text, padx=2, pady=2,
961977
image=im, indicatoron=False,
962978
command=lambda: self._button_click(text))
963979
b._ntimage = im
@@ -970,13 +986,15 @@ def _button_click(self, name):
970986
def toggle_toolitem(self, name, toggled):
971987
if name not in self._toolitems:
972988
return
973-
if toggled:
974-
self._toolitems[name].select()
975-
else:
976-
self._toolitems[name].deselect()
989+
for toolitem in self._toolitems[name]:
990+
if toggled:
991+
toolitem.select()
992+
else:
993+
toolitem.deselect()
977994

978995
def remove_toolitem(self, name):
979-
self._toolitems[name].pack_forget()
996+
for toolitem in self._toolitems[name]:
997+
toolitem.pack_forget()
980998
del self._toolitems[name]
981999

9821000

0 commit comments

Comments
 (0)