How do I close a tkinter window?

PythonTkinterGUI-Programming

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 −

raja
Published on 04-Mar-2021 18:15:58
Advertisements