首页 > 解决方案 > 如何在pyqt5的Qlabel中显示图像

问题描述

我正在尝试使用标签在 pyqt5 中显示图像,但是当我单击按钮时,图像未显示。而gui正在接近。

这是我的代码

import sys
import cv2
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QApplication ,QDialog
from PyQt5.uic import loadUi
from PyQt5.QtGui import QImage,QPixmap
from PyQt5 import QtCore

class anyname(QDialog):
        def __init__(self):
                        super(anyname,self).__init__()
                        loadUi('image.ui',self)
                        self.SHOW.clicked.connect(self.onClicked)
                        self.TEXT.setText('kindly press show button')

        @pyqtSlot()     
        def onClicked(self):
                img= cv2.imread('E:/photoshopimages/images.png')
                self.displayImage(img,1)
                cv2.destroyAllwindows()
        def displayImage(self, img,window=1):
                qformat = QImage.Format_Indexed8

                if len(img.shape) == 3:
                        if (img.shape[2]) == 4:
                                qformat=QImage.Format_RGBA888

                        else:
                                qformat=QImage.Format_RGB888
                img =QImage(img, img.shape[1],img.shape[0],qformat)
                img =img.rgbSwapped()
                self.imgLabel.setPixmap(QPixmap.fromImage(img))
                self.imgLabel.setAlignment(QtCore.Qt.AlignHCenter | Qtcore.Qt.AlignVCenter)

app=QApplication(sys.argv)
window=anyname()
window.show()
try:
    sys.exit(app.exec_())
except:
    print("exiting")

标签: pythonpython-3.xuser-interfacepyqt5qlabel

解决方案


推荐阅读