如何清除 Tkinter 上的窗口?这是我的代码:
import sys
from tkinter import *
mGui = Tk ()
mGui.geometry("600x600+545+170")
mGui.title("MyMathDictionary")
mLabel1 = Label (text = "Welcome to MyMathDictionary. Press Next to continue.",fg = "blue",bg = "white").place (x= 150,y = 200)
mbutton = Button (text = "Next").place(x = 275,y = 230)
mGui.mbutton = (mbutton.forget())
请您参考如下方法:
您遇到的问题是由两件事引起的:您将小部件放置在定义小部件的同一行 name = somewidget() ,并且您实际上并没有删除小部件。 mGui.mbutton = (mbutton.forget()) 这行实际上并没有做任何事情。您应该在小部件定义行中使用 command={function name},这样您就可以在单击按钮时调用函数。
.forget() 应该可以工作,但你用错了。你可能应该使用这样的东西:
import sys
import tkinter
from tkinter import *
def next_screen():
mLabel1.place_forget()
mbutton.place_forget()
mGui = tkinter.Tk()
mGui.geometry("600x600+545+170")
mGui.title("MyMathDictionary")
mLabel1 = tkinter.Label(text="Welcome to MyMathDictionary. Press Next to continue.",
fg="blue", bg="white")
mLabel1.place(x=150, y=200)
mbutton = tkinter.Button(text="Next", command=next_screen)
mbutton.place(x=275, y=230)
将 .pack() 或 .place() 与按钮或标签的定义放在同一行,将使小部件成为 nonetype 不知何故。我自己并不完全理解这一点,但是将 widget.place() 放在单独的一行中会有所帮助,您可以自己进行测试。
更好的方法是将小部件名称列表作为输入的函数,并将删除这些小部件中的每一个:
mbutton = tkinter.Button(text="Next", command=forget_page1)
mbutton.place(x=275, y=230)
def next_screen(names):
for widget in names:
widget.place_forget()
def forget_page1():
widgets = [mLabel1, mbutton]
next_screen(widgets)
# Code for the creation of page2 widgets
# You could probably make a function for every page, but I'm sure
# someone could come up with a better answer, instead of repeat
# making functions.
def forget_page2():
widgets = [page2label, page2button, image]
next_screen(widgets)
# Code for the creation of the widgets on page3?
