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: NoneReturn: 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
Python3
from tkinter import *
app=Tk()
app.title('Remove Tkinter Widget')
app.geometry("600x400")
def remove_widget():
label.place_forget()
label=Label(app, text="Tkinter Widget", font='Helvetica 20 bold')
label.place(relx=0.5, rely=0.3, anchor=CENTER)
button=Button(app, text="Remove Widget", command=remove_widget)
button.place(relx=0.5, rely=0.7, anchor=CENTER)
app.mainloop()
Output:
Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills and become a part of the hottest trend in the 21st century.
Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.
Last Updated :
22 Apr, 2022
Like Article
Save Article
Share your thoughts in the comments
Add Your Comment
Please Login to comment...