Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Text detection using Python
Next article icon
Tkinter supports a variety of methods to perform various tasks. It also offers some universal method.destroy() is a universal widget method i.e we can use this method with any of the available widgets as well as with the main tkinter window.Syntax:
widget_object = Widget(parent, command = widget_class_object.destroy)
This method can be used withafter() method.Code #1: destroy() method passed as commandPython3 1==
# importing only those functions# which are neededfromtkinterimport*fromtkinter.ttkimport*# creating tkinter windowroot=Tk()# Creating button. In this destroy method is passed# as command, so as soon as button 1 is pressed root# window will be destroyedbtn1=Button(root,text="Button 1",command=root.destroy)btn1.pack(pady=10)# Creating button. In this destroy method is passed# as command, so as soon as button 2 is pressed# button 1 will be destroyedbtn2=Button(root,text="Button 2",command=btn1.destroy)btn2.pack(pady=10)# infinite loopmainloop()
Output:destroy() method in TkinterAs you may observe, in above code that the command that is passed in button-2 is to destroy button-1 so as soon as you press button-2, button-2 will get destroyed.destroy() method passed as commandCode #2:destroy() method with after() methodPython3 1==
# importing only those functions# which are neededfromtkinterimport*fromtkinter.ttkimport*# creating tkinter windowroot=Tk()# Creating button. In this destroy method is passed# as command, so as soon as button 1 is pressed root# window will be destroyedbtn1=Button(root,text="Button 1")btn1.pack(pady=10)# Creating button. In this destroy method is passed# as command, so as soon as button 2 is pressed# button 1 will be destroyedbtn2=Button(root,text="Button 2")btn2.pack(pady=10)# after method destroying button-1# and button-2 after certain timebtn1.after(3000,btn1.destroy)btn2.after(6000,btn2.destroy)# infinite loopmainloop()
Output:From output you may see that both the widgets are destroyed after a certain time limit and only root window will be left empty.
Note: There is another method availablequit() which do not destroy widgets but it exits the tcl/tk interpreter i.e it stops themainloop().

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