PyQt5 Tutorial - Label

  1. PyQt5 Label Widget
  2. PyQt5 QLabel Set Font
  3. PyQt5 Label Alignment

We will learn to use PyQt5 label widget QLabel in this tutorial.

PyQt5 Label Widget

We’re going to add two labels to our window, where one of those labels is going to hold some text and one of those labels is going to hold an image.

QLabel.py
import sys
from PyQt5 import QtWidgets, QtGui

def basicWindow():
    app = QtWidgets.QApplication(sys.argv)
    windowExample = QtWidgets.QWidget()
    labelA = QtWidgets.QLabel(windowExample)
    labelB = QtWidgets.QLabel(windowExample)
    labelA.setText('Label Example')
    labelB.setPixmap(QtGui.QPixmap('python.jpg'))
    windowExample.setWindowTitle('Label Example')
    windowExample.setGeometry(100, 100, 300, 200)
    labelA.move(100, 40)
    labelB.move(120, 120)
    windowExample.show()
    sys.exit(app.exec_())

basicWindow()

Where,

labelA = QtWidgets.QLabel(w)

The first label labelA is a QtWidgets.QtLabel and the QtWidgets- w is in parentheses because it tells the program that the label labelA is added to the window w.

labelA.setText('Label Example')

labelA.setText sets the text in the label.

windowExample.setGeometry(100, 100, 300, 200)

It sets the window size to be (300, 200) and left upper corner coordinate to be (100, 100). You could refer to the setGeometry explanation in last section.

labelA.move(100, 40)
labelB.move(120, 120)

move() method moves the label to the direction of right and down. Like labelA.move(100, 40) moves the labelA to the coordinate of (100, 40) relative to the left-upper corner of the window.

labelB.setPixmap(QtGui.QPixmap('globe.png'))

It displays images in the labelB. QPixmap is the module in QtGui and it takes an image of Qt.

PyQt5 Label

PyQt5 QLabel Set Font

In the example above, we use the default font in label widget, and you could specify the preferred font like size, weight and font family of the label text.

QLabel_Set Font.py
import sys
from PyQt5 import QtWidgets, QtGui

def basicWindow():
    app = QtWidgets.QApplication(sys.argv)
    windowExample = QtWidgets.QWidget()
    labelA = QtWidgets.QLabel(windowExample)
    labelB = QtWidgets.QLabel(windowExample)
    labelA.setText('Times Font')
    labelA.setFont(QtGui.QFont("Times", 12, QtGui.QFont.Bold))
    labelB.setText('Arial Font')
    labelB.setFont(QtGui.QFont("Arial", 14, QtGui.QFont.Black))
    windowExample.setWindowTitle('Label Example')
    windowExample.setGeometry(100, 100, 300, 200)
    labelA.move(100, 40)
    labelB.move(100, 120)
    windowExample.show()
    sys.exit(app.exec_())

basicWindow()
labelA.setFont(QtGui.QFont("Times", 12, QtGui.QFont.Bold))

setFont() method sets the font of the label. The QFont class specifies a font with specific attributes.

PyQt5 Label Set Font

PyQt5 Label Alignment

The label text is by default aligned to the left edge, and this property could be modified with the method QLabel.setAlignment().

import sys
from PyQt5 import QtWidgets, QtGui, QtCore

def basicWindow():
    app = QtWidgets.QApplication(sys.argv)
    windowExample = QtWidgets.QWidget()

    labelLeft = QtWidgets.QLabel(windowExample)
    labelRight = QtWidgets.QLabel(windowExample)
    labelCenter = QtWidgets.QLabel(windowExample)

    labelLeft.setText('Left Align')
    labelRight.setText('Right Align')
    labelCenter.setText('Center Align')

    windowExample.setWindowTitle('Label Align Example')
    windowExample.setGeometry(100, 100, 300, 200)

    labelLeft.setFixedWidth(160)
    labelRight.setFixedWidth(160)
    labelCenter.setFixedWidth(160)

    labelLeft.setStyleSheet("border-radius: 25px;border: 1px solid black;")
    labelRight.setStyleSheet("border-radius: 25px;border: 1px solid black;")
    labelCenter.setStyleSheet("border-radius: 25px;border: 1px solid black;")

    labelLeft.setAlignment(QtCore.Qt.AlignLeft)
    labelRight.setAlignment(QtCore.Qt.AlignRight)
    labelCenter.setAlignment(QtCore.Qt.AlignCenter)
    
    labelLeft.move(80, 40)
    labelRight.move(80, 80)
    labelCenter.move(80, 120)    
    
    windowExample.show()
    sys.exit(app.exec_())

basicWindow()
labelLeft.setFixedWidth(160)
labelRight.setFixedWidth(160)
labelCenter.setFixedWidth(160)

It sets the fixed width to the three labels, otherwise, the label width is automatically set according to the label text length.

labelLeft.setStyleSheet("border-radius: 25px;border: 1px solid black;")

We could use CSS-alike style sheet to set the styles of the PyQt5 widgets. Here, the border of the label is set to be solid black with the border with of 1px, and the border radius is specified as 25px.

labelLeft.setAlignment(QtCore.Qt.AlignLeft)
labelRight.setAlignment(QtCore.Qt.AlignRight)
labelCenter.setAlignment(QtCore.Qt.AlignCenter)

The alignment property of the widget is set by the method setAlignment and its options are in the module PyQt5.QtCore.Qt, like

  • PyQt5.QtCore.Qt.AlignLeft
  • PyQt5.QtCore.Qt.AlignRight
  • PyQt5.QtCore.Qt.AlignCenter

As you could see from the image below, the labels are aligned according to their alignment properties.

PyQt5 Label Alignment

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.