Tkinter Designer

← tkinter how-to

How do I display text in tkinter?

A Label for text you show, a Text widget for text they edit. For one line of output — a result, a status, a caption — it is a Label.

import tkinter as tk

root = tk.Tk()
status = tk.Label(root, text="Ready")
status.pack()

root.mainloop()

Changing the text later

A Label does not track a Python variable. Assigning to the string you passed in changes nothing on screen, because the label copied the value when it was built. Update the widget itself:

status.config(text="Saved")   # or status["text"] = "Saved"

Or let a StringVar do it

Give the label a textvariable and it redraws whenever the variable changes. This is the version to use when several places update the same message.

status_var = tk.StringVar(value="Ready")
status = tk.Label(root, textvariable=status_var)

status_var.set("Saved")   # the label follows

Multi-line and long text

A Label will show newlines, and wraplength (in pixels) makes it wrap. But once the text is long, scrollable, or editable, a Text widget is the right tool — it takes line.column indices rather than a single string.

notes = tk.Text(root, height=8, width=40)
notes.insert("1.0", "Line one\nLine two")
notes.get("1.0", "end-1c")     # "end-1c" trims the newline tkinter adds

Fonts and colour

Both take font=(family, size) — add "bold" as a third item — and fg for the text colour. Note that fg and bg work on classic tkinter widgets but are ignored by the themed ttk ones, which take their colours from a style instead.

tk.Label(root, text="Total", font=("Helvetica", 14, "bold"), fg="#0b3954")

Doing it without the lookup

Text, font, weight and colour are fields in the inspector, and the canvas renders them the way tkinter will. Because the designer knows which widgets are themed, it emits a named ttk style where fg would have been silently ignored.

Lay the window out visually and read the generated Python as you go.

Open the tkinter designer →

Related questions