How to temporarily remove a Tkinter widget without using just .place?
In this article, we will cover how to temporarily remove a Tkinter widget without using just .place
We can temporarily remove a Tkinter widget by calling the remove_widget() method to make the widgets temporarily invisible. We need to call the place_forget() method, it will hide or forget a widget from the parent widget screen in Tkinter.
Syntax: widget.place_forget()
Parameter: None
Return: None
Stepwise Implementation
Step 1: Import the library.
from tkinter import *
Step 2: Create a GUI app using Tkinter.
app=Tk()
Step 3: Give a title and dimensions to the app.
app.title(“Title you want to assign to app”) app.geometry("Dimensions of the app")
Step 4: Make a function to remove a Tkinter widget when the function is called.
def remove_widget(): label.place_forget()
Step 5: Create a widget that needs to be removed on a particular instance.
label=Label(app, text="Tkinter Widget", font='#Text-Font #Text-Size bold')
label.place(relx=#Horizontal Offset, rely=#Vertical Offset, anchor=CENTER)
Step 6: Create a button to remove the widget.
button=Button(app, text="Remove Widget", command=remove_widget)
button.place(relx=#Horizontal Offset, rely=#Vertical Offset, anchor=CENTER)
Step 7: At last, call an infinite loop for displaying the app on the screen.
app.mainloop()
Below is the Implementation
# Python program to temporarily remove a
# Tkinter widget without using just place
# Import the Tkinter library
from tkinter import *
# Create a GUI app
app=Tk()
# Set the title and geometry of the window
app.title('Remove Tkinter Widget')
app.geometry("600x400")
# Make a function to remove the widget
def remove_widget():
label.place_forget()
# Create a label widget to display text
label=Label(app, text="Tkinter Widget", font='Helvetica 20 bold')
label.place(relx=0.5, rely=0.3, anchor=CENTER)
# Create a button to remove text
button=Button(app, text="Remove Widget", command=remove_widget)
button.place(relx=0.5, rely=0.7, anchor=CENTER)
# Make infinite loop for displaying app
# on the screen
app.mainloop()
Output:
