简体   繁体   中英

How to Clear the window in tkinter (Python)?

I want to hide/remove all the buttons from my window (temporarily) with the "hide_widgets" function so I can put them back after but its just not working for me, I have tried using grid_hide() and destroy() and anything I have tried so for from searching stackoverflow as not worked either.

Here is my program so far:

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()

Thank you in advance.

You can hide the Button s by calling each one's grid_forget() method.

To make that easier you might want to create a self.buttons list or dictionary that contains them all.

Alternatively there's also a grid_slaves() method you might be able to use on the Application instance that will give you a list of all the widgest it manages (or just the ones in a specified row or column). The Button s should be in one of these lists. I've never used it, so I don't know how easy it would be to identify them in the list returned however.

Have you tried replacing new_game.grid_forget() with self.new_game.grid_forget() ?

Check this answer out for an explanation as to why self needs to be referenced explicitly. I ran a very simple script to test this behavior and it worked fine.

好的,我现在开始工作了,愚蠢的我忘记了self.hide_widgets() “()”,我只是从没想过它,因为没有错误,因为它正在创建一个变量。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM