|
6 | 6 | import matplotlib as mpl
|
7 | 7 | from matplotlib import is_interactive
|
8 | 8 | from matplotlib.cbook import _api
|
9 |
| - |
| 9 | +from matplotlib.backend_bases import FigureCanvasBase |
10 | 10 | from ._manage_backend import current_backend_module
|
11 | 11 |
|
12 | 12 |
|
@@ -73,8 +73,26 @@ def promote_figure(fig, *, auto_draw=True):
|
73 | 73 | def _destroy(event):
|
74 | 74 |
|
75 | 75 | if event.key in mpl.rcParams["keymap.quit"]:
|
76 |
| - # TODO add notion of 'close' to managers |
77 |
| - event.canvas.manager.destroy() |
| 76 | + # grab the manager off the event |
| 77 | + mgr = event.canvas.manager |
| 78 | + if mgr is None: |
| 79 | + raise RuntimeError("Should never be here, please report a bug") |
| 80 | + fig = event.canvas.figure |
| 81 | + # remove this callback. Callbacks lives on the Figure so survive |
| 82 | + # the canvas being replaced. |
| 83 | + old_cid = getattr(mgr, "_destroy_cid", None) |
| 84 | + if old_cid is not None: |
| 85 | + fig.canvas.mpl_disconnect(old_cid) |
| 86 | + mgr._destroy_cid = None |
| 87 | + # close the window |
| 88 | + mgr.destroy() |
| 89 | + # disconnect the manager from the canvas |
| 90 | + fig.canvas.manager = None |
| 91 | + # reset the dpi |
| 92 | + fig.dpi = getattr(fig, "_original_dpi", fig.dpi) |
| 93 | + # Go back to "base" canvas |
| 94 | + # (this sets state on fig in the canvas init) |
| 95 | + FigureCanvasBase(fig) |
78 | 96 |
|
79 | 97 | manager._destroy_cid = fig.canvas.mpl_connect("key_press_event", _destroy)
|
80 | 98 |
|
|
0 commit comments