Home » PyQt QPixmap widget

PyQt QPixmap widget

Java SE 11 Developer (Upgrade) [1Z0-817]
Spring Framework Basics Video Course
Oracle Java Certification
1 Year Subscription
Java SE 11 Programmer I [1Z0-815] Practice Tests
Java SE 11 Programmer II [1Z0-816] Practice Tests

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

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More