Course Content
Python PySide6
About Lesson

In this lesson we want to learn about Python PySide6 QLabel, One of the most commonly used widgets in PySide6 is the QLabel, which can display text and images in a window or dialog. In this lesson, we will discuss how to work with the Python PySide6 QLabel widget to create labels and customize their appearance.


Creating PySide6 QLabel

To create a QLabel in PySide6, we first need to import the QtWidgets module



We can then create a new QLabel object by calling its constructor

This creates a new label with the text “Hello,!”.


We can add this label to our application’s layout using the addWidget() method of a layout object. For example, if we have a QVBoxLayout object called layout, we can add the label to it like this.



We can also set the text of the label using the setText() method

This changes the label’s text to “New text”.



Displaying Images

We can also use a QLabel to display images in our application. To do this, we first need to create a QPixmap object that contains the image



This creates a QPixmap object that contains the image in the file “image.png”. We can then set this pixmap as the label’s image using the setPixmap() method.

This displays the image in the label.



Customizing Appearance

We can also customize the appearance of a QLabel by setting various properties such as the font, color, and alignment. For example, we can set the font of a label using the setFont() method.

This sets the font of the label to Arial with a size of 12.


We can also set the color of the label’s text using the setStyleSheet() method:

This sets the color of the label’s text to red.


We can align the text of the label using the setAlignment() method:

This centers the text of the label within its bounding rectangle.


This is the complete code for Python PySide6 QLabel.

In this example, we create a new MainWindow object that contains two QLabel objects. The first label displays the text “Hello, world!” with a custom font, color, and alignment, while the second label displays an image loaded from the file “image.png”. These labels are added to a QVBoxLayout object, which is then set as the central widget of the main window. When the application is run, the two labels are displayed in the window.



Run the code and this will be the result 

Python PySide6 QLabel
Python PySide6 QLabel


Final Thoughts

PySide6 QLabel widget is a powerful tool for displaying text and images in graphical user interfaces. We can create new labels, set their text and images, and customize their appearance using various properties and methods. With these tools, we can create informative and visually appealing graphical user interfaces that communicate important information to users.