SoFunction
Updated on 2025-03-03

Guide to using QLabel Tag Components in PyQt

In PyQt,QLabelis a basic component for displaying text or images. It is usually used to display static information or as a tag for other components. This article will introduce in detailQLabelBasic usage and some advanced features.

Basic usage

First, we need to import the necessary modules and create a simpleQLabel

import sys
from  import QApplication, QWidget, QLabel
class MyWindow(QWidget):
    def __init__(self):
        super().__init__()
        ()
    def initUI(self):
        (300, 300, 300, 200)
        ('QLabel Example')
        # Create a tag        label = QLabel('This is a tag', self)
        (100, 80)
        ()
if __name__ == '__main__':
    app = QApplication()
    window = MyWindow()
    (app.exec_())

Show image

QLabelNot only can you display text, but you can also display images.

from  import QPixmap
class MyWindow(QWidget):
    def __init__(self):
        super().__init__()
        ()
    def initUI(self):
        (300, 300, 300, 200)
        ('QLabel display image example')
        # Create a tag and display an image        label = QLabel(self)
        pixmap = QPixmap('path/to/')
        (pixmap)
        ((), ())
        ()
if __name__ == '__main__':
    app = QApplication()
    window = MyWindow()
    (app.exec_())

Text formatting

You can use HTML tags to formatQLabeltext in  .

('<h1>This is a title</h1><p>This is a paragraph text.  </p>')

Alignment

You can setQLabelThe alignment of Chinese text.

()  # Center Align()   # Left Alignment()  # Right aligned

Automatic line wrap

You can setQLabelWhether to wrap the line automatically.

(True)  # Turn on automatic line wrap

Stylesheet

You can customize by setting style sheetsQLabelAppearance.

("""
    QLabel {
        color: #333;
        font-size: 16px;
    }
""")

Summarize

QLabelis a very practical component in PyQt, suitable for displaying various static information. By using HTML tags, you can easily format text. Custom stylesheets can make your application more beautiful and professional. I hope this article can help you better understand and use itQLabel

This is the article about the guide to using QLabel Tag Components in PyQt. For more related content on using QLabel Tag Components, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!