Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
destroy() method in Tkinter | Python
Next article icon

Tkinter provides a variety of built-in functions develop interactive and featured GUI (Graphical User Interface).after() function is also a Universal function which can be used directly on the root as well as with other widgets.
 

after(parent, ms, function = None, *args)


 

Parameters: 
parent: is the object of the widget or main window whichever is using this function. 
ms: is the time in milliseconds. 
function: which shall be called. 
*args: other options.


Code #1:
 

Python3
# importing only those functions which# are neededfromtkinterimportTk,mainloop,TOPfromtkinter.ttkimportButton# time function used to calculate timefromtimeimporttime# creating tkinter windowroot=Tk()button=Button(root,text='Geeks')button.pack(side=TOP,pady=5)print('Running...')# Calculating starting timestart=time()# in after method 5000 milliseconds# is passed i.e after 5 seconds# main window i.e root window will# get destroyedroot.after(5000,root.destroy)mainloop()# calculating end timeend=time()print('Destroyed after% d seconds'%(end-start))

Output: 
 


When you run the program it will show a Tkinter window having a
Button but after 5 seconds the window gets destroyed.
Code #2: Prompting a message after certain time (in our program after 5 seconds).
 

Python3
# importing only those functions which# are neededfromtkinterimportTk,mainloop,TOPfromtkinter.ttkimportButtonfromtkinter.messageboximport_show# creating tkinter windowroot=Tk()root.geometry('200x100 + 300 + 250')button=Button(root,text='Geeks')button.pack(side=TOP,pady=5)# in after method 5000 milliseconds# is passed i.e after 5 seconds# a message will be promptedroot.after(5000,lambda:_show('Title','Prompting after 5 seconds'))# Destroying root window after 6.7 secondsroot.after(6700,root.destroy)mainloop()

Output: 
In below output a messagebox will prompt after 5 seconds you can even call any function after a certain period of time by passing the function name.
 


 


Improve
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp