Tkinter 删除Else中的标签

Tkinter 删除Else中的标签,tkinter,label,python-3.7,Tkinter,Label,Python 3.7,Python3非常新,我用tkinter编写了我的第一个应用程序 当凭证(编号)超出范围(最大值和最小值)时,我有一个if创建标签,但是如果我首先运行超出范围,然后更改编号,我希望删除else代码开头的标签 我尝试了Label.delete,Label.destroy,但只抛出错误 if int(voucher.get()) > int(maxN): textbox.configure(state="normal") textbox.delete('1.0', END)

Python3非常新,我用tkinter编写了我的第一个应用程序

当凭证(编号)超出范围(最大值和最小值)时,我有一个if创建标签,但是如果我首先运行超出范围,然后更改编号,我希望删除else代码开头的标签

我尝试了
Label.delete
Label.destroy
,但只抛出错误

if int(voucher.get()) > int(maxN):
    textbox.configure(state="normal")
    textbox.delete('1.0', END)
    textbox.update()
    Label(root, text="The voucher " + str(voucher.get()) + " number is outside of valid range").place(x=200, y=175)
    textbox.configure(state="disabled")

elif int(voucher.get()) < int(minN):
    textbox.configure(state="normal")
    textbox.delete('1.0', END)
    textbox.update()
    Label(root, text="The voucher " + str(voucher.get()) + " number is outside of valid range").place(x=200, y=175)
    textbox.configure(state="disabled")

else:
    # I want to destroy Label here
    textbox.configure(state="normal")
    Label(root, text="All well")
如果int(凭证.get())>int(maxN):
textbox.configure(state=“normal”)
textbox.delete('1.0',END)
textbox.update()
标签(root,text=“凭证”+str(凭证.get())+“编号超出有效范围”).place(x=200,y=175)
textbox.configure(state=“disabled”)
elif int(凭证.get())

感谢您的帮助


//Fred

它正在抛出错误,因为您尚未将任何对象指定给这些标签。如果他们没有引用,python如何知道需要销毁哪个标签

destroy()
Label
类的一种方法,如果没有对类标签创建对象,则不能仅通过
Label.destroy()调用该方法

例如:

L = Label(root, text="Hello World")
L.pack()

# Now, we can destroy it by

L.destroy()

现在您的代码,因为我不知道您要删除哪个标签,所以将根据您的要求更改第一个标签

if int(voucher.get()) > int(maxN):
    textbox.configure(state="normal")
    textbox.delete('1.0', END)
    textbox.update()
    L1 = Label(root, text="The voucher " + str(voucher.get()) + " number is outside of valid range")
    L1.place(x=200, y=175)
    textbox.configure(state="disabled")

elif int(voucher.get()) < int(minN):
    textbox.configure(state="normal")
    textbox.delete('1.0', END)
    textbox.update()
    L2 = Label(root, text="The voucher " + str(voucher.get()) + " number is outside of valid range")
    L2.place(x=200, y=175)
    textbox.configure(state="disabled")

else:
    L1.destroy()      # Label in if will destroy
    L2.destroy()      # Label in elif will destroy
    textbox.configure(state="normal")
    L3 = Label(root, text="All well")
如果int(凭证.get())>int(maxN):
textbox.configure(state=“normal”)
textbox.delete('1.0',END)
textbox.update()
L1=标签(根,text=“凭证”+str(凭证.get())+“编号超出有效范围”)
L1.位置(x=200,y=175)
textbox.configure(state=“disabled”)
elif int(凭证.get())
您创建了标签,但没有为每个标签指定参考/标签名称。如果没有引用,则无法告诉程序要删除哪个标签

而不是使用:

Label(root, text="The voucher " + str(voucher.get()) + " number is outside of valid range").place(x=200, y=175)
您必须使用(例如,标签Label1:):

这将创建一个参考/标签名为“Label1”的标签 注意:我将.place()放在下面一行,因为当您将.place()放在同一行时,如下所示:

Label1 = Label(root, text="The voucher " + str(voucher.get()) + " number is outside of valid range").place(x=200, y=175)
else:
    if Label1.winfo_exists():
        Label1.destroy()
    textbox.configure(state="normal")
    Label2 = Label(root, text="All well")
    Label2.place(x=200, y=175)
参考/标签标签1设置为由以下各项返回的内容:

Label(root, text="The voucher " + str(voucher.get()) + " number is outside of   valid range").place(x=200, y=175)
因此它被设置为none,因为.place()返回none

要销毁标签(例如Label1),请使用Label1.destroy()

您的改进代码:


添加到else语句中,以便Label1.destroy()代码仅在存在标签1的情况下运行。这样,如果从未创建过标签1,则程序不会尝试销毁它

这将使您的else语句如下所示:

Label1 = Label(root, text="The voucher " + str(voucher.get()) + " number is outside of valid range").place(x=200, y=175)
else:
    if Label1.winfo_exists():
        Label1.destroy()
    textbox.configure(state="normal")
    Label2 = Label(root, text="All well")
    Label2.place(x=200, y=175)

非常感谢你们两位的帮助,问题解决了,还必须添加:全局label5使用:if label5.winfo_exists():label5.destroy()非常有用//弗莱德
else:
    if Label1.winfo_exists():
        Label1.destroy()
    textbox.configure(state="normal")
    Label2 = Label(root, text="All well")
    Label2.place(x=200, y=175)