【问题标题】:Show/Hide label by clicking on the tkinter button通过单击 tkinter 按钮显示/隐藏标签
【发布时间】:2021-03-02 17:47:55
【问题描述】:

我想通过单击按钮来隐藏标签,并通过单击同一个按钮来显示相同​​的标签,并且我想在单击的按钮下方显示这些标签。我已经尝试了下面的代码,但是通过单击任何按钮,只有 collaborate 标签在下面的 button2 中可见。但我希望它就像我点击 button1get started” 标签应该出现在 button1 下方,如果我点击 button2collaborate”标签应该出现在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


    【解决方案1】:

    想到的第一个似乎很简单的解决方案是使用单个标签并根据单击的按钮更新其文本。所以你应该改变你的功能:

    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))
    
    

    通过这种方式,您可以使用被点击的按钮的文本来更新它。

    显然,还有其他方法,例如传递要显示的文本而不是传递按钮作为参数:

    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'))
    

    此方法的用途是,您可以使用/传递除按钮文本之外的任何文本。此方法的其他变体包括使用StringVar() 并在其上调用set() 来更改标签的文本,原理基本相同,但使用不同的方式。

    更新:似乎我误读了您的问题,仍然很容易,这是一个示例:

    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'))
    

    现在标签将被放置after传递的按钮。

    【讨论】:

    • 谢谢,第二种方法有效,但第一个按钮标签显示在第二个按钮下方,但我希望 label1 在 button1 下可见,label2 在 button2 下可见。 @酷云
    • @Jung-suk 哦,你应该早点提到它。需要对代码进行全面修改
    • 我在解释中确实提到了这些要点。对不起!如果不清楚。 @酷云
    • 谢谢。 @酷云
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-24
    • 2023-03-09
    • 2010-11-18
    • 2021-03-19
    • 2011-03-05
    • 1970-01-01
    • 2016-05-21
    相关资源
    最近更新 更多