How could I hide an existing Label when a button is clicked in Python(Tkinter)?
Asked
Active
Viewed 3.6k times
4
-
possible duplicate of [In Tkinter is there any way to make a widget not visible?](http://stackoverflow.com/questions/3819354/in-tkinter-is-there-any-way-to-make-a-widget-not-visible) – Bhargav Rao Dec 26 '14 at 08:07
3 Answers
9
This really depends on the geometry manager you used. If you use
lbl = Tkinter.Label(parent)
to create the label, you will use one of the following to hide it.
lbl.grid_forget()
lbl.pack_forget()
lbl.place_forget()
edit (working example)
import tkinter
class MyClass(tkinter.Frame):
def __init__(self,parent, *args, **kwargs):
tkinter.Frame.__init__(self, parent, *args, **kwargs)
self.btn = tkinter.Button(self,text='Don\'t push me',command=self.buttonCmd)
self.btn.grid(row=0,column=0,sticky='nwes')
self.lbl = tkinter.Label(self,text='Push it, it\'s fun')
self.lbl.grid(row=0,column=1,sticky='nwes')
def buttonCmd(self,*args,**kwargs):
self.lbl.grid_forget()
root = tkinter.Tk()
MyFrame = MyClass(root)
MyFrame.pack(expand='true',fill='both')
root.mainloop()

baited
- 326
- 1
- 3
5
Use can use grid_remove()
to hide the label.
like self.myLabel.grid_remove()
. If you want to show it again then use self.myLabel.grid()
. This will show widget on its original position on grid.

DigviJay Patil
- 986
- 14
- 31
0
If you use pack for you widget:
from tkinter import *
root = Tk()
def hide():
label.pack_forget()
label = Label(root, text="The text")
label.bind("<Button-1>", hide)
label.pack()
root.mainloop()
If you use place to widget
change label.pack_forget()
to ```label.place_forget()
If you use grid to widget
change label.pack_forget()
to label.grid_forget()

Aryasatya
- 36
- 1
- 3