在Tkinter有什么方法可以使小部件不可见?

时间:2010-09-29 06:46:43

标签: python tkinter

这样的东西会使小部件正常显示:

Label(self, text = 'hello', visible ='yes') 

虽然这样的事情会使小部件根本不显示:

Label(self, text = 'hello', visible ='no') 

7 个答案:

答案 0 :(得分:47)

您可能会对窗口小部件的pack_forgetgrid_forget方法感兴趣。在以下示例中,单击时按钮消失

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

答案 1 :(得分:27)

另一个选项中解释的一个选项是使用pack_forgetgrid_forget。另一种选择是使用liftlower。这会更改窗口小部件的堆叠顺序。实际效果是你可以隐藏兄弟小部件(或兄弟姐妹的后代)后面的小部件。如果您希望它们可见,lift它们,当您希望它们不可见时,lower它们。

优势(或劣势......)是他们仍占用主人的空间。如果你“忘记”一个小部件,其他小部件可能会重新调整它们的大小或方向,但如果你提高或降低它们,它们就不会。

这是一个简单的例子:

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

答案 2 :(得分:4)

我知道这已经晚了几年,但这是Google截至2013年10月27日对“Tkinter hide Label”的第三次回应...所以如果有人像我这样的几周前建立一个简单的GUI并且只是希望显示一些文本而不通过“更低”或“提升”方法将其换成另一个小部件,我想提供一个我使用的解决方法(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()

这里的想法是你可以使用“.cget('bg')”http://effbot.org/tkinterbook/widget.htm将“禁用”文本的颜色设置为父级的背景(“bg”),使其“不可见”。按钮回调将Label重置为默认前景色,文本再次可见。

这里的缺点是你仍然需要为文本分配空间,即使你无法阅读它,至少在我的电脑上,文字并没有完美地融合到背景中。也许通过一些调整,颜色可能更好,对于紧凑的GUI,空白空间分配不应该是一个简短的模糊的麻烦。

有关我如何找到颜色的信息,请参阅Default window colour Tkinter and hex colour codes

答案 3 :(得分:1)

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

P.S。 config可以更改任何属性:

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

它绕过StringVarIntVar等。

答案 4 :(得分:1)

我没有使用网格或包。
我只使用了我的小部件的地方,因为它们的大小和位置是固定的。
我想在框架上实现隐藏/显示功能。
这是演示

original

请注意,在上面的示例中,当勾选“显示图形”时,会出现垂直滚动条。
取消选中复选框时图表消失。
我在那个区域拟合了一些条形图,为了保持示例简单。
从上面学到的最重要的一点是使用 frame.place_forget() 来隐藏和使用 frame.place(x=x_pos,y=y_pos) 来显示内容。

答案 5 :(得分:0)

对于像我这样讨厌OOP的人(这是基于Bryan Oakley的回答)

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

答案 6 :(得分:0)

要隐藏小部件,可以使用pack_forget()函数,再次显示它可以使用pack()函数,并在单独的函数中实现它们。

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()
相关问题