Automatically close window after a certain time in Tkinter

TkinterServer Side ProgrammingProgramming

In order to close a Tkinter application, we generally refer to close the parent window using the destroy() method. To close the Tkinter window automatically after a certain time limit, we have to use the after(time in ms, callback) method by specifying the time and the callback function which needs to be run after a certain time limit.

Example

#Import the required Libraries
from tkinter import *
from tkinter import ttk

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

#Set the geometry of tkinter frame
win.geometry("750x270")

#Initialize a Label widget
Label(win, text= "This window will get closed after 3 seconds...",
font=('Helvetica 20 bold')).pack(pady=20)

#Automatically close the window after 3 seconds
win.after(3000,lambda:win.destroy())

win.mainloop()

Output

Running the above code will display a window that will get closed after 3 seconds.

The given output window will get closed after 3 seconds which can be modified by changing the value in the after (time, callback) method.

raja
Published on 30-Apr-2021 15:46:08
Advertisements