按下按钮PyQt5时如何在标签中加载图像

时间:2019-02-17 17:53:04

标签: python python-3.x pyqt5

我是PyQt编程的新手,当我运行此代码时,按下按钮始终不会响应窗口。

和第二个问题,当我按下按钮时如何在标签中加载图像,我花了3个小时来问一个问题,但我没有得到答案:(( 如何解决它,对不起我的英语不好

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

class ShowImage (QMainWindow):
    def __init__(self):
        super(ShowImage,self).__init__()
        loadUi('ganteng.ui',self)
        self.image=None
        self.loadButton.clicked.connect(self.loadClicked)

@pyqtSlot()
def loadClicked(self):
    self.loadImage('2.jpg',cv2.IMREAD_GRAYSCALE)

def loadImage(self,Flname):
    self.image=cv2.imread(flname)
    self.displayImage()

def displayImage(self):
    qformat=QImage.Format_Indexed8

    if len(self.image.shape)==3:
        if (self.image.shape[2])==4:
            qformat=QImage.Format_RGBA8888

        else:
            qformat=QImage.Format_RGB888

            img=QImage(self.image,self.image.shape[1],self.image.shape[0],self.image.strides[0],qformat)
            img=img.rgbSwapped()

            self.imgLabel.serPixmap (QPixmap.fromImage(img))
            self.imgLabel.setAlignment (qtCore.Qt.AlignHCenter|QtCore.Qt.AlignVCenter)

app=QtWidgets.QApplication(sys.argv)
window=ShowImage()
window.setWindowTitle('gambar')
window.show()
app.exec_()

this the ui window

1 个答案:

答案 0 :(得分:0)

一切都很好,错别字。细心:)

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

class ShowImage (QMainWindow):
    def __init__(self):
        super().__init__()

        loadUi('ganteng.ui',self)
        self.image = None
        self.loadButton.clicked.connect(self.loadClicked)

    @pyqtSlot()
    def loadClicked(self):
        self.loadImage('head.jpg', cv2.IMREAD_GRAYSCALE)

    def loadImage(self, flname, cv ):                  # Flname <-> flname;  + , cv
        self.image = cv2.imread(flname)
        self.displayImage()

    def displayImage(self):
        qformat = QImage.Format_Indexed8

        if len(self.image.shape) == 3:
            if (self.image.shape[2]) == 4:
                qformat = QImage.Format_RGBA8888
            else:
                qformat = QImage.Format_RGB888

                img = QImage(self.image,
                             self.image.shape[1],
                             self.image.shape[0],
                             self.image.strides[0],
                             qformat)
                img = img.rgbSwapped()

                self.imgLabel.setPixmap(QPixmap.fromImage(img))      # serPixmap <-> setPixmap
                self.imgLabel.setAlignment(QtCore.Qt.AlignHCenter    # qtCore    <-> QtCore
                                         | QtCore.Qt.AlignVCenter)

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    window = ShowImage()
    window.setWindowTitle('gambar')
    window.show()
    sys.exit(app.exec_())

enter image description here