|
| 1 | +ToolManager |
| 2 | +----------- |
| 3 | + |
| 4 | +Federico Ariza wrote the new `matplotlib.backend_managers.ToolManager` that comes as replacement for `NavigationToolbar2` |
| 5 | + |
| 6 | +`ToolManager` offers a new way of looking at the user interactions with the figures. |
| 7 | +Before we had the `NavigationToolbar2` with its own tools like `zoom/pan/home/save/...` and also we had the shortcuts like |
| 8 | +`yscale/grid/quit/....` |
| 9 | +`Toolmanager` relocate all those actions as `Tools` (located in `matplotlib.backend_tools`), and defines a way to `access/trigger/reconfigure` them. |
| 10 | + |
| 11 | +The `Toolbars` are replaced for `ToolContainers` that are just GUI interfaces to `trigger` the tools. But don't worry the default backends include a `ToolContainer` called `toolbar` |
| 12 | + |
| 13 | + |
| 14 | +.. note:: |
| 15 | + For the moment the `ToolManager` is working only with `GTK3` and `Tk` backends. |
| 16 | + Make sure you are using one of those. |
| 17 | + Port for the rest of the backends is comming soon. |
| 18 | + |
| 19 | + To activate the `ToolManager` include the following at the top of your file: |
| 20 | + |
| 21 | + >>> matplotlib.rcParams['toolbar'] = 'toolmanager' |
| 22 | + |
| 23 | + |
| 24 | +Interact with the ToolContainer |
| 25 | +``````````````````````````````` |
| 26 | + |
| 27 | +The most important feature is the ability to easily reconfigure the ToolContainer (aka toolbar). |
| 28 | +For example, if we want to remove the "forward" button we would just do. |
| 29 | + |
| 30 | + >>> fig.canvas.manager.toolmanager.remove_tool('forward') |
| 31 | + |
| 32 | +Now if you want to programmatically trigger the "home" button |
| 33 | + |
| 34 | + >>> fig.canvas.manager.toolmanager.trigger_tool('home') |
| 35 | + |
| 36 | + |
| 37 | +New Tools |
| 38 | +````````` |
| 39 | + |
| 40 | +It is possible to add new tools to the ToolManager |
| 41 | + |
| 42 | +A very simple tool that prints "You're awesome" would be:: |
| 43 | + |
| 44 | + from matplotlib.backend_tools import ToolBase |
| 45 | + class AwesomeTool(ToolBase): |
| 46 | + def trigger(self, *args, **kwargs): |
| 47 | + print("You're awesome") |
| 48 | + |
| 49 | + |
| 50 | +To add this tool to `ToolManager` |
| 51 | + |
| 52 | + >>> fig.canvas.manager.toolmanager.add_tool('Awesome', AwesomeTool) |
| 53 | + |
| 54 | +If we want to add a shortcut ("d") for the tool |
| 55 | + |
| 56 | + >>> fig.canvas.manager.toolmanager.update_keymap('Awesome', 'd') |
| 57 | + |
| 58 | + |
| 59 | +To add it to the toolbar inside the group 'foo' |
| 60 | + |
| 61 | + >>> fig.canvas.manager.toolbar.add_tool('Awesome', 'foo') |
| 62 | + |
| 63 | + |
| 64 | +There is a second class of tools, "Toggleable Tools", this are almost the same as our basic tools, just that belong to a group, and are mutually exclusive inside that group. |
| 65 | +For tools derived from `ToolToggleBase` there are two basic methods `enable` and `disable` that are called automatically whenever it is toggled. |
| 66 | + |
| 67 | + |
| 68 | +A full example is located in :ref:`user_interfaces-toolmanager` |
0 commit comments