Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 256
Jamie Stivala Avatar asked Sep 16 '25 10:09

Jamie Stivala


1 Answers

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)
like image 130
Pythonista Avatar answered Sep 18 '25 09:09

Pythonista



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!