: How do I handle the window close event in Tkinter ?

: How do I handle the window close event in Tkinter?

Asked on November 22, 2018 in tkinter.
Add Comment

  • 1 Answer(s)

    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.protocol(“WM_DELETE_WINDOW”, on_closing)

    Answered on February 5, 2019.
    Add Comment

  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.