2016-09-22 327 views
0

我想隱藏/刪除我的窗口(暫時)與「hide_widgets」函數的所有按鈕,所以我可以把它們回來之後,但它只是不工作,我已經嘗試使用grid_hide()destroy()和任何我試過所以從搜索stackoverflow不工作。如何清除tkinter(Python)中的窗口?

這是到目前爲止我的程序:

from tkinter import * 

class Application(Frame): 
    #GUI Application 

    def __init__(self, master): 
     #Initialize the Frame 
     Frame.__init__(self,master) 
     self.grid() 
     self.create_widgets() 

    def create_widgets(self): 
     #Create new game etc... 

     #Title 
     self.title = Label(self,text = "Gnome") 
     self.title.grid() 

     #New Game 
     self.new_game = Button(self,text = "New Game") 
     self.new_game ["command"] = self.create_new_game 
     self.new_game.grid() 

     #Load Game 
     self.load_game = Button(self,text = "Load Game") 
     self.load_game ["command"] = self.display_saves 
     self.load_game.grid() 

     #Settings 
     self.settings = Button(self,text = "Settings") 
     self.settings ["command"] = self.display_settings 
     self.settings.grid() 

     #Story 
     self.story = Button(self,text = "Story") 
     self.story ["command"] = self.display_story 
     self.story.grid() 

     #Credits 
     self.credits = Button(self,text = "Credits") 
     self.credits ["command"] = self.display_credits 
     self.credits.grid() 

    def hide_widgets(self): 
     #clear window 
     new_game.grid_forget() 

    def create_new_game(self): 
     #Create new game file 
     self.hide_widgets 
     self.instruction = Label(self, text = "Name World:") 
     self.instruction.grid() 

     self.world_name = Entry(self) 
     self.world_name.grid() 

    def display_saves(self): 
     #display saved games and allow to run 
     print("saves") 

    def display_settings(self): 
     #display settings and allow to alter 
     print("settings") 

    def display_story(self): 
     #display story 
     print("story") 

    def display_credits(self): 
     #display credits 
     print("credits") 

root = Tk() 
root.title("Welcome") 
width, height = root.winfo_screenwidth(), root.winfo_screenheight() 
root.geometry('%dx%d+0+0' % (width,height)) 
app = Application(root) 

root.mainloop() 

預先感謝您。

+0

隱藏或禁用? –

+0

你的意思是像「new_game.hide()」??也不起作用。 – Choncy

+0

不,我的意思是改變狀態,還是想暫時徹底刪除它們? –

回答

0

好吧我現在工作,傻我忘了「()」在self.hide_widgets(),我從來沒有想過它,因爲沒有錯誤,因爲它是創建一個變量,而不是。

1

您可以通過調用各自的grid_forget()方法來隱藏Button

爲了方便起見,您可能需要創建一個包含全部的self.buttons列表或字典。

另外還有一個grid_slaves()方法,您可以在Application實例上使用它,該實例將爲您提供它管理的所有widgets(或只是指定行或列中的那些)的列表。 Button應該在其中一個列表中。我從來沒有使用它,所以我不知道在返回的列表中識別它們是多麼容易。

0

您是否試過用self.new_game.grid_forget()替換new_game.grid_forget()

檢查this回答解釋爲什麼self需要明確引用。我運行了一個非常簡單的腳本來測試這種行爲,它運行良好。

+0

我也需要這個,謝謝 – Choncy

+0

不客氣!不禁嘲笑這樣一個事實,即我們幾乎全都查看了缺少功能調用〜 – 2016-09-22 15:47:50

+0

其始終的小事情 – Choncy