Remove Widgets from grid in Tkinter

01 Apr, 2021
Do you know that you can remove or display the widgets from a grid on a single button click while working in your app? We can remove widgets from the grid by declaring the function for remove with grid_remove() method for every widget and then call that function in the button. Similarly, for displaying the widgets back on screen, We can declare the function for display with grid() method for every widget and then call that function in the button.
Step 1: First, import the library tkinter.
from tkinter import *
Step 2: Now, create a GUI app using tkinter.
app=Tk()
Step 3: Then, create a function to remove widgets from the grid in tkinter. In the function created, we have used the inbuilt function grid_remove() to remove certain widgets.
def remove(widget1):
    widget1.grid_remove()
Here, we have passed one widget as an argument. You can add as many widgets you want which you wish to remove from a certain grid.
Step 4: Further, create a function to display widgets back in the grid in tkinter. In the function created, we have used the inbuilt function grid() to display the certain widgets back with the grid values where you want to display them back.
def display(widget1):
    widget1.grid(column=#Column value where you want to get back the widget, 
                 row=#Row value where you want to get the widget back, 
                 padx=10, 
                 pady=10)
Here, we have passed one widget as an argument. You can add as many widgets you want which you wish to display back in a certain grid by specifying its grid location.
Step 5: Next, we create the widgets which we need to hide or display using the functions declared above. For removing and displaying back the widget from a certain row and column of the grid:
For widget Label, declare label as:
l1=Label(app, text=”#Text you wish to show in label”)
l1.grid(column=#Column value where you want to specify label, 
        row=#Row value where you want to specify label, 
        padx=#Padding Value of x, 
        pady=#Padding Value of y)
For widget Textbox, declare textbox as:
l = StringVar()
l1 = Entry(app, width = 15, textvariable = l)
l1.grid(column=#Column value where you want to specify textbox, 
        row=#Row value where you want to specify textbox, 
        padx=#Padding Value of x,
        pady=#Padding Value of y)
For widget Button, declare button as:
l1 = Button(app, text=”#Text you want to show in button”)
l1.grid(column=#Column value where you want to specify button, 
        row=#Row value where you want to specify button, 
        padx=#Padding Value of x, 
        pady=#Padding Value of y)
For any other widget other than the above specified, declare that widget in place of these.
Step 6: Later on, create a button which when clicked will remove the widgets from the screen.
remove_btn = Button(app, text = “Remove widgets”, command = lambda : remove(l1))
remove_btn.grid(column=0, row=0, padx=10, pady=10)
IMPORTANT NOTE: Don’t forget to call the remove function in the command attribute of the button by specifying the number of arguments in the function which you have created earlier.
Step 7: Moreover, create a button which when clicked will display the widgets back on the screen.
display_btn = Button(app, text = “#Text you wish to give to button”, command = lambda : display(l1))
display_btn.grid(column=0, row=1, padx=10, pady=10)
IMPORTANT NOTE: Don’t forget to call the display function in the command attribute of the button by specifying the number of arguments in the function which you have created earlier.
Step 8: Finally, make the loop for displaying the GUI app on the screen.
app.mainloop()
Example:
In this example, we will remove one label titled ‘Vinayak’ from the grid value (row=3, column=0) and one label titled ‘Rai‘ from grid value (row=4, column=0) using the single button ‘Remove Widgets‘ while display the label back at grid value (row=3, column=0) and button back at the grid value (row=4, column=0) using the button ‘Display Widgets.’
Python
# Python program to remove widgets# from grid in tkinter  # Import the library tkinterfrom tkinter import *  # Create a GUI appapp = Tk()  # Creating a function for removing widgets from griddef remove(widget1, widget2):    widget1.grid_remove()    widget2.grid_remove()  # Creating a function for making widget visible againdef display(widget1, widget2):    widget1.grid(column=0, row=3, padx=10, pady=10)    widget2.grid(column=0, row=4, padx=10, pady=10)  # Button widgetsb1 = Button(app, text="Vinayak")b1.grid(column=0, row=3, padx=10, pady=10)  # Label Widgetsl1 = Label(app, text="Rai")l1.grid(column=0, row=4, padx=10, pady=10)  # Create and show button with remove() functionremove_btn = Button(app, text="Remove widgets",                     command=lambda: remove(b1, l1))remove_btn.grid(column=0, row=0, padx=10, pady=10)  # Create and show button with display() functiondisplay_btn = Button(app, text="Display widgets",                     command=lambda: display(b1, l1))display_btn.grid(column=0, row=1, padx=10, pady=10)  # Make infinite loop for displaying app on screenapp.mainloop()
Output:
Picked
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 ?
Python Classes and Objects
Python OOPs Concepts
Introduction To PYTHON
How to drop one or multiple columns in Pandas Dataframe
Python | os.path.join() method
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
Python | Get unique values from a list
Python | datetime.timedelta() function