Jamie Stivala Jamie Stivala - 5 months ago 9
Python Question

Detect when the "x" or close button is pressed

How would you know when the user pressed the "x" button on the close button in a python tkinter program and do an event?

Answer

You can override the closing protocol.

def on_close():

     #custom close options, here's one example:

     close = messagebox.askokcancel("Close", "Would you like to close the program?")
     if close:
          root.destroy()

root.protocol("WM_DELETE_WINDOW",  on_close)