Tkinter: how to change label text
Changing the text on a tk.Label should be easy, right?
Start with importing the tkinter library for python 3 and creating the tk app. The tk.Tk()
object is responsible for going out and speaking to the operating system on your computer and requesting a window with min/max/close buttons, the ability to grab/resize etc. The root
is the name traditionally given to the base object that sits inside this window, and you can only have one base object:
import tkinter as tkroot = tk.Tk()
Now create the label, give it a parent (in this case root
) and add some text:
my_label = tk.Label(root, text="hello, learn-data.org")
… and then pack (easiest of the three layout managers):
my_label.pack()
That’s it for generating the text for a label. To see it in action, fire off the root mainloop:
root.mainloop()
The full code looks like this:
import tkinter as tkroot = tk.Tk() my_label = tk.Label(root, text = "hello, learn-data")
my_label.pack()root.mainloop()
And you should get this:
tk.Label
: Let’s change the text
If you run this bit of code below where you’ve declared the my_label object:
print(help(my_label))
you’ll see that my_label is in fact a Label object (duh), with the following options:
and text
is in there. In fact all standard options can be called up (with the appropriate parameters) along the following lines:
my_label['text'] = "goodbye, cruel world"
We want to test this and see if it will change the text of the label. It would be nice if we could press a button to see the before and after effects. Modify your code as follows:
import tkinter as tkroot = tk.Tk() my_label = tk.Label(root, text = "hello, learn-data")
my_label.pack()my_btn = tk.Button(root, text = "press to change", command = change_text)
my_btn.pack()root.mainloop()
Depending on what editor you’re using you should see change_text
is highlighted. This is because the code will look for a function with that name – and there isn’t one. Add this change_text()
function anywhere above the my_btn
line ( I usually keep my functions near the top of the code):
import tkinter as tkdef change_text():
my_label['text'] = "goodbye, cruel world"root = tk.Tk() my_label = tk.Label(root, text = "hello, learn-data")
my_label.pack()my_btn = tk.Button(root, text = "press to change", command = change_text)
my_btn.pack()root.mainloop()
The second way to change label text is to use config
(short for configure
):
def change_text():
my_label.config(text = "goodbye, cruel world")
This works just like before.
The third way is to pull out the text as a string variable. It’s a little more complicated, but gives you more options down the road. Notice you have to change text
to textvariable
:
my_text = tk.StringVar()
my_text.set('hello, learn-data')
my_label = tk.Label(root, textvariable = my_text)
And you would change it by using .set(‘text’) as follows:
def change_text():
my_text.set('goodbye, cruel world')
Full code here:
import tkinter as tkdef change_text():
my_text.set('goodbye, cruel world')root = tk.Tk() my_text = tk.StringVar()
my_text.set('hello, learn-data')
my_label = tk.Label(root, textvariable = my_text)
my_label.pack()my_btn = tk.Button(root, text = "press to change", command = change_text)
my_btn.pack()root.mainloop()
(Any use? claps are nice, or you can comment/ask questions…)