Page 361 -
P. 361

terminate with extreme prejudice


           Capturing the protocol event isn’t enough


          Your code captures the protocol event and redefines its behavior. But, what
           about the behavior that used to execute by default?
           Take a look at this small example program, which redefines the close-box
           protocol to check with the user before actually destroying the window:


            from tkinter import *
            from tkinter.messagebox import askokcancel


            app = Tk()
            app.title("Capturing Events")
            app.geometry('250x100+200+200')

            def shutdown():
                if askokcancel(title = 'Are you sure?', message = 'Do you really want to quit?'):
                    app.destroy()


            app.protocol("WM_DELETE_WINDOW", shutdown)
            app.mainloop()















                                    Clicking on the close box runs the
                                    “shutdown()” function, which then
                                    displays an AskOkCancel dialog.
           Do this!
                                                                               If you click OK, the GUI is
                                                                                destroyed (which has the effect of
                                                                                closing the window).

                   Add the app.destroy() line of code to the end of your
                   shutdown() function and see if it makes any difference.


           326    Chapter 9
   356   357   358   359   360   361   362   363   364   365   366