PyQt5中按钮连接有什么问题? [英] What is the problem with buttons connection in PyQt5?
问题描述
我的问题是我无法连接两个按钮之间的实现.在我按下灰度"按钮并获得灰度图像后,我按下Canny"按钮但用户界面突然关闭.我不知道我的代码有什么问题.
I have the problem that I cannot connect the implementation between two buttons. After I pressed the "Grayscale" button and got the grayscale image, I pressed the "Canny" button but the user interface suddenly closed. I don't know what's wrong in my code.
def getImage(self):
global fname
fname = QFileDialog.getOpenFileName(self, 'Open file',
'C:\\Users\binil-ping\Desktop\CODE',"Image Files (*.jpg *.gif *.bmp *.png)")
pixmap = QPixmap(fname[0])
self.label.setPixmap(QPixmap(pixmap))
self.resize(pixmap.width(), pixmap.height())
def Grayscale(self):
global edges
edges = cv2.imread(fname[0], 0)
edges = cv2.GaussianBlur(edges, (5, 5), 0)
height, width = edges.shape[:2]
ret,edges = cv2.threshold(edges,150,255,cv2.THRESH_BINARY)
kernel = np.ones((5,5),np.uint8)
edges = cv2.morphologyEx(edges, cv2.MORPH_OPEN, kernel)
edges = cv2.morphologyEx(edges, cv2.MORPH_OPEN, kernel)
edges = QImage(edges, width, height, QImage.Format_Grayscale8)
pixmap = QPixmap.fromImage(edges)
self.label.setPixmap(pixmap)
self.resize(pixmap.width(), pixmap.height())
def Canny(self):
edges2 = cv2.imread(edges[0],-1)
edges2 = cv2.Canny(edges2,180,200)
edges2 = QImage(edges2, width, height, QImage.Format_Grayscale8)
pixmap = QPixmap.fromImage(edges2)
self.label.setPixmap(pixmap)
self.resize(pixmap.width(), pixmap.height())
推荐答案
您的代码存在很多问题(包括您没有提供 最小的、可重现的示例),其中一些解释了崩溃或可能导致另一个崩溃(我用 [*] 标记了它们):
There are a lot of problems with your code (including the fact that you didn't provide a minimal, reproducible example), some of them explaining the crash or possibly leading to another one (I've marked them with [*]):
- 正如已经指出的那样,尽可能避免使用全局变量(几乎总是这样),而是使用类属性;
- 你不断地覆盖这些全局变量,使调试变得非常混乱;实际上,您将
edges
用于 numpy 数组 和 QImage; - [*] 您正在尝试
cv2.imread(edges[0],-1)
,但不仅 imread 需要一个字符串,而且由于上一点,在那个pointedges
甚至是一个 QImage; - [*] 一些变量没有在函数范围内声明(Canny 函数中的宽度/高度);
- 你真的应该避免对函数和变量使用大写的名字,因为它们通常只用于类名和内置常量;
- 在转换回 QImage 时存在一些问题,我想这是由于您对数组应用的各种转换(其中一些似乎也是不必要的)对 Format_Grayscale8 格式不太友好,也许您应该更好地调查一下;
- as already pointed out, avoid using globals whenever possible (which is almost always), use class attributes instead;
- you are continuously overwriting those globals, making everything very confusing also for debugging; in fact you're using
edges
for both a numpy array and a QImage; - [*] you are trying to
cv2.imread(edges[0],-1)
, but not only imread expects a string, but, as a result of the previous point, at that pointedges
is even a QImage; - [*] some variables are not declared in the scope of the function (width/height in the Canny function);
- you should really avoid using capitalized names for functions and variables, as they are usually only for class names and builtin constants;
- there are some issues in the conversion back to QImage, I suppose that's due to the various transformations you're applying to to the array (some of them also seem unnecessary) that are not very friendly with the Format_Grayscale8 format, perhaps you should better investigate about that;
这是对您的代码的可能改进.我正在添加小部件创建部分,因为它在原始示例中缺失.
Here's a possible improvement over your code. I'm adding the widget creation parts, as it was missing in the original example.
class ShowImage(QWidget):
def __init__(self):
QWidget.__init__(self)
layout = QVBoxLayout(self)
self.label = QLabel()
layout.addWidget(self.label)
self.getImageBtn = QPushButton()
layout.addWidget(self.getImageBtn)
self.getImageBtn.clicked.connect(self.getImage)
self.grayBtn = QPushButton('gray')
layout.addWidget(self.grayBtn)
self.grayBtn.clicked.connect(self.grayScale)
self.cannyBtn = QPushButton('canny')
layout.addWidget(self.cannyBtn)
self.cannyBtn.clicked.connect(self.canny)
self.fileName = None
self.edges = None
def getImage(self):
fname = QFileDialog.getOpenFileName(self, 'Open file',
'C:\\Users\binil-ping\Desktop\CODE',"Image Files (*.jpg *.gif *.bmp *.png)")
if fname[0]:
self.fileName = fname[0]
pixmap = QPixmap(self.fileName)
self.label.setPixmap(pixmap)
def grayScale(self):
if not self.fileName:
return
edges = cv2.imread(self.fileName, 0)
edges = cv2.GaussianBlur(edges, (5, 5), 0)
ret,edges = cv2.threshold(edges, 150, 255, cv2.THRESH_BINARY)
kernel = np.ones((5, 5), np.uint8)
edges = cv2.morphologyEx(edges, cv2.MORPH_OPEN, kernel)
edges = cv2.morphologyEx(edges, cv2.MORPH_OPEN, kernel)
self.edges = edges
height, width = edges.shape[:2]
image = QImage(edges, width, height, QImage.Format_Grayscale8)
pixmap = QPixmap.fromImage(image)
self.label.setPixmap(pixmap)
def canny(self):
if self.edges is None:
return
edges2 = cv2.Canny(self.edges, 180, 200)
height, width = edges2.shape[:2]
edges2 = QImage(edges2, width, height, QImage.Format_Grayscale8)
pixmap = QPixmap.fromImage(edges2)
self.label.setPixmap(pixmap)
这篇关于PyQt5中按钮连接有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!