How do I handle the window close event in Tkinter?

209,367

Solution 1

Tkinter supports a mechanism called protocol handlers. Here, the term protocol refers to the interaction between the application and the window manager. The most commonly used protocol is called WM_DELETE_WINDOW, and is used to define what happens when the user explicitly closes a window using the window manager.

You can use the protocol method to install a handler for this protocol (the widget must be a Tk or Toplevel widget):

Here you have a concrete example:

import tkinter as tk
from tkinter import messagebox

root = tk.Tk()

def on_closing():
    if messagebox.askokcancel("Quit", "Do you want to quit?"):
        root.destroy()

root.protocol("WM_DELETE_WINDOW", on_closing)
root.mainloop()

Solution 2

Matt has shown one classic modification of the close button.
The other is to have the close button minimize the window.
You can reproduced this behavior by having the iconify method
be the protocol method's second argument.

Here's a working example, tested on Windows 7 & 10:

# Python 3
import tkinter
import tkinter.scrolledtext as scrolledtext

root = tkinter.Tk()
# make the top right close button minimize (iconify) the main window
root.protocol("WM_DELETE_WINDOW", root.iconify)
# make Esc exit the program
root.bind('<Escape>', lambda e: root.destroy())

# create a menu bar with an Exit command
menubar = tkinter.Menu(root)
filemenu = tkinter.Menu(menubar, tearoff=0)
filemenu.add_command(label="Exit", command=root.destroy)
menubar.add_cascade(label="File", menu=filemenu)
root.config(menu=menubar)

# create a Text widget with a Scrollbar attached
txt = scrolledtext.ScrolledText(root, undo=True)
txt['font'] = ('consolas', '12')
txt.pack(expand=True, fill='both')

root.mainloop()

In this example we give the user two new exit options:
the classic File → Exit, and also the Esc button.

Solution 3

Depending on the Tkinter activity, and especially when using Tkinter.after, stopping this activity with destroy() -- even by using protocol(), a button, etc. -- will disturb this activity ("while executing" error) rather than just terminate it. The best solution in almost every case is to use a flag. Here is a simple, silly example of how to use it (although I am certain that most of you don't need it! :)

from Tkinter import *

def close_window():
  global running
  running = False  # turn off while loop
  print( "Window closed")

root = Tk()
root.protocol("WM_DELETE_WINDOW", close_window)
cv = Canvas(root, width=200, height=200)
cv.pack()

running = True;
# This is an endless loop stopped only by setting 'running' to 'False'
while running: 
  for i in range(200): 
    if not running: 
        break
    cv.create_oval(i, i, i+1, i+1)
    root.update() 

This terminates graphics activity nicely. You only need to check running at the right place(s).

Solution 4

If you want to change what the x button does or make it so that you cannot close it at all try this.

yourwindow.protocol("WM_DELETE_WINDOW", whatever)

then defy what "whatever" means

def whatever():
    # Replace this with your own event for example:
    print("oi don't press that button")

You can also make it so that when you close that window you can call it back like this

yourwindow.withdraw() 

This hides the window but does not close it

yourwindow.deiconify()

This makes the window visible again

Solution 5

I'd like to thank the answer by Apostolos for bringing this to my attention. Here's a much more detailed example for Python 3 in the year 2019, with a clearer description and example code.


Beware of the fact that destroy() (or not having a custom window closing handler at all) will destroy the window and all of its running callbacks instantly when the user closes it.

This can be bad for you, depending on your current Tkinter activity, and especially when using tkinter.after (periodic callbacks). You might be using a callback which processes some data and writes to disk... in that case, you obviously want the data writing to finish without being abruptly killed.

The best solution for that is to use a flag. So when the user requests window closing, you mark that as a flag, and then react to it.

(Note: I normally design GUIs as nicely encapsulated classes and separate worker threads, and I definitely don't use "global" (I use class instance variables instead), but this is meant to be a simple, stripped-down example to demonstrate how Tk abruptly kills your periodic callbacks when the user closes the window...)

from tkinter import *
import time

# Try setting this to False and look at the printed numbers (1 to 10)
# during the work-loop, if you close the window while the periodic_call
# worker is busy working (printing). It will abruptly end the numbers,
# and kill the periodic callback! That's why you should design most
# applications with a safe closing callback as described in this demo.
safe_closing = True

# ---------

busy_processing = False
close_requested = False

def close_window():
    global close_requested
    close_requested = True
    print("User requested close at:", time.time(), "Was busy processing:", busy_processing)

root = Tk()
if safe_closing:
    root.protocol("WM_DELETE_WINDOW", close_window)
lbl = Label(root)
lbl.pack()

def periodic_call():
    global busy_processing

    if not close_requested:
        busy_processing = True
        for i in range(10):
            print((i+1), "of 10")
            time.sleep(0.2)
            lbl["text"] = str(time.time()) # Will error if force-closed.
            root.update() # Force redrawing since we change label multiple times in a row.
        busy_processing = False
        root.after(500, periodic_call)
    else:
        print("Destroying GUI at:", time.time())
        try: # "destroy()" can throw, so you should wrap it like this.
            root.destroy()
        except:
            # NOTE: In most code, you'll wanna force a close here via
            # "exit" if the window failed to destroy. Just ensure that
            # you have no code after your `mainloop()` call (at the
            # bottom of this file), since the exit call will cause the
            # process to terminate immediately without running any more
            # code. Of course, you should NEVER have code after your
            # `mainloop()` call in well-designed code anyway...
            # exit(0)
            pass

root.after_idle(periodic_call)
root.mainloop()

This code will show you that the WM_DELETE_WINDOW handler runs even while our custom periodic_call() is busy in the middle of work/loops!

We use some pretty exaggerated .after() values: 500 milliseconds. This is just meant to make it very easy for you to see the difference between closing while the periodic call is busy, or not... If you close while the numbers are updating, you will see that the WM_DELETE_WINDOW happened while your periodic call "was busy processing: True". If you close while the numbers are paused (meaning that the periodic callback isn't processing at that moment), you see that the close happened while it's "not busy".

In real-world usage, your .after() would use something like 30-100 milliseconds, to have a responsive GUI. This is just a demonstration to help you understand how to protect yourself against Tk's default "instantly interrupt all work when closing" behavior.

In summary: Make the WM_DELETE_WINDOW handler set a flag, and then check that flag periodically and manually .destroy() the window when it's safe (when your app is done with all work).

PS: You can also use WM_DELETE_WINDOW to ask the user if they REALLY want to close the window; and if they answer no, you don't set the flag. It's very simple. You just show a messagebox in your WM_DELETE_WINDOW and set the flag based on the user's answer.

Share:
209,367

Related videos on Youtube

Matt Gregory
Author by

Matt Gregory

Updated on April 17, 2022

Comments

  • Matt Gregory
    Matt Gregory about 2 years

    How do I handle the window close event (user clicking the 'X' button) in a Python Tkinter program?

  • Brian Jack
    Brian Jack over 12 years
    If you are using something like Twisted that maintains an event loop independently or Tkinter (eg: twisted's reactor object) make sure the outer main loop is stopped with whatever smenatics it provides for that purpose (eg: reactor.stop() for twisted)
  • IronManMark20
    IronManMark20 about 9 years
    On my Python 2.7 on Windows, Tkinter didn't have a submodule messagebox. I used import tkMessageBox as messagebox
  • Christian Dean
    Christian Dean about 7 years
    I think you should at make known that you copied this answer and code from someone/where else.
  • Matt Gregory
    Matt Gregory about 7 years
    I don't know, that's not the code that I originally posted.
  • Apostolos
    Apostolos about 6 years
    Doesn't work for me. It doesn't change classic Python's chaotic reaction to the interruption of graphics when one hard-closes the window (e.g. with Alt+F4).
  • Russell Smith
    Russell Smith over 4 years
    @MitchMcMabers: on the other hand, there have been virtually no significant changes to tkinter in a very long time.
  • user1318499
    user1318499 about 4 years
    Question is about the OS's X button for closing the window, not a regular button control.
  • Delrius Euphoria
    Delrius Euphoria over 3 years
    As said in the comments of other answers, "Question is about the OS's X button for closing the window, not a regular button control."
  • Ethan Furman
    Ethan Furman over 3 years
    Definitely interesting! However, I would immediately uninstall a program that didn't quit when I hit the close button.
  • Honest Abe
    Honest Abe over 3 years
    Yea, it violates the principle of least astonishment. I'm going to leave it, since it's still a valid answer to the question, and the example has some bonus educational value.
  • Ethan Furman
    Ethan Furman over 3 years
    Absolutely -- it got an upvote from me. :-)
  • Ath.Bar.
    Ath.Bar. almost 3 years
    This throws error if the window is closed by window.destroy(). It says: can't invoke "wm" command: application has been destroyed
  • martineau
    martineau over 2 years
    Setting the protocol this way only works for the window being closed by clicking on the close button the OS puts on the window (or an OS-specific keyboard command like alt-F4 is pressed, but not with many if not most of the other ways the tkinter mainloop() might be terminated (which also destroys the window).
  • Marinos TBH
    Marinos TBH almost 2 years
    Is there a way to make tkitner window, top level i mean it dosen't hide if you click in the background of the desktop or for example a browser or any other app ?