2016-09-22 938 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