Learn Python PyQt

PyQt label

The main functions for the QLabel control class are as follows.

  • setAlignment(): Set the alignment of the text
  • setIndent(): set text indentation
  • text(): Get text content
  • setText(): set text content
  • selectedText(): returns the selected character
  • setBuddy(): setting partnerships
  • setWordWrap (): set whether to allow line feed

Signals (events) commonly used in QLabel.

  1. Triggered when the mouse slides over the QLabel control: linkHovered
  2. Triggered when the mouse clicks on the QLabel control: linkActivated

A QLabel displays text or images

Book: Create Desktop Apps with Python PyQt5

QLabel example

For example, the following demo shows the use of QLabel. A QLabel can be a plain label, contain links, contain colored text and even contain images.

pyqt qlabel example

Qt’s controls support HTML syntax and can control display effects with HTML code.

import sys
from PyQt5.QtWidgets import QVBoxLayout,QMainWindow,QApplication,QLabel,QWidget
from PyQt5.QtGui import QPixmap, QPalette
from PyQt5.QtCore import Qt

class QLabelDemo(QWidget) :
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        label1 = QLabel(self)
        label2 = QLabel(self)
        label3 = QLabel(self)
        label4 = QLabel(self)

        label1.setText("<font color=yellow>PythonPyQt.com</font>")
        label1.setAutoFillBackground(True)
        palette = QPalette()
        palette.setColor(QPalette.Window,Qt.blue)
        label1.setPalette(palette)
        label1.setAlignment(Qt.AlignCenter)

        label2.setText("<a href='#'>QLabel2</a>")

        label3.setAlignment(Qt.AlignCenter)
        label3.setToolTip('Hint')
        label3.setPixmap(QPixmap("python.png"))

        label4.setOpenExternalLinks(True)
        label4.setText("<a href='https://python.org'>Python.org</a>")
        label4.setAlignment(Qt.AlignCenter)
        label4.setToolTip('Python.org')

        vbox = QVBoxLayout()

        vbox.addWidget(label1)
        vbox.addWidget(label2)
        vbox.addWidget(label3)
        vbox.addWidget(label4)

        label2.linkHovered.connect(self.linkHovered)
        label4.linkActivated.connect(self.linkClicked)

        self.setLayout(vbox)
        self.setWindowTitle('QLabel Example')

    def linkHovered(self):
        print('Link hovered')

    def linkClicked(self):
        print('Link clicked')

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = QLabelDemo()
    main.show()
    sys.exit(app.exec_())

QLabel setup partner control

In the demo below, use alt + N to set the cursor for nameEdit and alt + p to set the cursor for password.
It shows two labels (QLabel) and buttons.

pyqt qlabel
from PyQt5.QtWidgets import *
import sys

class QLabelBuddy(QDialog) :
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle('QLabel Example')
        self.setFixedWidth(250)

        nameLabel = QLabel('&Name',self)
        nameLineEdit = QLineEdit(self)
        nameLabel.setBuddy(nameLineEdit)

        passwordLabel = QLabel('&Password',self)
        passwordLineEdit = QLineEdit(self)
        passwordLabel.setBuddy(passwordLineEdit)

        btnOK = QPushButton('&OK')
        btnCancel = QPushButton('&Cancel')

        mainLayout = QGridLayout(self)
        mainLayout.addWidget(nameLabel,0,0)
        mainLayout.addWidget(nameLineEdit,0,1,1,2)

        mainLayout.addWidget(passwordLabel,1,0)
        mainLayout.addWidget(passwordLineEdit,1,1,1,2)

        mainLayout.addWidget(btnOK,2,1)
        mainLayout.addWidget(btnCancel,2,2)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = QLabelBuddy()
    main.show()
    sys.exit(app.exec_())

Book: Create Desktop Apps with Python PyQt5