我是PyQt编程的新手。当我没有按下灰色按钮时,一切看起来都很正常,但是当我添加self.GrayButton.clicked.connect(self.grayClicked)
时,主窗口不再响应。我不知道我的代码有什么问题。
我的GUI错误还是我的代码错误?
按钮名称列表:
我的代码:
import sys
import cv2
import numpy as np
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('yugi.ui',self)
self.image = None
self.loadButton.clicked.connect(self.loadClicked)
self.GrayButton.clicked.connect(self.grayClicked)
@pyqtSlot()
def loadClicked(self):
self.loadImage('8.jpg', cv2.IMREAD_GRAYSCALE)
def loadImage(self, flname, cv ): # Flname <-> flname; + , cv
self.image = cv2.imread(flname)
self.displayImage()
def grayClicked(self, np):
H, W = self.image.shape[:2]
gray = np.zeros((H, W), np.uint8)
for i in range(H):
for j in range(W):
gray[i,j]= np.clip(0.299 * self.image[i, j, 0] + 0.587 *
self.image[i, j, 1] + 0.114 * self.image[i, j, 2], 0, 255)
self.image=gray
self.displayImage(2)
def displayImage(self,windows=1):
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 window==1:
self.imgLabel.setPixmap(QPixmap.fromImage(img))
self.imgLabel.setAligment(QtCore.QtCore.AlignHCenter|QtCore.Qt.AlignVCenter)
self.imgLabel.setScaledContents(True)
if window==2:
self.imgLabel2.setPixmap(QPixmap.fromImage(img))
self.imgLabel2.setAlignment(QtCore.Qt.AlignHCenter|qtCore.Qt.AlignVCenter)
self.imgLabel2.setScaledContents(True)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = ShowImage()
window.setWindowTitle('gambar')
window.show()
sys.exit(app.exec_())
和GUI: