Home > Back-end >  How do I update Tkinter label?
How do I update Tkinter label?

Time:12-18

I'm trying to update the Tkinter label on click of the button. Doesn't work when I click the button.

Here's the code:

valid = tk.StringVar()
valid.set("Not signed in")


validtext = tk.Label(root, text=valid.get(), font="Arial").grid(row=5, column=1)

testbtn_txt = tk.StringVar()
testbtn = tk.Button(root, textvariable=testbtn_txt, command=lambda:testfunc(), font="Arial", bg="#ff0000", fg="white", height=1, width=10)
testbtn_txt.set("Test")
testbtn.grid(row=3, column=1)

def testfunc():
    valid.set("Signed in")
    validtext.update()

What am I doing wrong? :( Thanks for help.

CodePudding user response:

Thank you, @jasonharper. I changed to textvariable=valid and it worked.

The following needed changing:

validtext = tk.Label(root, text=valid.get(), font="Arial").grid(row=5, column=1)

to

validtext = tk.Label(root, textvariable=valid, font="Arial").grid(row=5, column=1)
  • Related