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

Skip to content

Describe FigureManager #14106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 38 additions & 8 deletions lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -2463,20 +2463,53 @@ def button_press_handler(event, canvas, toolbar=None):


class NonGuiException(Exception):
"""Raised when trying show a figure in a non-GUI backend."""
pass


class FigureManagerBase:
"""
Helper class for pyplot mode, wraps everything up into a neat bundle.
A backend-independent abstraction of a figure container and controller.

The figure manager is used by pyplot to interact with the window in a
backend-independent way. It's an adapter for the real (GUI) framework that
represents the visual figure on screen.

GUI backends define from this class to translate common operations such
as *show* or *resize* to the GUI-specific code. Non-GUI backends do not
support these operations an can just use the base class.

This following basic operations are accessible:

**Window operations**

- `~.FigureManagerBase.show`
- `~.FigureManagerBase.destroy`
- `~.FigureManagerBase.full_screen_toggle`
- `~.FigureManagerBase.resize`
- `~.FigureManagerBase.get_window_title`
- `~.FigureManagerBase.set_window_title`

**Key and mouse button press handling**

The figure manager sets up default key and mouse button press handling by
hooking up the `.key_press_handler` to the matplotlib event system. This
ensures the same shortcuts and mouse actions across backends.

**Other operations**

Subclasses will have additional attributes and functions to access
additional functionality. This is of course backend-specific. For example,
most GUI backends have ``window`` and ``toolbar`` attributes that give
access to the native GUI widgets of the respective framework.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should either standardize or deprecate these attributes...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TL;DR I prefer to just document the current state within this PR. Any API changes should be discussed separately.

Can we standardize of deprecate either way? I think they are the only (easy) way to access the window and toolbar; so deprecating is not an option. OTOH, not all backends have these, e.g. FigureManagerMac does not have a window - well we could set them to None if not available.

Since the manager is actually a slightly broader concept, I'm not sure if this functionality should be in the base class - Well it's sort of already past that point having FigureManagerBase.set_window_title. One possibility would be to introduce a FigureWindowManager in between, that introduces this functionality. Also, when changing the API, I would like to think about how support a tabbed window #2194 within this framework.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, that was intended as a side discussion, not to block this PR.


Attributes
----------
canvas : :class:`FigureCanvasBase`
The backend-specific canvas instance
The backend-specific canvas instance.

num : int or str
The figure number
The figure number.

key_press_handler_id : int
The default key handler cid, when using the toolmanager.
Expand All @@ -2491,7 +2524,6 @@ class FigureManagerBase:

figure.canvas.mpl_disconnect(
figure.canvas.manager.button_press_handler_id)

"""
def __init__(self, canvas, num):
self.canvas = canvas
Expand Down Expand Up @@ -2533,7 +2565,7 @@ def full_screen_toggle(self):
pass

def resize(self, w, h):
""""For GUI backends, resize the window (in pixels)."""
"""For GUI backends, resize the window (in pixels)."""

def key_press(self, event):
"""
Expand All @@ -2544,9 +2576,7 @@ def key_press(self, event):
key_press_handler(event, self.canvas, self.canvas.toolbar)

def button_press(self, event):
"""
The default Matplotlib button actions for extra mouse buttons.
"""
"""The default Matplotlib button actions for extra mouse buttons."""
if rcParams['toolbar'] != 'toolmanager':
button_press_handler(event, self.canvas, self.canvas.toolbar)

Expand Down