blob: 23ac103bdad041c8cdb34649b9a441666b2a9314 [file] [log] [blame]
Guido van Rossum35820f71994-10-07 09:55:26 +00001from Tkinter import *
2
Tim Peters182b5ac2004-07-18 06:16:08 +00003# This file shows how to trap the killing of a window
Guido van Rossum35820f71994-10-07 09:55:26 +00004# when the user uses window manager menus (typ. upper left hand corner
Tim Peters182b5ac2004-07-18 06:16:08 +00005# menu in the decoration border).
Guido van Rossum35820f71994-10-07 09:55:26 +00006
7
8### ******* this isn't really called -- read the comments
9def my_delete_callback():
Collin Winter6f2df4d2007-07-17 20:59:35 +000010 print("whoops -- tried to delete me!")
Guido van Rossum35820f71994-10-07 09:55:26 +000011
12class Test(Frame):
13 def deathHandler(self, event):
Collin Winter6f2df4d2007-07-17 20:59:35 +000014 print(self, "is now getting nuked. performing some save here....")
Guido van Rossum35820f71994-10-07 09:55:26 +000015
16 def createWidgets(self):
Tim Peters182b5ac2004-07-18 06:16:08 +000017 # a hello button
18 self.hi_there = Button(self, text='Hello')
19 self.hi_there.pack(side=LEFT)
Guido van Rossum35820f71994-10-07 09:55:26 +000020
21 def __init__(self, master=None):
Tim Peters182b5ac2004-07-18 06:16:08 +000022 Frame.__init__(self, master)
23 Pack.config(self)
24 self.createWidgets()
Guido van Rossum35820f71994-10-07 09:55:26 +000025
Tim Peters182b5ac2004-07-18 06:16:08 +000026 ###
27 ### PREVENT WM kills from happening
28 ###
Guido van Rossum35820f71994-10-07 09:55:26 +000029
Tim Peters182b5ac2004-07-18 06:16:08 +000030 # the docs would have you do this:
Guido van Rossum35820f71994-10-07 09:55:26 +000031
Tim Peters182b5ac2004-07-18 06:16:08 +000032# self.master.protocol("WM_DELETE_WINDOW", my_delete_callback)
Guido van Rossum35820f71994-10-07 09:55:26 +000033
Tim Peters182b5ac2004-07-18 06:16:08 +000034 # unfortunately, some window managers will not send this request to a window.
35 # the "protocol" function seems incapable of trapping these "aggressive" window kills.
36 # this line of code catches everything, tho. The window is deleted, but you have a chance
37 # of cleaning up first.
38 self.bind_all("<Destroy>", self.deathHandler)
Guido van Rossum35820f71994-10-07 09:55:26 +000039
40
41test = Test()
42test.mainloop()