-1

如何清除 a = Label() 的代码?

使用 .place() 函数 a = Label() 时如何清除此标签的文本?

l = IntVar()
t = IntVar()
a = StringVar()

def speed(): 
    global a
    length= l.get()
    time = t.get()
    answer = length/time
    a = Label(frame, text=answer, font="Times 13 bold", bg="Black", fg="WHITE" ).place(x = 100 , y = 160)
    
def clear():
    '''This function is not clearing label of where I am displaying the answer in a = Label '''
    l.set('')
    t.set('')
    #a.set('')<---NOT WORKING
    #a.delete(0,'END')<----NOT WORKING
    #a.destroty()<----NOT WORKING
    #a.configure(text="")<---NOT WORKING
    
ll = Entry(frame, width=30, textvariable=l , border=5).place(x=150 , y=90)
le = Label(frame, text="Enter Length :" , font="Times 16 bold" , bg="WHITE", fg="Black" ).place(x=10 , y = 88 )

tt = Entry(frame, width=30, textvariable=t , border=5).place(x=150 , y=130)
ti = Label(frame, text="Enter Time :" ,font="Times 16 bold" , bg="WHITE", fg="Black" ).place(x=10 , y = 128 )

ans = Label(frame, text="Speed = " , font="Times 16 bold", bg="WHITE", fg="Black" ).place(x = 10 , y = 160)

spd = Button(frame, text="Calculate Speed", width=20, command=speed , bg="Black", fg="WHITE" , border = 4 ).place(x=30 , y = 190 )
cl = Button(frame, text="Clear Text", width=20, command=clear , bg="Black", fg="WHITE" , border = 4 ).place(x=200 , y = 190 )
4

1 回答 1

0

问题是,由于您使用.placeLabel/Entry对象的对象定义,因此返回值将是None

x = Label(<configurable_options>).place(...)  # x is None

然而,

x = Label(<configurable_options>)  # defines x as a Label object
x.place(...)  # places x at your desired location

适用于所有布局管理器-placegridpack

此外,您还需要为textvaribale您分配一个以便Label能够清除它们(感谢@CoolCloud)

l = StringVar()
l1 = Label(..., textvariable=l)

如果您不想添加文本变量,则只需.config使用Label.

l1.config(text='')
于 2020-10-09T11:02:49.667 回答