在 Python Tkinter 中按下按钮后如何清除窗口?

问题描述:

我目前正在使用 python tkinter 为孩子们创建一个数学测验.在这个测验中,我有 3 个不同的页面".开始页面、测验页面和测验结束时的分数页面.在我的起始页中,我有用户可以选择的三种不同难度的测验.一旦单击EASY"或HARD"按钮,我如何从开始页面清除窗口的元素,例如我的标签和按钮,以便我可以开始测验?

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?

如果你把所有的小部件放在一个框架中,在窗口内,像这样:

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)

或在按钮中:

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

清除屏幕.您也可以像创建 root(不推荐)或创建一个 toplevel 实例一样创建一个新窗口,这对于创建多个但基本相同的实例更好.你也可以使用 grid_forget():

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

将创建 w,但将其从屏幕上移除,准备好使用相同的选项重新显示.

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