Skip to content
Related Articles

Related Articles

Improve Article

PyQt5 – How to align Text of Label

  • Last Updated : 26 Mar, 2020

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 :


In order to use use this we have to import Qtcore from PyQt5

from PyQt5 import QtCore

Below is the implementation :

# importing the required libraries
from PyQt5.QtWidgets import * 
from PyQt5 import QtCore
from PyQt5.QtGui import * 
import sys
class Window(QMainWindow):
    def __init__(self):
        # set the title
        # setting  the geometry of window
        self.setGeometry(0, 0, 400, 300)
        # creating a label widget
        self.label_1 = QLabel('Left', self)
        # moving position
        self.label_1.move(100, 100)
        # setting up border
        self.label_1.setStyleSheet("border: 1px solid black;")
        # setting alignment to left
        # creating a label widget
        self.label_2 = QLabel('Centre', self)
        # moving position
        self.label_2.move(100, 130)
        # setting up border
        self.label_2.setStyleSheet("border: 1px solid black;")
        # setting alignment to centre
        # creating a label widget
        self.label_3 = QLabel('Right', self)
        # moving position
        self.label_3.move(100, 160)
        # setting up border
        self.label_3.setStyleSheet("border: 1px solid black;")
        # setting alignment to right
        # show all the widgets
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app

Output :

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :