class Faceshow(QMainWindow, Ui_MainWindow): def init(self, parent=None): super(Faceshow, self).init(parent) self.setupUi(self) self.main_button.clicked.connect(self.show_resu) self.main_exit.clicked.connect(self.quit) self.form = None def show_resu(self): try: img1, img2, img3, img4, text1, text2 = start() except Exception as e: print(e) return img1 = ndarray_to_qpixmap(img1) img2 = ndarray_to_qpixmap(img2) img3 = ndarray_to_qpixmap(img3) img4 = ndarray_to_qpixmap(img4) self.form = RESUshow(img1, img2, img3, img4, text1, text2) self.form.pushButton.clicked.connect(self.form.save_img4) self.form.show() def quit(self): self.close() if name == 'main': hxz = QApplication(sys.argv) ui = Faceshow() ui.show() sys.exit((hxz.exec_())) 修改上述代码,使得start()函数出现异常后,刷新主界面并不影响槽函数的再次触发
时间: 2023-06-26 19:04:53 浏览: 114
您可以在show_resu函数中添加一个try-except语句,并在except语句中处理异常,例如显示一个错误消息框。同时,您可以将self.form设为None,以确保每次重新打开时都会创建新的RESUshow对象。
修改后的代码如下:
```python
from PyQt5.QtWidgets import QMessageBox
class Faceshow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(Faceshow, self).__init__(parent)
self.setupUi(self)
self.main_button.clicked.connect(self.show_resu)
self.main_exit.clicked.connect(self.quit)
self.form = None
def show_resu(self):
try:
img1, img2, img3, img4, text1, text2 = start()
except Exception as e:
print(e)
QMessageBox.warning(self, 'Error', 'Failed to process images.')
return
img1 = ndarray_to_qpixmap(img1)
img2 = ndarray_to_qpixmap(img2)
img3 = ndarray_to_qpixmap(img3)
img4 = ndarray_to_qpixmap(img4)
if self.form:
self.form.close()
self.form = RESUshow(img1, img2, img3, img4, text1, text2)
self.form.pushButton.clicked.connect(self.form.save_img4)
self.form.show()
def quit(self):
self.close()
if __name__ == '__main__':
hxz = QApplication(sys.argv)
ui = Faceshow()
ui.show()
sys.exit(hxz.exec_())
```
阅读全文