python Tkinter clear certain label of there output text

ive wrote some code that writes an output to the screen and it can do this multiple times the problem is i cant seem to get the text before to clear before i write over it so it just looks a bit terrible ive tried using the

tk.delete(0, END)

but thats has done nothing to help so im not sure what to do here is the code if you want to look :

entry1 = tk.Entry(root) 
canvas1.create_window(200, 140, window=entry1)

def encrypt ():
    
    x1 = entry1.get()
        
    try :
        val = int(x1)
    except ValueError :
        label4 = tk.Label(root, text= encryption.encryption(x1) , font=('helvetica', 10, 'bold'))
        canvas1.create_window(200, 250, window=label4)
    else :
        label4 = tk.Label(root, text= encryption.decryption(int(x1)) , font=('helvetica', 10, 'bold'))
        canvas1.create_window(200, 250, window=label4)        
    
    label3 = tk.Label(root, text= 'The new encrypted message is : ',font=('helvetica', 10))
    canvas1.create_window(200, 210, window=label3)
    
button1 = tk.Button(text='Encrypt data', command=encrypt , bg='brown', fg='white', font=('helvetica', 9, 'bold'))
canvas1.create_window(200, 180, window=button1)

i need it to write over the output label which is label4 by the way.

Upvotes: 0

Views: 177

Answers (2)

acw1668
acw1668

Reputation: 47193

Better to create label3 and label4 outside encrypt() function:

label3 = tk.Label(root, text='The new encrypted message is:', font=('helvetica', 10))
canvas1.create_window(200, 210, window=label3, state='hidden', tag='result') # initially hidden

label4 = tk.Label(root, font=('helvetica', 10, 'bold'))
canvas1.create_window(200, 250, window=label4, state='hidden', tag='result') # initially hidden

Then update label4 and make label3 and label4 visible inside encrypt():

def encrypt():
    x1 = entry1.get()
    try:
        val = int(x1)
    except ValueError:
        label4.config(text=encryption.encryption(x1))
    else:
        label4.config(text=encryption.decryption(val))
    # make label3 and label4 visible
    canvas1.itemconfig('result', state='normal')

Upvotes: 0

scotty3785
scotty3785

Reputation: 7006

Labels don't have a delete method, you just change the text.

label4['text'] = ""

Or

label4.config(text="")

Upvotes: 1

Related Questions