From: Fernando P. <Fer...@co...> - 2005-01-31 23:10:47
|
Fernando Perez wrote: > Hi all, > > we've just run into a nasty problem with the TkAgg backend if close('all') is > called. In our setup, we use ipython+pylab with TkAgg because we also need > MayaVi to be active, and the GTK/WX backends block Tk windows. But if a call > is made to close('all'), matplotlib closes not only all of its own windows, > but also it destroys the MayaVi window in some very nasty way. The VTK > wrapper complains loudly about improper deletions, and afterwards running any > mayavi code is impossible (Tcl/Tk errors come from inside python itself). > > The problem is that matplotlib should not be touching any windows that don't > belong to it. I quickly wrote the following wrapper code to use here to work > around this bug: OK, a bit more info. It turns out that the crash happens whenever the _first_ figure window is deleted with a close(fignum) command. So in order to really block the problem, I had to create a dummy 'sentinel' window, numbered -666, for which close() is never called. It's OK to close this window via the window manager by clicking on its close button, but pylab.close() must NEVER be called on it. The current code looks like this: # Temporary hack around a matplotlib figure closing bug import matplotlib.pylab as mm try: mm.all_figures except AttributeError: mm.all_figures = [] # hack: sentinel to prevent pylab from destroying tk windows. NEVER make a -666 figure! mm.figure(-666) def figure(num=1,*args,**kw): """Wrapper around mm.figure which updates a global list of held figures.""" if num == -666: raise ValueError,'-666 is an internal sentinel, do not use for your figures' mm.figure(num,*args,**kw) mm.all_figures.append(num) def close(*args): """Close all open figures managed by our figure() wrapper.""" if len(args)==1 and args[0]=='all': print 'Closing figures:',mm.all_figures map(mm.close,mm.all_figures) mm.all_figures = [] else: mm.close(*args) Regards, f |