Under some circumstances, you might want to perform an action before the user closes a top-level window, for instance, to prevent you losing unsaved work. Tkinter allows you to intercept this type of event to conditionally destroy the window.
Handling window deletion
Getting ready
We will reuse the App class from the preceding recipe, and we will modify the Window class so that it shows a dialog to confirm closing the window:
How to do it...
In Tkinter, we can detect when a window is about to be closed by registering a handler function for the WM_DELETE_WINDOW protocol...