我将使用伪代码,因为我的真实代码更加混乱。 基本上我有一个看起来像这样的代码:
if i>0 and B!=1:
#display label #1 using .grid
elif: i<0 and B!=1 :
#display label #2 but on the same place as label#1
所以我最终将两个标签叠加在一起。如何获得其中一个标签
在显示其他标签之前删除了吗?
编辑:请注意,我的程序也将在无限循环中运行,因此两个标签都将在程序运行时显示。
答案 0 :(得分:2)
使用widget.destroy()
方法销毁label1
:
if i>0 and B!=1:
label1 = Tk.Label(root,text='Label1')
label1.grid(row = 2,column=5)
elif i<0 and B!=1:
if 'label1' in globals():
label1.destroy()
label2 = Tk.Label(root,text='Label2')
label2.grid(row = 2,column=5)
注意你可以使用:
if 'label1' in globals():
label1.destroy()
避免local variable "label1" refrence before assignment
错误