How to Hide, Recover and Delete Tkinter Widgets? - GeeksforGeeks

11 Dec, 2020
Tkinter is a Python Package for creating GUI(Graphical User Interface) applications. Tkinter provides us with a variety of common GUI elements which we can use to build out interfaces — such as buttons, label, frame, message, menu, and various kind of entry fields and display areas. We call these elements of Widgets.
Widget is an element of Graphical User Interface (GUI) that illustrates information so that a user can interact with the OS.  In Tkinter, Widgets are objects; instances of classes that represent buttons, frames, and so on.  
In this article, we will demonstrate how to Hide, Recover, and Delete the Tkinter widgets, by using the various elements of widgets like buttons, labels, frames etc.
We could hide the Tkinter widgets by calling pack_forget() method to make the widgets invisible. We need to call the pack() method again to pack the widget to make it visible, or to recover it. We can also delete Tkinter widgets permanently by calling the destroy() method in this section.
Example 1: Hide and Recover the button in Tkinter using forget_pack() and pack() method.
Syntax:
widget.forget_pack()
Widget can be any valid widget which is visible.
Python3
# Imports tkinterfrom tkinter import *  # toplevel windowroot = Tk()  # Method to make Button(Widget) invisible from toplevel    def hide_button(widget):    # This will remove the widget from toplevel    widget.pack_forget()    # Method to make Button(widget) visibledef show_button(widget):    # This will recover the widget from toplevel    widget.pack()    # Button widgetsB1 = Button(root, text="Button 1")B1.pack()    # See, in command hide_button() function is passed to hide Button B1B2 = Button(root, text="Button 2", command=lambda: hide_button(B1))B2.pack()  # In command show_button() function is passed to recover Button B1B3 = Button(root, text="Button 3", command=lambda: show_button(B1))B3.pack()  root.mainloop()
Output:
Example 2: Hide and Recover the Label in Tkinter using forget_pack() and pack() method. 
Python3
# Imports tkinterfrom tkinter import *  # toplevel windowroot = Tk()  # label widgetlabel = Label(root, text="LABEL")    # Method to make Label(Widget) invisibledef hide_label():    # This will remove the widget    label.pack_forget()    # Method to make Label(widget) visibledef recover_label():    # This will recover the widget    label.pack()    # hide_label() function hide the label temporarilyB2 = Button(root, text="Click To Hide label", fg="red", command=hide_label)B2.pack()  # recover_label() function recover the labelB1 = Button(root, text="Click To Show label",            fg="green", command=recover_label)B1.pack()  # pack Label widgetlabel.pack()  # Start the GUIroot.mainloop()
Output:
Example 3: Delete Tkinter widgets permanently by calling the destroy() method.  We can use this method with any of the available widgets as well as with the main tkinter window.
Python3
from tkinter import *  # toplevel windowroot = Tk()  # label widgetmylabel = Label(root, text="GeeksforGeeks")mylabel.pack()  # delete_label() function  to delete Label(Widget) permanently    def delete_label():    mylabel.destroy()    # Creating button. In this destroy method is passed# as command, so as soon as button is pressed# Label will be destroyedB1 = Button(root, text="DELETE", command=delete_label)B1.pack()  # start the GUIroot.mainloop()
Output:
Python-tkinter
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
How to drop one or multiple columns in Pandas Dataframe
Python Classes and Objects
Python | os.path.join() method
Python | Get unique values from a list
Create a directory in Python
Defaultdict in Python
Python | Pandas dataframe.groupby()