How To Align Text In Tkinter Label?

Aligning text within Tkinter labels is a fundamental aspect of GUI (Graphical User Interface) design in Python. Tkinter provides various options for aligning text within labels to enhance the visual presentation of your application. In this article, we’ll explore different methods to align text in Tkinter labels in Python.

The Label Widget

The Tkinter Label widget is used to display text or images. It is a versatile widget that supports various attributes for customizing its appearance and behavior. Some of the key attributes of a Label widget include text, image, background, height, width, etc.

The syntax for creating a label in Tkinter is as follows:

label_name = tkinter.Label(parent,text="text_to_display")

Aligning Text in Tkinter Label

Text alignment in a Tkinter Label can be controlled using the anchor attribute. The anchor attribute accepts a string value that specifies the position of the text within the Label. The values correspond to compass directions such as n, ne, e, see, s, sw, w, nw, and center. The center alignment is the default setting of the Tkinter label.

Additionally, horizontal text alignment can be controlled using the justify attribute, which can be set to LEFT, CENTER, or RIGHT.

Now let us see a few different examples to better understand to align text in Tkinter label.

Using the justify Attribute

The justify parameter in the Tkinter Label widget allows you to specify the horizontal alignment of text within the label. By default, text is left-aligned. However, you can set it to ‘left’, ‘center’, or ‘right’ based on your requirements.

Python
# import tkinter module
from tkinter import Tk, Label

# create main window
root = Tk()

# Left-aligned text
label_left = Label(root, text="Left Aligned Text", justify="left")
label_left.pack(pady=10, padx=10, anchor="w")

# Center-aligned text
label_center = Label(root, text="Center Aligned Text", justify="center")
label_center.pack(pady=10, padx=50, anchor="w")

# Right-aligned text
label_right = Label(root, text="Right Aligned Text", justify="right")
label_right.pack(pady=10, padx=100, anchor="w")

root.mainloop()

Output:

Label text alignment using Justify attribute

Using the anchor Attribute

The anchor parameter in the Tkinter Label widget allows you to specify the point within the label where the text should be anchored. This parameter is particularly useful when dealing with labels of varying sizes.

Python
# import tkinter module
from tkinter import Tk, Label

# create main window
root = Tk()

# Anchoring text to the west (left)
label_west = Label(root, text="Left Aligned Text", anchor="w")
label_west.pack(pady=10, padx=10, anchor="w")

# Anchoring text to the center
label_center = Label(root, text="Centre Aligned Text", anchor="center")
label_center.pack(pady=10, padx=50, anchor="w")

# Anchoring text to the east (right)
label_east = Label(root, text="Right Aligned Text", anchor="e")
label_east.pack(pady=10, padx=100, anchor="w")

root.mainloop()

Output:

Label alignment using Anchor attribute

Using the padx and pady Attributes

You can achieve text alignment within Tkinter labels by adjusting the padding around the text. By adding padding on the left and right sides (padx) or top and bottom sides (pady), you can effectively align the text within the label.

Python
# import tkinter module
from tkinter import Tk, Label

# create main window
root = Tk()

# Adding horizontal padding to center the text
label_padx = Label(root, text="Centered Text with Padding", padx=20)
label_padx.pack(pady=10, padx=10, anchor="w")

root.mainloop()

Output:

Label alignment using pady and padx



Contact Us