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

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

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

matplotlib - users mailing list

Hello Matplotlib Users !

 

The new ToolManager works well, it is very easy to add new ToolButtons !

See snapshot and associated code.

 

Questions:

  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 ?

 

Thanks!

 

 

# 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")

    else:

        print(f"{state} selected")

 

    # zoom selected

    # pan selected

    # no tool selected

 

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

 

plt.show()


Schlumberger-Private


_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users