How to exit from Python using a Tkinter Button?

To exit from Python using a Tkinter button, you can use either the destroy() or quit() method. Both methods close the application window, but they work differently under the hood.

Steps

  • Import the tkinter library and create an instance of tkinter frame.

  • Set the size of the frame using geometry method.

  • Define a function close() to close the window. Call the method win.destroy() inside close().

  • Next, create a button and call the close() function.

  • Finally, run the mainloop of the application window.

Method 1: Using destroy()

The destroy() method completely destroys the window and terminates the mainloop ?

# Import the library
from tkinter import *

# Create an instance of window
win = Tk()

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

# Title of the window
win.title("Click the Button to Close the Window")

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

# Create a Button to call close()
Button(win, text="Close the Window", font=("Calibri",14,"bold"), command=close).pack(pady=20)

win.mainloop()

Method 2: Using quit()

The quit() method stops the mainloop but keeps the window object in memory ?

# Import the library
from tkinter import *

# Create an instance of window
win = Tk()

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

# Title of the window
win.title("Click the Button to Close the Window")

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

# Create a Button to call close()
Button(win, text="Close the Window", font=("Calibri",14,"bold"), command=close).pack(pady=20)

win.mainloop()

Comparison

Method Action Memory Usage Best For
destroy() Destroys window completely Frees memory Single window applications
quit() Stops mainloop only Keeps objects in memory Multi-window applications

Conclusion

Use destroy() to completely close and clean up the application. Use quit() when you need to stop the mainloop but keep the window object for potential reuse.

Updated on: 2026-03-26T18:33:39+05:30

25K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements