PyQt5
module is used to create GUI applications.
We will learn to add an image to the PyQt5
window in this shot.
PyQt5
module with pip.pip install pyqt5
QPixmap()
, which will accept the image path as a parameter and returns an instance of QPixmap
.pixmap = QPixmap('nature.png')
Qpixmap
instance to label.label.setPixmap(pixmap)
The following code snippet will create a PyQt5
window and load an image on it:
Window
class.window
title. It will be displayed on top of the window
.window
size, here the width is 500
and height is 500
.QLabel
and assign it to the label variable.QPixmap()
, by passing the image path as a parameter and assigning the instance to pixmap
.pixmap
to label.from PyQt5.QtWidgets import *from PyQt5.QtGui import QPixmapimport sysclass Window(QMainWindow):def __init__(self):super().__init__()self.acceptDrops()# set window titleself.setWindowTitle("Load Image on Window")# set window sizeself.setGeometry(0, 0, 500, 500)# Create an instance of labelself.label = QLabel(self)# Load imageself.pixmap = QPixmap('nature.png')# Set image to labelself.label.setPixmap(self.pixmap)# Resize the label according to image sizeself.label.resize(self.pixmap.width(),self.pixmap.height())# show the widgetsself.show()# Instantiate pyqt5 applicationApp = QApplication(sys.argv)# create window instancewindow = Window()# start the appsys.exit(App.exec())