How do I close a tkinter window?

Creating an application using tkinter is easy but sometimes, it becomes difficult to close the window or the frame without closing it through the button on the title bar. In such cases, we can use the destroy() method to close the window programmatically.

As tkinter attributes are independent of each other, we can create a separate method to close the window using a button or other events.

Using destroy() Method

The destroy() method completely removes the window and all its child widgets from memory ?

# Import the library
from tkinter import *

# Create an instance of window
win = Tk()

# Set the geometry of the window
win.geometry("700x400")

# Define a function to close the window
def close_win():
    win.destroy()

# Create a label
Label(win, text="Click the button to Close the window",
      font=('Arial', 16)).pack(pady=20)

# Create a Button
Button(win, text="Close", font=('Arial', 12),
       command=close_win).pack(pady=20)

win.mainloop()

Alternative Methods

Using quit() Method

The quit() method exits the main loop but keeps the window in memory ?

from tkinter import *

root = Tk()
root.geometry("400x200")

def quit_app():
    root.quit()

Label(root, text="Using quit() method", font=('Arial', 14)).pack(pady=10)
Button(root, text="Quit", command=quit_app).pack(pady=10)

root.mainloop()

Using Protocol Handler

Handle the window close event when user clicks the X button ?

from tkinter import *
from tkinter import messagebox

root = Tk()
root.geometry("400x200")

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

root.protocol("WM_DELETE_WINDOW", on_closing)

Label(root, text="Try closing with X button", font=('Arial', 14)).pack(pady=20)
Button(root, text="Close", command=root.destroy).pack(pady=10)

root.mainloop()

Comparison

Method Effect Best For
destroy() Removes window from memory Completely closing application
quit() Exits mainloop only Temporary hide/pause
protocol() Custom close behavior Confirmation dialogs

Conclusion

Use destroy() to completely close tkinter windows and free memory. Use protocol("WM_DELETE_WINDOW") to customize the close behavior and add confirmation dialogs.

Updated on: 2026-03-25T16:49:15+05:30

14K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements