How To Align Text In Tkinter Label?
Last Updated :
07 Jun, 2024
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 attributeUsing 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 attributeUsing 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
Similar Reads
How to Get the Tkinter Label Text?
Prerequisite: Python GUI â Tkinter Python offers multiple options for developing a GUI (Graphical User Interface). Out of all the GUI methods, tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with tkinter is the fastest and
2 min read
How to change the Tkinter label text?
Prerequisites: Introduction to tkinter Tkinter is a standard GUI (Graphical user interface) package for python. It provides a fast and easy way of creating a GUI application. To create a tkinter application: Importing the module â tkinterCreate the main window (container)Add any number of widgets to
3 min read
How To Make A Label Bold Tkinter?
Tkinter has various customization settings that allow you to modify the appearance of GUI elements, including labels. Making a label bold can be achieved using different methods, such as configuring the font directly or using styles. In this article, we will explore different approaches to make a la
2 min read
How to set font for Text in Tkinter?
Tkinter allows you to customize the appearance of text in various widgets. Customizing the font helps in enhancing the readability and user experience of the application interface. Tkinter offers several ways to set fonts, whether you prefer to define a font once for reuse or specify it for individu
3 min read
How To Print Entry Text Tkinter?
Tkinter is a popular GUI library in Python that allows developers to create desktop applications. One common task is retrieving and displaying the text entered by users. In this article, we'll go through the steps to create a simple Tkinter application that prints the text from an Entry widget in Py
4 min read
How to bind all the number keys in Tkinter?
In this article, we will discuss how to bind all the number keys in Tkinter. Key Binding helps you to create complex GUI applications where you bind some specific key to functions, that executes when that key is pressed. Syntax: def key_press(a):  Label(app, text="You have pressed: " + a.char, font
2 min read
How to Change the Tkinter Label Font Size?
In Tkinter, labels are used to display text but adjusting their font size can improve readability or match a specific design. Tkinter offers multiple ways to modify a labelâs font size. Letâs explore different methods to achieve this.Using tkFont.Font()tkinter.font.Font() class allows you to define
3 min read
PyQt5 â How to align Text of Label
In this article, we will see how we can align text of labels in PyQt5 application, we can align text in three different ways which are left, right and Center.Syntax :Â Â label.setAlignment(QtCore.Qt.AlignLeft) label.setAlignment(QtCore.Qt.AlignCenter) label.setAlignment(QtCore.Qt.AlignRight) In order
2 min read
How To Add An Image In Tkinter?
Python Tkinter supports various image formats such as PNG, JPEG, GIF, and BMP. Images are handled in Tkinter using the PhotoImage and BitmapImage classes and the Pillow Library. The PhotoImage class is more commonly used due to its support for color images, whereas BitmapImage is limited to monochro
4 min read
How To Change The Title Bar In Tkinter?
Changing the title bar in Tkinter is a fundamental aspect of customizing your application's window. The title bar typically displays the name of the window and can be modified to reflect the content or functionality of your application. Here, we'll explore three different approaches to change the ti
2 min read