Changing Tkinter Label Text Dynamically using Label.configure()
If you’re building a desktop application with Python’s Tkinter, you may need to change the text displayed on your labels dynamically. Fortunately, Tkinter provides a way to update the text on a label without having to destroy and recreate it. In this tutorial, you’ll learn how to use the Label.configure()
method to update your labels at runtime.
The Label widget
First, let’s review the basics of the Label
widget. The Label
widget is used to display one or more lines of text or an image. You can set the initial text for your label when you create it by passing a text
parameter:
import tkinter as tk
root = tk.Tk()
label = tk.Label(root, text="Hello, World!")
label.pack()
root.mainloop()
In this example, we create a new Tk
instance, create a Label
widget with the text “Hello, World!”, and then pack the label into the window using label.pack()
. Note that when you create a Label
widget, it’s important to specify its parent widget as the first parameter. In this case, we set root
as the parent widget.
Updating the label text
To update the text of a Label
widget dynamically, we’ll use the configure()
method. The configure()
method allows you to change the value of any option for a widget. In the case of the Label
widget, the option we want to change is text
.
Here’s an example that updates the text of the label to “Hello, Tkinter!” when a button is clicked:
import tkinter as tk
root = tk.Tk()
def change_text():
label.configure(text="Hello, Tkinter!")
label = tk.Label(root, text="Hello, World!")
label.pack()
button = tk.Button(root, text="Click me!", command=change_text)
button.pack()
root.mainloop()
In this example, we create a new function named change_text()
that changes the text of the label
widget using label.configure(text="Hello, Tkinter!")
. We then create a new button widget with the text “Click me!” and set its command
parameter to change_text
. This means that when the button is clicked, the change_text()
function will be called, and the label’s text will be updated.
Updating the label text dynamically
Now that we know how to update the text of a label, let’s look at a more practical example. Suppose we have a program that reads data from a sensor and displays it on a label. We want to update the label’s text dynamically as new sensor readings come in.
Here’s an example that simulates sensor data and updates the label’s text once per second:
import tkinter as tk
import random
import time
root = tk.Tk()
def update_label():
value = random.uniform(0, 100)
label.configure(text=f"Sensor reading: {value:.2f}")
root.after(1000, update_label)
label = tk.Label(root, text="Sensor reading: 0.00")
label.pack()
update_label()
root.mainloop()
In this example, we create a new function named update_label()
that generates a random sensor reading and updates the label’s text with the new value. We then use the after()
method to schedule the update_label()
function to be called once per second. This way, our label’s text is updated dynamically with new sensor readings.
Note that we use f-strings
to format the label’s text with the sensor reading. f-strings
are a convenient way to embed values in strings in Python 3.6 and later.
Conclusion
In this tutorial, you learned how to use the Label.configure()
method to update the text of a Label
widget dynamically. This can be useful for many different types of applications, from sensor monitoring to chat applications. With these techniques, you can build responsive, dynamic UIs using Python’s Tkinter library.