Tkinter – Close window programmatically
In Tkinter, you can close a window programmatically by using Tk.destroy() method.
To close a window in Tkinter programmatically, call destroy method on the Tk class object which is our window.
window = tk.Tk() window.destroy()
In this tutorial, you will learn how to programmatically close the window in Tkinter, with examples.
1. Close window on Button click
In this example, we create a basic Tk window with a button in it. When user clicks on this button, we close the window using destroy() method.
import tkinter as tk # Create the main window window = tk.Tk() window.title("PythonExamples.org") window.geometry("300x200") def button_click(): window.destroy() # Create a button button = tk.Button(window, text="Close window", command=button_click) button.pack() # Run the application window.mainloop()
If you press Close window button, the window.destroy() method is called, and the window is closed.
In this Python Tkinter tutorial, we learned how to close the window programmatically using Tk.destroy() method, with examples.