Skip to content

Commit

Permalink
Handle FigureManager destroy internaly without pyplot.
Browse files Browse the repository at this point in the history
  • Loading branch information
OceanWolf committed Jun 4, 2015
1 parent 76ec60e commit 6637e60
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/matplotlib/_pylab_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def destroy(cls, num):
cls._activeQue.append(f)

del cls.figs[num]
manager.destroy()
manager.destroy() # Unneeded with MEP27 remove later
gc.collect(1)

@classmethod
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -2530,7 +2530,7 @@ def key_press_handler(event, canvas, toolbar=None):
if isinstance(canvas.manager, FigureManagerBase): # Using old figman.
Gcf.destroy_fig(canvas.figure)
else:
canvas.manager._destroy('window_destroy_event')
canvas.manager.destroy('window_destroy_event')

if toolbar is not None:
# home or reset mnemonic (default key 'h', 'home' and 'r')
Expand Down
24 changes: 15 additions & 9 deletions lib/matplotlib/backend_managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def __init__(self, figure, num):
self._backend = get_backend()
self._mainloop = self._backend.MainLoop()
self.window = self._backend.Window('Figure %d' % num)
self.window.mpl_connect('window_destroy_event', self._destroy)
self.window.mpl_connect('window_destroy_event', self.destroy)

self.canvas = self._backend.FigureCanvas(figure, manager=self)

Expand Down Expand Up @@ -122,23 +122,29 @@ def key_press(self, event):
"""
key_press_handler(event, self.canvas, self.canvas.toolbar)

def _destroy(self, event=None):
# Callback from the when the window wants to destroy itself
s = 'window_destroy_event'
event = FigureManagerEvent(s, self)
self._callbacks.process(s, event)

def destroy(self, *args):
"""Called to destroy this FigureManager, gets called by Gcf through
event magic.
"""Called to destroy this FigureManager.
"""

# Make sure we run this routine only once for the FigureManager
# This ensures the nasty __del__ fix below works.
if getattr(self, '_destroying', False):
return

self._destroying = True
self.canvas.destroy()
if self.toolbar:
self.toolbar.destroy()
self.window.destroy()

# Fix as for some reason we have extra references to this#
# i.e. ``del self._mainloop`` doesn't work
self._mainloop.__del__()

s = 'window_destroy_event'
event = FigureManagerEvent(s, self)
self._callbacks.process(s, event)

def show(self):
"""Shows the figure"""
self.window.show()
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/backend_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ def trigger(self, sender, event, data=None):
except:
pass
else:
manager._destroy('window_destroy_event')
manager.destroy('window_destroy_event')


class ToolEnableAllNavigation(ToolBase):
Expand Down

0 comments on commit 6637e60

Please sign in to comment.