Tkinter Label – Center align text

Tkinter Label – Center align text

You can center align the text in a Label widget in Tkinter as shown in the following screenshot.

Tkinter Label - Center align text
Tkinter Label – Center align text

To center align the text in a Label widget in Tkinter, set the anchor parameter to “center”.

The syntax to specify the anchor parameter in a Label widget constructor, so that the text is center aligned, is as shown in the following.

tk.Label(anchor="center")

In this tutorial, you will learn how to center align the text in a Label widget in Tkinter, with examples.

Examples

1. Center align text in a Label widget

In this example, we shall create a Label widget with some text, and set the alignment of the text to center.

We have set the background color and width, so that we can visually check if the text is actually center aligned or not.

Program

import tkinter as tk

# Create the main window
window = tk.Tk()
window.title("PythonExamples.org")
window.geometry("300x200")

# Create a label widget
label = tk.Label(
    window,
    text="Hello World!",
    anchor="center",
    width=25,
    bg="lightgreen"
)

# Pack the label widget to display it
label.pack()

# Run the application
window.mainloop()

Output

Run on Windows 10.

Tkinter Label – Center align text

Run on MacOS.

Tkinter Label - Center align text - Example output
Tkinter Label – Center align text – Example output

Summary

In this Python Tkinter tutorial, we learned how to center align text in a Label widget, with examples.

Related Tutorials

Code copied to clipboard successfully 👍