不可见按钮(使用pack\u forget)仍然占用sp

2024-10-04 05:24:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用pack\u 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()

编辑:我使用python3.2


Tags: textselfmasterapplicationinitdefbuttonroot