2

如何清除 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())
4

1 回答 1

2

您遇到的问题是由两件事引起的:小部件与小部件place在同一行中,并且您实际上并没有删除小部件。这条线并没有真正做任何事情。您应该在小部件定义行中使用,以便在单击按钮时调用函数。definename = 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?
于 2013-08-08T19:47:53.357 回答