python - 如何清除 Tkinter 上的页面

标签 python tkinter

如何清除 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/

相关文章:

python - 在解析 JSON 文件数据时,根据 Python 中的配置文件中提到的位置添加具有空值的缺失字段

python - 如何在 def 函数中使用 tkinter 从 url 发布图像

python - Tkinter: "Python may not be configured for Tk"

python - 使用请求模块,如何处理请求响应中的 'set-cookie'?

python - 在 python 中的 xlsxwriter 的 data_validation() 方法中使用 validate as list 从源设置默认值?

python - 如何使用Python仅获取网页的文本,就像在浏览器中全选和复制一样?

python - Perl/Tk 的滚动伪小部件是否有 tkinter 等效项?

python - tkinter 中的 Tcl 错误

python - 使单元格适合文本(并使其只读)

python - 想要程序循环的说明 (Python)