spacepaste

  1.  
  2. import sys
  3. import gc
  4. def test_matplotlib():
  5. from matplotlib import pylab
  6. pylab.plot(range(10))
  7. pylab.show()
  8. def test_matplotlib2():
  9. import Tkinter as Tk
  10. from matplotlib.figure import Figure
  11. from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
  12. from matplotlib.widgets import SubplotTool
  13. class Canvas(object):
  14. def draw(self, *args, **kwargs):
  15. pass
  16. class SubplotPars(object):
  17. left = 0
  18. bottom = 0
  19. right = 1
  20. top = 1
  21. wspace = 0
  22. hspace = 0
  23. class Dummy(object):
  24. subplotpars = SubplotPars()
  25. canvas = Canvas()
  26. def __init__(self):
  27. self.count = 0
  28. def subplots_adjust(self, *args, **kwargs):
  29. self.count += 1
  30. print('callback count', self.count)
  31. class MySubplotTool(SubplotTool):
  32. def __del__(self):
  33. print('in __del__')
  34. def create_sub(*args, **kwargs):
  35. dummy = Dummy()
  36. window = Tk.Tk()
  37. toolfig = Figure(figsize=(6,3))
  38. canvas = FigureCanvasTkAgg(toolfig, master=window)
  39. toolfig.subplots_adjust(top=0.9)
  40. tool = MySubplotTool(dummy, toolfig)
  41. canvas.show()
  42. canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)
  43. window = Tk.Tk()
  44. button = Tk.Button(master=window, text='press me', command=create_sub)
  45. button.pack()
  46. Tk.mainloop()
  47. print('1st gc.collect', gc.collect())
  48. print('2nd gc.collect', gc.collect())
  49. print('gc.garbage', gc.garbage)
  50. if __name__ == '__main__':
  51. opt = 0
  52. if len(sys.argv) > 1:
  53. opt = int(sys.argv[1])
  54. if opt == 0:
  55. test_matplotlib()
  56. elif opt == 1:
  57. test_matplotlib2()
  58.