【发布时间】:2021-03-02 17:47:55
【问题描述】:
我想通过单击按钮来隐藏标签,并通过单击同一个按钮来显示相同的标签,并且我想在单击的按钮下方显示这些标签。我已经尝试了下面的代码,但是通过单击任何按钮,只有 collaborate 标签在下面的 button2 中可见。但我希望它就像我点击 button1 “get started” 标签应该出现在 button1 下方,如果我点击 button2 “collaborate”标签应该出现在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)
以上代码的输出:
【问题讨论】:
标签: python-3.x tkinter button label show-hide