Joe Stanford
Joe Stanford

Reputation: 51

How do i clear a window after pressing a button in Python Tkinter?

I am currently creating a math's quiz for kids in python tkinter. In this quiz i have 3 different 'pages' as per say. A start page, a quiz page and a score page for when the quiz is finished. In my start page, i have three different difficulties of the quiz the user can choose from. How do i essentially clear elements of the window from the start page such as my label's and button's once that button "EASY" or "HARD" is clicked so i can start the quiz?

Upvotes: 5

Views: 4281

Answers (3)

Miriam
Miriam

Reputation: 2721

If you put all your widgets in one frame, inside the window, like this:

root=Tk()
main=Frame(root)
main.pack()
widget=Button(main, text='whatever', command=dosomething)
widget.pack()
etc.

Then you can clear the screen like this:

main.destroy()
main=Frame(root)

Or in a button:

def clear():
    global main, root
    main.destroy()
    main=Frame(root)
    main.pack()
clearbtn=Button(main, text='clear', command=clear)
clearbtn.pack()

To clear the screen. You can also just create a new window the same way as you created root(not recommended) or create a toplevel instance, which is better for creating multiple but essentially the same. You can also use grid_forget():

w=Label(root, text='whatever')
w.grid(options)
w.grid_forget()

Will create w, but the remove it from the screen, ready to be put back on with the same options.

Upvotes: 3

Bernhard
Bernhard

Reputation: 1273

You could use a tabbed view and switch the tabs according to the difficulty selected (see tkinter notebooks https://docs.python.org/3.1/library/tkinter.ttk.html#notebook) Or you could have the quiz in their selected difficulty in their own windows.

Upvotes: 2

user9483860
user9483860

Reputation:

I would suggest you to use different frames that can be pushed on top of each other. Have a look at this answer Switch between two frames in tkinter.

Upvotes: 1

Related Questions