销毁 Tk 中的标签

发布于 2025-01-17 04:34:14 字数 1017 浏览 0 评论 0原文

我为此伤透了脑筋。我是 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 函数没有执行任何操作。它不会返回错误。它就是行不通。任何建议将不胜感激。 :)

I've wracked my brain about this. I'm new to Python and Tk,and just trying it out. I would think this would be really easy, but I can't get it. Here's my code:

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()

I don't get an error, but the clear_label function doesn't do anything. It doesn't return an error. It just doesn't work. Any suggestions would be appreciated. :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

櫻之舞 2025-01-24 04:34:14

您实际上从未将标签放入窗户中,因此没有什么可以破坏的。如果运行此代码,您可以看到打包后,您的函数将按预期工作。

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()

You never actually packed the label into the window, therefore there was nothing to destroy. If you run this code, you can see that once packed, your function works as expected.

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()
逆流 2025-01-24 04:34:14

不确定该练习的目的是销毁标签还是只是清除标签并赋予其新值。如果是后者,可以使用文本变量参数进行标注来实现。

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()

Not sure whether the aim of the exercise is to destroy a label or just clear the label and give it a new value. If it is the latter, it can be achieved using the text variable parameter to label.

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()
爱,才寂寞 2025-01-24 04:34:14

原则上,您不必删除并重新创建 Label,只需清除 Label 和 Entry 中的字段即可:

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

In principle, you don't have to delete and re-create the Label, just clear the fields in the Label and in the Entry:

def clear_label():
    mylabel.config(text="")
    entry1.delete(0, 'end')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文