Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 963
Joe Stanford Avatar asked Apr 10 '18 08:04

Joe Stanford


2 Answers

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.

like image 186
Artemis Avatar answered Sep 22 '22 18:09

Artemis


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.

like image 34
Bernhard Avatar answered Sep 21 '22 18:09

Bernhard