Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python | PanedWindow Widget in Tkinter
Next article icon

If we want to unmap any widget from the screen or toplevel then forget() method is used. There are two types of forget method pack_forget() ( similar to forget() ) and grid_forget() which are used with pack() and grid() method respectively.

pack_forget() method -

Syntax: widget.pack_forget()

widget can be any valid widget which is visible.

Code #1:

Python
# Imports tkinter and ttk modulefromtkinterimport*fromtkinter.ttkimport*# toplevel windowroot=Tk()# method to make widget invisible# or remove from topleveldefforget(widget):# This will remove the widget from toplevel# basically widget do not get deleted# it just becomes invisible and loses its position# and can be retrievewidget.forget()# method to make widget visibledefretrieve(widget):widget.pack(fill=BOTH,expand=True)# Button widgetsb1=Button(root,text="Btn 1")b1.pack(fill=BOTH,expand=True)# See, in command forget() method is passedb2=Button(root,text="Btn 2",command=lambda:forget(b1))b2.pack(fill=BOTH,expand=True)# In command retrieve() method is passedb3=Button(root,text="Btn 3",command=lambda:retrieve(b1))b3.pack(fill=BOTH,expand=True)# infinite loop, interrupted by keyboard or mousemainloop()


Output:

forget_pack() method

After forget

After forget

After retrieval

After retrieval

Notice

the difference in the position of Button 1 before and after forget as well as after retrieval.

grid_forget() method -

Syntax: widget.grid_forget()
widget can be any valid widget which is visible.

Note :

This method can be used only withgrid() geometry methods.

Code #2:

Python
# Imports tkinter and ttk modulefromtkinterimport*fromtkinter.ttkimport*# toplevel windowroot=Tk()# method to make widget invisible# or remove from topleveldefforget(widget):# This will remove the widget from toplevel# basically widget do not get deleted# it just becomes invisible and loses its position# and can be retrievewidget.grid_forget()# method to make widget visibledefretrieve(widget):widget.grid(row=0,column=0,ipady=10,pady=10,padx=5)# Button widgetsb1=Button(root,text="Btn 1")b1.grid(row=0,column=0,ipady=10,pady=10,padx=5)# See, in command forget() method is passedb2=Button(root,text="Btn 2",command=lambda:forget(b1))b2.grid(row=0,column=1,ipady=10,pady=10,padx=5)# In command retrieve() method is passedb3=Button(root,text="Btn 3",command=lambda:retrieve(b1))b3.grid(row=0,column=2,ipady=10,pady=10,padx=5)# infinite loop, interrupted by keyboard or mousemainloop()


Output:

forget_grid() method

After Forget

After Forget

After Retrieval

After  Retrieval

Notice that the position of Button 1 remains same after forget and retrieval. With grid_forget() method, you can place it at any grid after retrieval but generally, the original grid is chosen.


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