Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding Tkinter "X" button control (the button that close the window) [duplicate]

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?

like image 715
erkangur Avatar asked Jul 20 '10 23:07

erkangur


People also ask

How do I handle the window close event in Tkinter?

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.

How do I close a Tkinter window without destroying it?

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.

What command forces your application to loop until it is closed window Tkinter TK ())?

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.


1 Answers

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.

like image 164
Nick Presta Avatar answered Sep 20 '22 04:09

Nick Presta