5

我试图隐藏此函数中的所有内容:

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 中的内容)。有什么办法可以做到这一点吗?

4

1 回答 1

6

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

我有一些想法,可能会派上用场。我建议您将要同时隐藏的所有对象放在一个中Frame,这样您就可以使用Frame.grid_forget()而不是

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

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

于 2013-06-19T17:41:06.227 回答