2013-06-19 99 views
5

我試圖隱藏在這個函數中的一切:在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中)。有什麼辦法可以做到這一點?

+0

是否要刪除窗口小部件或清除字段中的值? – FabienAndre

+2

你可能會對這個其他線程感興趣:[在Tkinter有沒有什麼辦法讓widget不可見?](http://stackoverflow.com/questions/3819354)(特別是[Bryan的回答](http:// stackoverflow .com/a/5928294)) – FabienAndre

+0

@FabienAndre我想刪除小部件。 –

回答

6

是的,有一些方法。我看到你正在使用grid。所以,隱藏一個對象使用Object.grid_forget()。 爲了以防萬一,如果您使用pack,您可以按Object.pack_forget()隱藏對象。同樣的事情適用於place

我有一些想法,可能會派上用場。我建議你必須要在一個Frame同時隱藏所有的對象,所以你只需要使用Frame.grid_forget()代替

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

請記住,使用這樣只會讓任何物體隱形,但它仍然存在「內」內存及其所有屬性。

+0

我試過把self.txtHome.grid_forget(),但它沒有工作。我寫錯了嗎? –

+0

在你發佈的例子中,self.txtHome沒有佈局,即你沒有調用grid,這可以解釋grid_forget沒有做任何事情。 – FabienAndre

+0

@FabienAndre其實網格只是在最後被調用。 –