I want to hide the label by clicking on the button and show the same label by clicking on the same button and I want to show those labels below the clicked button. I have tried the below code but by clicking any of the button only the collaborate label is visible in below to button2. but I want it as if I click button1 the "get started" label should appear below to button1 and if I clicked button2 the "collaborate" label should appear below to button2
import tkinter as tk
root=tk.Tk()
def label_hide_show():
global hidden
if hidden:
label1.pack(side = "top", fill = tk.BOTH)
hidden = False
else:
label1.pack_forget()
hidden=True
hidden = True
btn1 = tk.Button(root, text="Get Started", height=3,width=26,bg="White", fg="Black", command=label_hide_show)
label1 = tk.Label(root, text="Get started")
btn1.pack(side = "top", fill = tk.BOTH)
btn2 = tk.Button(root, text="Collaborate", height=3,width=26,bg="White", fg="Black", command=label_hide_show)
label1 = tk.Label(root, text="Collaborate")
btn2.pack(side = "top", fill = tk.BOTH)
Output of the above code:
The first solution that came to mind and seemed easy is to use a single label and just update its text based on the button clicked. So you should change your function to:
def label_hide_show(btn):
global hidden
if hidden:
label1.config(text=btn.cget('text')) # Change text to clicked buttons text
label1.pack(side = "top", fill = tk.BOTH)
hidden = False
else:
label1.pack_forget()
hidden=True
btn1 = tk.Button(root, text="Get Started", height=3,width=26,bg="White", fg="Black", command=lambda: label_hide_show(btn1))
label1 = tk.Label(root) # Make sure to just use one of this label
btn2 = tk.Button(root, text="Collaborate", height=3,width=26,bg="White", fg="Black", command=lambda: label_hide_show(btn2))
This way you can get it to update with the text of the button that gets clicked.
Obviously, there are other ways too, like passing the text you want to show instead of passing the button as argument:
def label_hide_show(text):
global hidden
if hidden:
label1.config(text=text)
label1.pack(side = "top", fill = tk.BOTH)
hidden = False
else:
label1.pack_forget()
hidden=True
btn1 = tk.Button(root, text="Get Started", height=3,width=26,bg="White", fg="Black", command=lambda: label_hide_show('Get started'))
The use of this method is, you can use/pass ANY text other than the buttons text. Other variations of this methods include using a StringVar()
and calling set()
on it to change the text of label, essentially the same principle but using a different set of ways.
UPDATE: Seems like I misread your question, still its easy, here is an example:
def label_hide_show(btn,text):
global hidden
if hidden:
label1.config(text=text) # Change to passed text
label1.pack(side = "top", fill = tk.BOTH, after=btn) # Pack it after the btn
hidden = False
else:
label1.pack_forget()
hidden=True
btn1 = tk.Button(root, text="Get Started", height=3,width=26,bg="White", fg="Black", command=lambda: label_hide_show(btn1,'Label below one'))
btn2 = tk.Button(root, text="Collaborate", height=3,width=26,bg="White", fg="Black", command=lambda: label_hide_show(btn2,'Label below two'))
Now the label will be placed after
the passed button.