import sys import gc def test_matplotlib(): from matplotlib import pylab pylab.plot(range(10)) pylab.show() def test_matplotlib2(): import Tkinter as Tk from matplotlib.figure import Figure from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg from matplotlib.widgets import SubplotTool class Canvas(object): def draw(self, *args, **kwargs): pass class SubplotPars(object): left = 0 bottom = 0 right = 1 top = 1 wspace = 0 hspace = 0 class Dummy(object): subplotpars = SubplotPars() canvas = Canvas() def __init__(self): self.count = 0 def subplots_adjust(self, *args, **kwargs): self.count += 1 print('callback count', self.count) class MySubplotTool(SubplotTool): def __del__(self): print('in __del__') def create_sub(*args, **kwargs): dummy = Dummy() window = Tk.Tk() toolfig = Figure(figsize=(6,3)) canvas = FigureCanvasTkAgg(toolfig, master=window) toolfig.subplots_adjust(top=0.9) tool = MySubplotTool(dummy, toolfig) canvas.show() canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1) window = Tk.Tk() button = Tk.Button(master=window, text='press me', command=create_sub) button.pack() Tk.mainloop() print('1st gc.collect', gc.collect()) print('2nd gc.collect', gc.collect()) print('gc.garbage', gc.garbage) if __name__ == '__main__': opt = 0 if len(sys.argv) > 1: opt = int(sys.argv[1]) if opt == 0: test_matplotlib() elif opt == 1: test_matplotlib2()