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.

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

Example

#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=('Poppins bold', 25)).pack(pady= 20)

#Create a Button

Button(win, text= "Close", font=('Poppins bold', 16),
command=close_win).pack(pady=20)

win.mainloop()

Output

If we run the above code, it will produce the following output window −

Updated on: 04-Mar-2021

11K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements