在Python Tkinter中删除标签?

时间:2013-06-19 17:31:07

标签: python user-interface grid tkinter visible

我试图隐藏此功能中的所有内容:

def addHome(self):
    Label(self, text = "Would you like to add to your to-do list, or generate a random item?", bg="#efefef").grid(row = 3, columnspan = 2, sticky="W")
    self.txtHome = Entry(self)

    self.btnAddToIt = Button(self, text = "Add To It!", bg="#efefef")
    self.btnAddToIt.grid(row = 4, columnspan = 2)
    self.btnAddToIt["command"] = self.addToIt

    self.btnRandom = Button(self, text = "Random!", bg="#efefef")
    self.btnRandom.grid(row = 5, columnspan = 2)
    self.btnRandom["command"] = self.addRandom

这样我就可以展示这些功能中的东西:

def addToIt(self):
    #self.clearMiddle()
    Label(self, text = "Add To List").grid(row = 3, columnspan = 2)
    self.addInput()

    self.btnProcessAdd = Button(self, text = "Add To It!", bg="#efefef")
    self.btnProcessAdd.grid(row = 7, column = 0)
    self.btnProcessAdd["command"] = self.processAdd

    self.btnCancel = Button(self, text = "Cancel", bg="#efefef")
    self.btnCancel.grid(row = 7, column = 1)
    self.btnCancel["command"] = self.addHome

def addInput(self):
    #adds input for add to item page
    Label(self, text = "Name of Item:", bg="#efefef", width=50).grid(row=3, column=0)
    self.nameOfItem = Entry(self)
    self.nameOfItem.grid(row = 3, column = 1)
    self.nameOfItem.insert(0, "Be Awesome")

    Label(self, text = "Item Category:", bg="#efefef", width=50).grid(row = 4, column = 0, sticky="E")
    self.itemCategory = Listbox(self, height = 5)
    self.itemCategory.grid(row = 4, column = 1)
    self.itemCategory.insert(END, "Fun", "School", "Work", "Exercise", "Other")

    Label(self, text = "Other Item Details:", bg="#efefef", width=50).grid(row = 5, column = 0, sticky="E")
    self.otherItemDetails = Text(self, width=22, height=3)
    self.otherItemDetails.grid(row = 5, column = 1)

    Label(self, text = "Due Date (mm/dd/yy):", bg="#efefef", width=50).grid(row = 6, column = 0, sticky="E")
    self.dueDate = Entry(self)
    self.dueDate.grid(row = 6, column = 1)
    self.dueDate.insert(0, "06/19/2013")

然后在按下“取消”按钮时反之亦然(清除addToIt和addInput中的内容)。我有什么方法可以做到这一点吗?

1 个答案:

答案 0 :(得分:6)

是的,有一些方法。我看到你正在使用grid。因此,要隐藏对象,请使用Object.grid_forget()。 以防万一,如果您使用pack,则可以Object.pack_forget()隐藏对象。同样适用于place

我有一些想法,这可能会派上用场。我建议您将所有要同时隐藏的对象同时隐藏在一个Frame中,这样您只需使用Frame.grid_forget()代替

Obj1.grid_forget()
Obj2.grid_forget()
Obj3.grid_forget()
.
.
.

请记住,使用此对象只会使任何对象不可见,但它仍然存在于“内存”中及其所有属性。