How to temporarily remove a Tkinter widget without using just .place?
Last Updated :
22 Apr, 2022
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
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:
Please Login to comment...