When the user presses a close Button
that I created, some tasks are performed before exiting. However, if the user clicks on the [X]
button in the top-right of the window to close the window, I cannot perform these tasks.
How can I override what happens when the user clicks [X]
button?
To handle the window close event in Tkinter with Python, we call the root. protocol method with the 'WM_DELETE_WINDOW' event. to call root. protocole with "WM_DELETE_WINDOW" to add a close window handler.
You can use master. withdraw() or iconify() if you do not want it to show. This should be unnecessary once the code after mainloop run without error.
mainloop() tells Python to run the Tkinter event loop. This method listens for events, such as button clicks or keypresses, and blocks any code that comes after it from running until you close the window where you called the method.
It sounds as if your save window should be modal.
If this is a basic save window, why are you reinventing the wheel? Tk
has a tkFileDialog
for this purpose.
If what you want is to override the default behaviour of destroying the window, you can simply do:
root.protocol('WM_DELETE_WINDOW', doSomething) # root is your root window def doSomething(): # check if saving # if not: root.destroy()
This way, you can intercept the destroy()
call when someone closes the window (by any means) and do what you like.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With