Function to close the window in Tkinter

TkinterPythonServer Side ProgrammingProgramming

Whenever we run a tkinter application, it displays a GUI-based window that would have widgets, frames, and other elements. Let us suppose we want to close our application with a function. The destroy() method in Python tkinter is used to terminate the normal execution of the application after the mainloop function.

Example

In this example, will create a button object which triggers to close the application.

#Import the tkinter library
from tkinter import *

#Create an instance of tkinter frame
win = Tk()

#Set the geometry
win.geometry("650x250")

#Define a function
def close_app():
   win.destroy()

#Create a text Label
Label(win, text= "Click to close the Window", font= ('Helvetica bold', 14)).pack(pady=20)

#Create a Button for closing the window
button= Button(win, text= "Close", command= close_app, font=('Helvetica bold', 10))
button.pack(pady=10)
win.mainloop()

Output

Running the above code will display a window that contains a button that can be used to close the window.

Now, you can click on the “Close” button to close the window.

raja
Published on 27-Mar-2021 10:56:20
Advertisements