How to Create Label in Python & PyQt6

In this lesson we are going to learn How to Create Label in Python & PyQt6, for create of label in PyQt6, we are going to use QLabel class. QLabel class is used for displaying messages, also you can use QLabel class for displaying images, there are different methods that you can use in QLabel class, for example we have setText() and it is used for setting the text, we have setPixamp() method and that is used for setting the image in the QLabel, there are the important methods for QLabel class.

 

 

  • setText(): This method is used for setting the text in the QLabel class
  • setPixmap(): This method is used for setting pixmap, an instance of the QPixmap class, to the Label widget
  • setNum(): This method adds an integer or double value to the Label widget
  • clear(): This method clears text from the Label widget
  • setMovie(): This method is used for setting a gif image in the QLabel

 

 

 

 

This is our code for showing a text using QLabel.

 

 

You can use QLabel class for creating the object of label.

 

 

Also you can use setText() for setting the text of the label.

 

 

Using move() method we can move a widget from one position to another position, you can give the x and y position for the widget.

 

 

 

We can use setFont() to change the text of the label, setFont() expects a QFont class, QFont

class is related to QtGui module, you can give the family of the font and also the size of the font.

 

 

If you want to change the color of the label, than you can use setStyleSheet() method.

 

 

 

Now run the complete code this will be the result.

How to Create Label in Python & PyQt6
How to Create Label in Python & PyQt6

 

 

 

 

As I have already said that you can add images to a label, for that we can use setPixmap() method, Qt provides four classes for handling images we have QImage, QPixmap, QBitmap and  QPicture, in this lesson we are interested in QPixmap.

 

 

 

Run the complete code and this will be the result.

How to Load Image in Python & PyQt6
How to Load Image in Python & PyQt6

 

 

 

There is a setMovie() method that you can add gif images to your label, for that first we need to create the object of QMovie class, QMovie class is used to show simple animations without sound. If you want to display video and media content, use the Qt Multimedia  multimedia framework instead.

 

 

Leave a Comment