如何清除 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())
最佳答案
您遇到的问题是由两件事引起的:您 place
该小部件与您 define
位于同一行小部件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?
关于python - 如何清除 Tkinter 上的页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18134116/