Skip to content Skip to sidebar Skip to footer

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 i

Solution 1:

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.

Solution 2:

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.

Post a Comment for "How Do I Clear A Window After Pressing A Button In Python Tkinter?"