To begin, I advise against using loops in tkinter, because they will interfere with mainloop .
What you want to do can be done in at least two ways:
Using the same object StringVar
is for the object Entry
and for Label
, which is as simple as the following:
from tkinter import *
root = Tk()
prompt = Label(root, text="Insere algo")
prompt.pack(side="left")
entry_content = StringVar()
# Associando "entry_content" a propriedade "textvariable".
# O que escreves no "entry" é "absorvido" pelo "entry_content"
entry = Entry(root, textvariable=entry_content)
entry.pack(side="left")
# O valor do "entry_content" é modificado no "entry"
# E essa modificação vai-se refletir nesta "label"
label = Label(root, text="", textvariable=entry_content)
label.pack(side="left")
root.mainloop()
Do not use an object StringVar
, but bind a call to a function with a certain event, in which case the event is <KeyRelease>
, which almost happens when you stop loading a key. In this function you can type control the contents of the Label and compare it with the contents of the Entry.
from tkinter import *
root = Tk()
prompt = Label(root, text="Insere algo")
prompt.pack(side="left")
entry = Entry(root)
entry.pack(side="left")
def on_key_pressing(event, entry, label):
print(entry.get(), label.cget("text"))
if entry.get() != label.cget("text"):
label.config(text=entry.get())
# Associando o evento <Key> com a chamada
# a funcão on_key_pressing
entry.bind("<KeyRelease>", lambda e: on_key_pressing(e, entry, label))
label = Label(root, text="")
label.pack(side="left")
root.mainloop()
If you do not understand something, ask in the comments, and I edit the answer;)