5

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?

Joe Stanford
  • 51
  • 1
  • 4
  • You posted this question after @JhonKey posted the same, so should be a duplicate, but this is better quality, and a can't vote for duplicate since JhonKey's doesn't have accepted/upvoted answers. – Artemis Apr 10 '18 at 08:54
  • I am still struggling on this so if anyone has a different answer would be much appreciated. – Joe Stanford Apr 10 '18 at 09:53

3 Answers3

3

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.

Artemis
  • 2,553
  • 7
  • 21
  • 36
  • Your method to clear the screen would not really work. I am trying to delete certain aspects of my window once either 3 buttons are pressed. This is because i want to keep my background, photo etc to keep the same GUI design. I know you can do `pack_forget` or `place_forget` etc. I just needed help on how to code it. A method i found was once my button was clicked, a function would be called and inside this function would delete these widgets. However having difficulty coding this as i am only learning right now. – Joe Stanford Apr 10 '18 at 09:05
  • @JoeStanford With the first option (in a button), you only need to put stuff in the frame you want to clear, the rest can just go in the root. – Artemis Apr 10 '18 at 09:09
  • @JoeStanford Edited answer to include `grid_forget()` – Artemis Apr 10 '18 at 09:12
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.

Bernhard
  • 1,253
  • 8
  • 18
1

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.

  • Thanks for the anwser, i have just looked at it and i understand the concept however findind it quite difficult for me to implement it into my code. I'll keep it on trying, thanks. – Joe Stanford Apr 10 '18 at 08:44