【问题标题】:Invisible button (using pack_forget) still occupies space隐形按钮(使用 pack_forget)仍然占用空间
【发布时间】:2013-05-01 17:04:24
【问题描述】:

我使用 pack_forget() 使按钮不可见。但是当我随后创建一个标签时,它出现在不可见按钮的下方。我怎样才能避免这种位移?

以下示例代码演示了该问题:

from tkinter import *

class Application(Frame):
    def secondwidget(self):
        self.b.pack_forget()
        self.l = Label(text="Lowered Label :(")
        self.l.pack()

    def firstwidget(self):
        self.b = Button(self)
        self.b["text"] = "Button"
        self.b["command"] = self.secondwidget
        self.b.pack()

    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.pack()
        self.firstwidget()

root = Tk()
app = Application(master=root)
app.mainloop()
root.destroy()

编辑:我使用 python 3.2

【问题讨论】:

    标签: python user-interface python-3.x tkinter


    【解决方案1】:

    您忘记为标签设置父级:

    self.l = Label(text="Lowered Label :(")
    print(self.l.winfo_parent() == str(self))
    >>> False
    

    比较:

    self.b = Button(self)
    print(self.b.winfo_parent() == str(self))
    >>> True
    

    【讨论】:

    猜你喜欢
    • 2018-02-26
    • 1970-01-01
    • 2011-03-04
    • 2017-04-13
    • 2013-09-13
    • 1970-01-01
    • 2018-11-26
    • 1970-01-01
    • 2019-12-12
    相关资源
    最近更新 更多