Skip to content

Commit b4f9dde

Browse files
committed
DOC: add docstring to close
1 parent 86950c2 commit b4f9dde

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

mpl_gui/__init__.py

+36
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,37 @@ def close_all(self):
252252
self.close(fig)
253253

254254
def close(self, val):
255+
"""
256+
Close (meaning destroy the UI) and forget a managed Figure.
257+
258+
This will do two things:
259+
260+
- start the destruction process of an UI (the event loop may need to
261+
run to complete this process and if the user is holding hard
262+
references to any of the UI elements they may remain alive).
263+
- Remove the `Figure` from this Registry.
264+
265+
We will no longer have any hard references to the Figure, but if
266+
the user does the `Figure` (and its components) will not be garbage
267+
collected. Due to the circular references in Matplotlib these
268+
objects may not be collected until the full cyclic garbage collection
269+
runs.
270+
271+
If the user still has a reference to the `Figure` they can re-show the
272+
figure via `show`, but the `FigureRegistry` will not be aware of it.
273+
274+
Parameters
275+
----------
276+
val : 'all' or int or str or Figure
277+
278+
- The special case of 'all' closes all open Figures
279+
- If any other string is passed, it is interpreted as a key in
280+
`by_label` and that Figure is closed
281+
- If an integer it is interpreted as a key in `by_number` and that
282+
Figure is closed
283+
- If it is a `Figure` instance, then that figure is closed
284+
285+
"""
255286
if val == "all":
256287
return self.close_all()
257288
# or do we want to close _all_ of the figures with a given label / number?
@@ -261,12 +292,17 @@ def close(self, val):
261292
fig = self.by_number[val]
262293
else:
263294
fig = val
295+
if fig not in self.figures:
296+
raise ValueError(
297+
"Trying to close a figure not associated with this Registry."
298+
)
264299
if fig.canvas.manager is not None:
265300
fig.canvas.manager.destroy()
266301
# disconnect figure from canvas
267302
fig.canvas.figure = None
268303
# disconnect canvas from figure
269304
_FigureCanvasBase(figure=fig)
305+
assert fig.canvas.manager is None
270306
if fig in self.figures:
271307
self.figures.remove(fig)
272308

0 commit comments

Comments
 (0)