Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialmajesticcat123
3,020 PointsHow to update a tkinter Label()?
I'm working on a pretty complicated program for a prototype of an app. But I am not so great with tkinter, and because I was having a couple of problems with updating Label()
objects, I made a little program to try that. My code for that little program is:
from tkinter import *
def change_func():
labeltext.set("yep done")
window = Tk()
labeltext = StringVar()
labeltext.set("change meh")
label = Label(textvariable=labeltext)
button = Button(text="change", command=change_func())
label.pack()
button.pack()
window.mainloop()
However, when I run the program, the window that comes up just says yep done
above the button saying changed
. I haven't clicked the button at all. I have tried many different methods that I found online to change the value, none have worked yet.
On my more complicated program, the final value of the Label()
that I am changing the text of is not defined until you click a button to define it, and it shows the original value no matter what you do. Please can someone help!
1 Answer
Jeff Muday
Treehouse Moderator 28,720 PointsYou're very close.
The button needs to be passed the function reference (e.g. without parenthesis) rather than the return value of the function. Change the line like below and it will work like a charm!
button = Button(text="change", command=change_func )