/Demo/tkinter/matt/killing-window-w-wm.py

http://unladen-swallow.googlecode.com/ · Python · 42 lines · 16 code · 12 blank · 14 comment · 0 complexity · 5a8e7288dfe96b7c0deaab803a134ae2 MD5 · raw file

  1. from Tkinter import *
  2. # This file shows how to trap the killing of a window
  3. # when the user uses window manager menus (typ. upper left hand corner
  4. # menu in the decoration border).
  5. ### ******* this isn't really called -- read the comments
  6. def my_delete_callback():
  7. print "whoops -- tried to delete me!"
  8. class Test(Frame):
  9. def deathHandler(self, event):
  10. print self, "is now getting nuked. performing some save here...."
  11. def createWidgets(self):
  12. # a hello button
  13. self.hi_there = Button(self, text='Hello')
  14. self.hi_there.pack(side=LEFT)
  15. def __init__(self, master=None):
  16. Frame.__init__(self, master)
  17. Pack.config(self)
  18. self.createWidgets()
  19. ###
  20. ### PREVENT WM kills from happening
  21. ###
  22. # the docs would have you do this:
  23. # self.master.protocol("WM_DELETE_WINDOW", my_delete_callback)
  24. # unfortunately, some window managers will not send this request to a window.
  25. # the "protocol" function seems incapable of trapping these "aggressive" window kills.
  26. # this line of code catches everything, tho. The window is deleted, but you have a chance
  27. # of cleaning up first.
  28. self.bind_all("<Destroy>", self.deathHandler)
  29. test = Test()
  30. test.mainloop()