Something like this, would make the widget appear normally:
Label(self, text = 'hello', visible ='yes')
While something like this, would make the widget not appear at all:
Label(self, text = 'hello', visible ='no')
from Tkinter import * def hide_me(event): event.widget.pack_forget() root = Tk() btn=Button(root, text="Click") btn.bind('<Button-1>', hide_me) btn.pack() btn2=Button(root, text="Click too") btn2.bind('<Button-1>', hide_me) btn2.pack() root.mainloop()
One option, as explained in another answer, is to use
grid_forget. Another option is to use
lower. This changes the stacking order of widgets. The net effect is that you can hide widgets behind sibling widgets (or descendants of siblings). When you want them to be visible you
lift them, and when you want them to be invisible you
The advantage (or disadvantage…) is that they still take up space in their master. If you “forget” a widget, the other widgets might readjust their size or orientation, but if you raise or lower them they will not.
Here is a simple example:
import Tkinter as tk class SampleApp(tk.Tk): def __init__(self, *args, **kwargs): tk.Tk.__init__(self, *args, **kwargs) self.frame = tk.Frame(self) self.frame.pack(side="top", fill="both", expand=True) self.label = tk.Label(self, text="Hello, world") button1 = tk.Button(self, text="Click to hide label", command=self.hide_label) button2 = tk.Button(self, text="Click to show label", command=self.show_label) self.label.pack(in_=self.frame) button1.pack(in_=self.frame) button2.pack(in_=self.frame) def show_label(self, event=None): self.label.lift(self.frame) def hide_label(self, event=None): self.label.lower(self.frame) if __name__ == "__main__": app = SampleApp() app.mainloop()
I know this is a couple of years late, but this is the 3rd Google response now for “Tkinter hide Label” as of 10/27/13… So if anyone like myself a few weeks ago is building a simple GUI and just wants some text to appear without swapping it out for another widget via “lower” or “lift” methods, I’d like to offer a workaround I use (Python2.7,Windows):
from Tkinter import * class Top(Toplevel): def __init__(self, parent, title = "How to Cheat and Hide Text"): Toplevel.__init__(self,parent) parent.geometry("250x250+100+150") if title: self.title(title) parent.withdraw() self.parent = parent self.result = None dialog = Frame(self) self.initial_focus = self.dialog(dialog) dialog.pack() def dialog(self,parent): self.parent = parent self.L1 = Label(parent,text = "Hello, World!",state = DISABLED, disabledforeground = parent.cget('bg')) self.L1.pack() self.B1 = Button(parent, text = "Are You Alive???", command = self.hello) self.B1.pack() def hello(self): self.L1['state']="normal" if __name__ == '__main__': root=Tk() ds = Top(root) root.mainloop()
The idea here is that you can set the color of the DISABLED text to the background (‘bg’) of the parent using “.cget(‘bg’)” http://effbot.org/tkinterbook/widget.htm rendering it “invisible”. The button callback resets the Label to the default foreground color and the text is once again visible.
Downsides here are that you still have to allocate the space for the text even though you can’t read it, and at least on my computer, the text doesn’t perfectly blend to the background. Maybe with some tweaking the color thing could be better and for compact GUIs, blank space allocation shouldn’t be too much of a hassle for a short blurb.
See Default window colour Tkinter and hex colour codes for the info about how I found out about the color stuff.
For hiding a widget you can use function pack_forget() and to again show it you can use pack() function and implement them both in separate functions.
from Tkinter import * root = Tk() label=Label(root,text="I was Hidden") def labelactive(): label.pack() def labeldeactive(): label.pack_forget() Button(root,text="Show",command=labelactive).pack() Button(root,text="Hide",command=labeldeactive).pack() root.mainloop()
I’m also extremely late to the party, but I’ll leave my version of the answer here for others who may have gotten here, like I did, searching for how to hide something that was placed on the screen with the
.place() function, and not
In short, you can hide a widget by setting the width and height to zero, like this:
widget.place(anchor="nw", x=0, y=0, width=0, height=0)
To give a bit of context so you can see what my requirement was and how I got here.
In my program, I have a window that needs to display several things that I’ve organized into 2 frames, something like this:
[WINDOW - app] [FRAME 1 - hMainWndFrame] [Buttons and other controls (widgets)] [FRAME 2 - hJTensWndFrame] [other Buttons and controls (widgets)]
Only one frame needs to be visible at a time, so on application initialisation, i have something like this:
hMainWndFrame = Frame(app, bg="#aababd") hMainWndFrame.place(anchor="nw", x=0, y=0, width=480, height=320) ... hJTensWndFrame = Frame(app, bg="#aababd")
.place() instead of
.grid() because i specifically want to set precise coordinates on the window for each widget. So, when i want to hide the main frame and display the other one (along with all the other controls), all i have to do is call the
.place() function again, on each frame, but specifying zero for width and height for the one i want to hide and the necessary width and height for the one i want to show, such as:
hMainWndFrame.place(anchor="nw", x=0, y=0, width=0, height=0) hJTensWndFrame.place(anchor="nw", x=0, y=0, width=480, height=320)
Now it’s true, I only tested this on
Frames, not on other widgets, but I guess it should work on everything.
For someone who hate OOP like me (This is based on Bryan Oakley’s answer)
import tkinter as tk def show_label(): label1.lift() def hide_label(): label1.lower() root = tk.Tk() frame1 = tk.Frame(root) frame1.pack() label1 = tk.Label(root, text="Hello, world") label1.pack(in_=frame1) button1 = tk.Button(root, text="Click to hide label",command=hide_label) button2 = tk.Button(root, text="Click to show label", command=show_label) button1.pack(in_=frame1) button2.pack(in_=frame1) root.mainloop()
import tkinter as tk ... x = tk.Label(text='Hello', visible=True) def visiblelabel(lb, visible): lb.config(visible=visible) visiblelabel(x, False) # Hide visiblelabel(x, True) # Show
config can change any attribute:
x.config(text='Hello') # Text: Hello x.config(text='Bye', font=('Arial', 20, 'bold')) # Text: Bye, Font: Arial Bold 20 x.config(bg='red', fg='white') # Background: red, Foreground: white
It’s a bypass of