972
In this article we will look at the PyQt QPixmap widget.
A QPixmap can be used to show an image in a PyQT window. QPixmap() can load an image, as a parameter it has the filename. The supported image types will be in the table below
You then put the pixmap into the QLabel widget to display it
Supported Image Types
These are the supported image formats
BMP |
GIF |
JPG |
JPEG |
PNG |
PBM |
PGM |
PPM |
XBM |
XPM |
Lets look at some of the methods that are available for the QCheckBox widget
Methods
Methods and Description |
---|
copy() – Copies pixmap data from a QRect object |
fromImage() – Converts QImage object into QPixmap |
grabWidget() – Creates a pixmap from the given widget |
grabWindow() – Create pixmap of data in a window |
Load() – Loads an image file as pixmap |
save() – Saves the QPixmap object as a file |
toImage – Converts a QPixmap to QImage |
Examples
Lets look at a basic example
import sys from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout from PyQt5.QtGui import QPixmap from PyQt5.QtCore import Qt class MyApp(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): pixmap = QPixmap('python.png') labelImg = QLabel() labelImg.setPixmap(pixmap) #this makes the label the size of the image labelSize = QLabel('Width: '+str(pixmap.width())+', Height: '+str(pixmap.height())) labelSize.setAlignment(Qt.AlignCenter) vbox = QVBoxLayout() vbox.addWidget(labelImg) vbox.addWidget(labelSize) self.setLayout(vbox) self.setWindowTitle('QPixmap Example') self.move(300, 300) self.show() if __name__ == '__main__': app = QApplication(sys.argv) ex = MyApp() sys.exit(app.exec_())
This displayed the following