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
- How to Use Qt Designer in PyQt and PyQt6
- Build Text to Speech App with Python & PyQt5
- How to Build GUI Window in PyQt6
- How to Convert UI file to PY File
- How to Load Qt Designer UI File
This is our code for showing a text using QLabel.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
from PyQt6.QtWidgets import QApplication, QWidget, QLabel from PyQt6.QtGui import QIcon, QFont import sys class Window(QWidget): def __init__(self): super().__init__() self.setGeometry(200, 200, 700, 400) self.setWindowTitle("Python QLabel") self.setWindowIcon(QIcon('python.png')) label = QLabel("Python GUI Development - Geekscoders.com", self) #label.setText("New Text is Here") #label.move(100,100) label.setFont(QFont("Sanserif", 15)) label.setStyleSheet('color:red') #label.setText(str(12)) #label.setNum(15) #label.clear() app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec()) |
You can use QLabel class for creating the object of label.
1 |
label = QLabel("Python GUI Development - Geekscoders.com", self) |
Also you can use setText() for setting the text of the label.
1 |
label.setText("New Text is Here") |
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.
1 |
label.move(100,100) |
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.
1 |
label.setFont(QFont("Sanserif", 15)) |
If you want to change the color of the label, than you can use setStyleSheet() method.
1 |
label.setStyleSheet('color:red') |
Now run the complete code this will be the result.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
from PyQt6.QtWidgets import QApplication, QWidget, QLabel from PyQt6.QtGui import QIcon, QFont, QPixmap, QMovie, QRegion from PyQt6.QtCore import Qt import sys class Window(QWidget): def __init__(self): super().__init__() self.setGeometry(200, 200, 700, 400) self.setWindowTitle("Python QLabel") self.setWindowIcon(QIcon('qt.png')) label = QLabel(self) pixmap = QPixmap('python.png') label.setPixmap(pixmap) app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec()) |
Run the complete code and this will be the result.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
from PyQt6.QtWidgets import QApplication, QWidget, QLabel from PyQt6.QtGui import QIcon, QMovie import sys class Window(QWidget): def __init__(self): super().__init__() self.setGeometry(200, 200, 700, 400) self.setWindowTitle("Python QLabel") self.setWindowIcon(QIcon('python.png')) label = QLabel(self) movie = QMovie('sky.gif') movie.setSpeed(500) label.setMovie(movie) movie.start() app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec()) |