在Python Tkinter中删除标签?

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

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

解决方法:

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

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

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

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

上一篇:6.WinFor练习--简单记事本程序


下一篇:[Web 前端] 012 css 元素溢出