8000 Always attach a manager attribute (possibly None) on canvas. by anntzer · Pull Request #15340 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Always attach a manager attribute (possibly None) on canvas. #15340

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 1 commit into from
Sep 25, 2019
Merged
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions doc/api/next_api_changes/2018-09-25-AL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
``FigureCanvasBase`` now always has a ``manager`` attribute, which may be None
``````````````````````````````````````````````````````````````````````````````

Previously, it did not necessarily have such an attribute. A check for
``hasattr(figure.canvas, "manager")`` should now be replaced by
``figure.canvas.manager is not None`` (or ``getattr(figure.canvas, "manager", None) is not None``
for back-compatibility).
3 changes: 2 additions & 1 deletion lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -1627,6 +1627,7 @@ def __init__(self, figure):
self._is_saving = False
figure.set_canvas(self)
self.figure = figure
self.manager = None
# a dictionary from event name to a dictionary that maps cid->func
self.callbacks = cbook.CallbackRegistry()
self.widgetlock = widgets.LockDraw()
Expand Down Expand Up @@ -2127,7 +2128,7 @@ def get_window_title(self):
Get the title text of the window containing the figure.
Return None if there is no window (e.g., a PS backend).
"""
if hasattr(self, "manager"):
if self.manager:
return self.manager.get_window_title()

def set_window_title(self, title):
Expand Down
29 changes: 11 additions & 18 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,25 +430,18 @@ def show(self, warn=True):
If ``True`` and we are not running headless (i.e. on Linux with an
unset DISPLAY), issue warning when called on a non-GUI backend.
"""
if self.canvas.manager is None:
raise AttributeError(
"Figure.show works only for figures managed by pyplot, "
"normally created by pyplot.figure()")
try:
manager = getattr(self.canvas, 'manager')
except AttributeError as err:
raise AttributeError("%s\n"
"Figure.show works only "
"for figures managed by pyplot, normally "
"created by pyplot.figure()." % err)

if manager is not None:
try:
manager.show()
return
except NonGuiException:
pass
if (backends._get_running_interactive_framework() != "headless"
and warn):
cbook._warn_external('Matplotlib is currently using %s, which is '
'a non-GUI backend, so cannot show the '
'figure.' % get_backend())
self.canvas.manager.show()
except NonGuiException:
if (backends._get_running_interactive_framework() != "headless"
and warn):
cbook._warn_external(
f"Matplotlib is currently using {get_backend()}, which is "
f"a non-GUI backend, so cannot show the figure.")

def _get_axes(self):
return self._axstack.as_list()
Expand Down
0