In this lesson we are going to learn How to Show Image in Python & PyQt6, for showing image in python and pyqt6 we are going to use QLabel. 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.
- How to Use Qt Designer in PyQt and PyQt6
- Build Text to Speech App with Python & PyQt5
- How to Build GUI Window in PyQt6
This is the complete code for this lesson.
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()) |
For showing image in PyQt6, Qt provides four classes for handling images we have
QImage, QPixmap, QBitmap and QPicture, in this lesson we are interested in
QPixmap, and we have used that in here, make sure that you have already added
an image in your working director.
1 |
pixmap = QPixmap('python.png') |
Run the complete code and this will be the result.
