Skip to content
Related Articles

Related Articles

How to Hide, Recover and Delete Tkinter Widgets?
  • Last Updated : 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

filter_none

edit
close

play_arrow

link
brightness_4
code

# Imports tkinter
from tkinter import *
  
# toplevel window
root = 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) visible
def show_button(widget):
    # This will recover the widget from toplevel
    widget.pack()
  
  
# Button widgets
B1 = Button(root, text="Button 1")
B1.pack()
  
  
# See, in command hide_button() function is passed to hide Button B1
B2 = Button(root, text="Button 2", command=lambda: hide_button(B1))
B2.pack()
  
# In command show_button() function is passed to recover Button B1
B3 = Button(root, text="Button 3", command=lambda: show_button(B1))
B3.pack()
  
root.mainloop()

chevron_right


Output:

Example 2: Hide and Recover the Label in Tkinter using forget_pack() and pack() method. 

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Imports tkinter
from tkinter import *
  
# toplevel window
root = Tk()
  
# label widget
label = Label(root, text="LABEL")
  
  
# Method to make Label(Widget) invisible
def hide_label():
    # This will remove the widget
    label.pack_forget()
  
  
# Method to make Label(widget) visible
def recover_label():
    # This will recover the widget
    label.pack()
  
  
# hide_label() function hide the label temporarily
B2 = Button(root, text="Click To Hide label", fg="red", command=hide_label)
B2.pack()
  
# recover_label() function recover the label
B1 = Button(root, text="Click To Show label",
            fg="green", command=recover_label)
B1.pack()
  
# pack Label widget
label.pack()
  
# Start the GUI
root.mainloop()

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

from tkinter import *
  
# toplevel window
root = Tk()
  
# label widget
mylabel = 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 destroyed
B1 = Button(root, text="DELETE", command=delete_label)
B1.pack()
  
# start the GUI
root.mainloop()

chevron_right


Output:

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :