How to Get Text from a Tkinter Label in Python

In my years of building Python desktop applications, I’ve often found myself needing to grab the text currently displayed on a Label.

Whether you are building a financial dashboard or a simple data entry tool, retrieving that string is a common requirement.

While Labels are primarily meant for displaying information, there are times when your logic needs to know exactly what the user is looking at.

In this tutorial, I will show you exactly how I handle this in my professional projects using a couple of different, reliable methods.

The Challenge with Tkinter Labels

Unlike Entry widgets, which have a very simple .get() method, Labels work a bit differently in the Tkinter framework.

I remember being slightly confused by this when I first started, but once you understand the internal attributes, it becomes second nature.

Method 1: Use the cget() Method

The most direct way I’ve found to get text from a Label is by using the cget() method.

This method allows you to “see” into the configuration of the widget and pull out the value of a specific attribute, like “text.”

I prefer this method when I haven’t used a variable to track the label’s content and just need a quick way to access the string.

Real-World Example: New York Stock Exchange (NYSE) Price Monitor

Let’s look at a scenario where we are monitoring a stock price. We want to “read” the current price displayed on the screen to perform a calculation.

import tkinter as tk
from tkinter import messagebox

def check_price():
    # Here is the magic: using cget to retrieve the text
    current_price = price_label.cget("text")
    
    # Cleaning the string to do some math
    numeric_price = float(current_price.replace('$', ''))
    
    if numeric_price > 150:
        messagebox.showinfo("Trade Alert", f"The price is {current_price}. It's time to sell!")
    else:
        messagebox.showinfo("Trade Alert", f"The price is {current_price}. Continue to hold.")

# Setting up the main window
root = tk.Tk()
root.title("Wall Street Price Tracker")
root.geometry("400x250")

header = tk.Label(root, text="Apple Inc. (AAPL)", font=("Arial", 16, "bold"))
header.pack(pady=10)

# The label we want to get text from
price_label = tk.Label(root, text="$154.25", fg="green", font=("Helvetica", 24))
price_label.pack(pady=20)

# Button to trigger the 'get' logic
btn = tk.Button(root, text="Analyze Current Price", command=check_price)
btn.pack(pady=10)

root.mainloop()

I executed the above example code and added the screenshot below.

Get Text from a Tkinter Label in Python

In the code above, price_label.cget(“text”) goes into the widget and returns the string “$154.25”.

It is a very clean approach because you don’t need to manage any extra variables in your code.

Method 2: Access Text via Dictionary Indexing

Another “pro tip” I’ve picked up over the years is that Tkinter widgets behave a lot like Python dictionaries.

Instead of calling a method, you can simply use the attribute name as a key in a set of brackets.

I use this when I want my code to be a bit more concise, though it does the same thing as cget().

Real-World Example: US Postal Service (USPS) Shipping Label Preview

Imagine you are building a tool for a small business in Chicago to verify shipping labels before they are printed.

import tkinter as tk

def verify_zip_code():
    # Getting text using dictionary-style indexing
    full_address = address_label["text"]
    
    # Checking if the address belongs to California (CA)
    if "CA" in full_address:
        result_label.config(text="Status: West Coast Shipment detected.")
    else:
        result_label.config(text="Status: Standard Domestic Shipment.")

app = tk.Tk()
app.title("USPS Shipping Assistant")
app.geometry("450x300")

# A label containing a full US address
address_label = tk.Label(
    app, 
    text="123 Disney Way, Anaheim, CA 92802", 
    wraplength=300, 
    justify="center",
    font=("Verdana", 12)
)
address_label.pack(pady=30)

check_btn = tk.Button(app, text="Verify Destination", command=verify_zip_code)
check_btn.pack()

result_label = tk.Label(app, text="Status: Waiting...", fg="blue")
result_label.pack(pady=20)

app.mainloop()

I executed the above example code and added the screenshot below.

How to Get Text from a Tkinter Label in Python

Using address_label[“text”] is a very Pythonic way to grab that information quickly.

Method 3: Use a StringVar (The Recommended Way)

While the methods above work great, if I’m building a large-scale application, I almost always use a StringVar.

A StringVar is a special Tkinter object that acts as a wrapper around a Python string. You link it to the Label, and then you interact with the variable instead of the widget itself.

This is the most robust method because it keeps your data logic separate from your UI components.

Real-World Example: US Tax Calculator Feedback

Let’s say we are displaying the calculated Sales Tax for a purchase made in Seattle, Washington.

import tkinter as tk

def update_and_get():
    # Updating the label first
    tax_amount.set("$8.50")
    
    # Retrieving the value from the StringVar, not the Label
    current_val = tax_amount.get()
    print(f"Log: The tax shown to the user is {current_val}")

root = tk.Tk()
root.title("Washington State Tax Tool")
root.geometry("400x200")

# We create the StringVar first
tax_amount = tk.StringVar()
tax_amount.set("$0.00")

label_title = tk.Label(root, text="Calculated Sales Tax (Seattle):")
label_title.pack(pady=10)

# Link the Label to the StringVar using 'textvariable'
display_label = tk.Label(root, textvariable=tax_amount, font=("Courier", 18), fg="red")
display_label.pack(pady=10)

action_btn = tk.Button(root, text="Calculate & Log", command=update_and_get)
action_btn.pack(pady=10)

root.mainloop()

I executed the above example code and added the screenshot below.

Get Text from a Tkinter Label

With this approach, tax_amount.get() is how you retrieve the text. It is extremely reliable.

Common Issues to Avoid

I’ve seen many developers try to use label.get() and get frustrated when it throws an error.

It’s important to remember that get() is not a built-in method for the Label widget class.

Another thing to watch out for is that cget() always returns a string, even if the text looks like a number.

If you are working with US currency or measurements (like inches or miles), always remember to convert the string to a float or integer.

Compare the Methods

I usually choose my method based on the complexity of the project I’m working on at the time.

  • Use cget(): For quick scripts or when you need to grab text from a label you didn’t create yourself.
  • Use Dictionary Indexing: When you want short, readable code for simple labels.
  • Use StringVar: For professional applications where data changes frequently.

In this tutorial, I’ve shown you three different ways to retrieve text from a Tkinter Label.

I’ve used these techniques across countless projects, and they have never let me down.

I recommend starting with cget() for simple tasks and moving to StringVar as your apps get more complex.

You may like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.