如何在 tkinter 中更新小部件?

How to update a widget in tkinter?

所以我正在用 python 制作一个 tkinter GUI 应用程序,你如何让它在按下按钮时忘记标签而不是按钮?

import tkinter as tk
window = tk.Tk()
def func():
    mainframe = tk.Frame()
    label = tk.Label(text = "Label", master = mainframe)
    label.pack()
    mainframe.pack()
def function():
    mainframe = tk.Frame()
    button = tk.Button(text = "Button", master = mainframe, command = lambda: remove(mainframe))
    button.pack()
    mainframe.pack()
    func()
def remove(mainframe):

    mainframe.forget()

function()
window.mainloop()

我很不擅长编码,这可能是一个非常愚蠢的问题。

只需像下面那样添加 label.forget()

import tkinter as tk
window = tk.Tk()
def func():
    mainframe = tk.Frame()
    label = tk.Label(text = "Label", master = mainframe)
    label.pack()
    label.forget()
    mainframe.pack()
def function():
    mainframe = tk.Frame()
    button = tk.Button(text = "Button", master = mainframe, command = lambda: remove(mainframe))
    button.pack()
    mainframe.pack()
    func()
def remove(mainframe):

    mainframe.forget()

function()
window.mainloop()
import tkinter as tk

window = tk.Tk()

labelframe = None


def func():
    global labelframe
    labelframe = tk.Frame()
    label = tk.Label(text="Label", master=labelframe)
    label.pack()
    labelframe.pack()


def function():
    global label
    buttonframe = tk.Frame()
    button = tk.Button(text="Button", master=buttonframe, command=lambda: remove(labelframe))
    button.pack()
    buttonframe.pack()
    func()


def remove(to_forget):
    to_forget.forget()


function()
window.mainloop()

我知道了,我只需要定义一个全局变量并像这样将其更改为大型机即可。谢谢你的帮助:)我真的很感激。

    import tkinter as tk
    window = tk.Tk()
    def func():
        mainframe = tk.Frame()
        label = tk.Label(text = "Label", master = mainframe)
        label.pack()
        mainframe.pack()
        global x
        x = mainframe
    def function():
        mainframe = tk.Frame()
        button = tk.Button(text = "Button", master = mainframe, command = lambda: 
    remove(mainframe))
        button.pack()
        mainframe.pack()
        func()
    def remove(mainframe):

        x.forget()

    function()
    window.mainloop()