销毁 Tk 中的标签

Destroying a label in Tk

我为此绞尽脑汁。我是 Python 和 Tk 的新手,只是尝试一下。我认为这真的很容易,但我做不到。这是我的代码:

from tkinter import *
window = Tk()
window.geometry = ('400x200')
mylabel = Label(window)

def button_command():
    Label(window).destroy()
    text = entry1.get()
    selection=variable.get()
    if selection == "Celsius":
        f = "Fahrenheit: " + str(round((int(text) - 32) * 5/9,2))
        mylabel = Label(window, text = f).pack()
    else:
        c = "Celsuius: " + str(round((int(int(text)) * 9/5) + 32))
        mylabel = Label(window, text = c).pack()
    
    return None

def clear_label():
    mylabel.destroy()


entry1 = Entry(window, width = 20)
entry1.pack()
variable = StringVar(window)
variable.set("Fahrenheit") # default value

w = OptionMenu(window, variable, "Fahrenheit", "Celsius")
w.pack()


Button(window, text="Button", command=button_command).pack()
Button(window, text="Clear", command=clear_label).pack()

window.mainloop()

我没有收到错误,但是 clear_label 函数没有执行任何操作。它不会 return 错误。它只是行不通。任何建议,将不胜感激。 :)

您实际上从未将标签装入 window,因此没有任何东西可以销毁。如果你 运行 这段代码,你可以看到打包后,你的函数按预期工作。

from tkinter import *
window = Tk()
window.geometry = ('400x200')
mylabel = Label(window, text="test")

def button_command():
    Label(window).destroy()
    text = entry1.get()
    selection=variable.get()
    if selection == "Celsius":
        f = "Fahrenheit: " + str(round((int(text) - 32) * 5/9,2))
        mylabel = Label(window, text = f).pack()
    else:
        c = "Celsuius: " + str(round((int(int(text)) * 9/5) + 32))
        mylabel = Label(window, text = c).pack()
    
    return None

def clear_label():
    mylabel.destroy()

mylabel.pack

entry1 = Entry(window, width = 20)
entry1.pack()
variable = StringVar(window)
variable.set("Fahrenheit") # default value

w = OptionMenu(window, variable, "Fahrenheit", "Celsius")

mylabel.pack()

Button(window, text="Button", command=button_command).pack()
Button(window, text="Clear", command=clear_label).pack()

window.mainloop()

不确定练习的目的是破坏标签还是只是清除标签并赋予它新的值。如果是后者,可以使用text变量参数来标注来实现。

from tkinter import *


def button_command():
    text = entry1.get()
    selection=variable.get()
    # Change the value of the stringvar to set the new value
    if selection == "Celsius":
        labelvalue.set("Fahrenheit: " + str(round((int(text) - 32) * 5/9,2)))
    else:
        labelvalue.set("Celsuius: " + str(round((int(int(text)) * 9/5) + 32)))
    
    return None

def clear_label():
    # No need to destroy - just change the value
    labelvalue.set("")

window = Tk()
window.geometry = ('400x200')

entry1 = Entry(window, width = 20)
entry1.pack()
variable = StringVar(window)
variable.set("Fahrenheit") # default value

w = OptionMenu(window, variable, "Fahrenheit", "Celsius")
w.pack()


Button(window, text="Button", command=button_command).pack()
Button(window, text="Clear", command=clear_label).pack()

# The text displayed in mylabel will be the contents of labelvalue
labelvalue = StringVar()
mylabel = Label(window, textvariable=labelvalue)
mylabel.pack()

window.mainloop()

原则上不需要删除和re-createLabel,只需清除Label和Entry中的字段:

def clear_label():
    mylabel.config(text="")
    entry1.delete(0, 'end')