[Matplotlib-users] New Matplotlib’s toolmanager Works Well ! - How can we attach a callback and use it with tkinter ?

Carlos Baumann CBaumann at slb.com
Thu Sep 19 09:40:40 EDT 2019

Hello Matplotlib Users !

The new ToolManager works well, it is very easy to add new ToolButtons !
See snapshot and associated code.


  1.  Can we use this with Tkinter ? If yes can you send or point me to one example ?

(I know how to use a NavigationToolbar2Tk() with tkinter, but not a ToolbarTk() )

  1.  How do we attach a callback function to the new button ?


[cid:image001.png at 01D56EC3.1069B9A0]

# From the bottom of:
# https://stackoverflow.com/questions/20711148/ignore-matplotlib-cursor-widget-when-toolbar-widget-selected/20712813#20712813

# This works well, and the Toggle Buttons remain Toggled, depressed.

import matplotlib.pyplot as plt

# This is important
plt.rcParams['toolbar'] = 'toolmanager'

# # https://fossies.org/linux/matplotlib/lib/matplotlib/backends/_backend_tk.py
#   516         if matplotlib.rcParams['toolbar'] == 'toolbar2':
#   517             toolbar = NavigationToolbar2Tk(self.canvas, self.window)
#   518         elif matplotlib.rcParams['toolbar'] == 'toolmanager':
#   519             toolbar = ToolbarTk(self.toolmanager, self.window)

fig, ax = plt.subplots()

# print(fig.canvas.manager.toolbar._groups)
# {'navigation': <tkinter.Frame object .!toolbartk.!frame>, 'zoompan': <tkinter.Frame object .!toolbartk.!frame3>,
# 'io': <tkinter.Frame object .!toolbartk.!frame5>}

#  807     def add_toolitem(self, name, group, position, image_file, description, toggle):

fig.canvas.manager.toolbar.add_toolitem('newCB', 'newg', 0, None, 'new check-button', True) # creates new group and check-button !!!
fig.canvas.manager.toolbar.add_toolitem('newB', 'newg', 0, None, 'new button', False) # creates new group and button !!!
fig.canvas.manager.toolbar.add_toolitem('newCB', 'navigation', 0, None, 'new check-button', True) # creates button at the end of the first group

def on_click(evt):
    state = fig.canvas.manager.toolbar.toolmanager.active_toggle["default"]
    if state is None:
        print("no tool selected")
        print(f"{state} selected")

    # zoom selected
    # pan selected
    # no tool selected

cid = fig.canvas.mpl_connect('button_press_event', on_click)


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20190919/4e5657fa/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.png
Type: image/png
Size: 29172 bytes
Desc: image001.png
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20190919/4e5657fa/attachment-0001.png>

More information about the Matplotlib-users mailing list