退出 PyQT 应用程序时出现分段错误

Segmentation fault when exiting PyQT app

首先,我已经尽力在这里和其他地方找到解决这个问题的方法,我对问题是什么有一个大概的了解,但我不清楚如何解决它。

基本问题是,当我通过按标准 "x" 按钮关闭我的应用程序时出现分段错误。

最重要的细节(我认为)是我使用的是 MacOS Sierra、python 3.5.2 和 pyqt5。

我正在构建的应用程序非常松散地基于另一个项目 (Dioptas),这是一个相对成熟的项目。我或多或少开始了。

当我关闭 window 时,终端按照 MainController.close_event() 中的指示打印出来:

> here
> closed
> accepted
> Segmentation fault: 11

我在网上尝试了很多建议,我相当确定这是由于 python 没有关闭所有 windows(可能是因为它们关闭的顺序) - QApplication.CloseAllWindows() 表示它们以随机顺序关闭,一方面)。如果有人有建议或解决方案,我将不胜感激。

以下是我的代码:

import sys
import pyqtgraph as pg
import numpy as np
from PIL import Image
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *

class MainController(QWidget):
    def __init__(self):
        super().__init__
        self.start()
        self.create_signals()

    def start(self):
        self.widget = MainWidget()
        self.widget.show()

    def create_signals(self):
        self.widget.closeEvent = self.close_event

    def close_event(self, ev):
        print("here")
        QApplication.closeAllWindows()
        print("closed")
        ev.accept()

class MainWidget(QWidget):
    def __init__(self, *args, **kwargs):
        super(MainWidget, self).__init__(*args, **kwargs)
        self.layout = QHBoxLayout()
        self.layout.setContentsMargins(2, 2, 2, 2)
        self.layout.setSpacing(6)

        self.stepFilterDisplayWidget = StepFilterDisplayWidget()
        self.stepFilterControlWidget = StepFilterControlWidget()
        self.layout.addWidget(self.stepFilterDisplayWidget)
        self.layout.addWidget(self.stepFilterControlWidget)
        self.setLayout(self.layout)
        self.setGeometry(100,100,1000,700)

class StepFilterDisplayWidget(QWidget):
    def __init__(self, *args, **kwargs):
        super(StepFilterDisplayWidget,self).__init__(*args,**kwargs)

        self.layout = QVBoxLayout()
        self.setLayout(self.layout)

        self.plot = pg.ImageView()
        self.layout.addWidget(self.plot)

        self.button = QPushButton("Plot", self)
        self.button.clicked.connect(self.showImage)

        self.layout.addWidget(self.button)

    def showImage(self):
        im = Image.open('S_15a_crop.tif')
        self.data = np.array(im)
        self.plot.setImage(self.data)


class StepFilterControlWidget(QWidget):
    def __init__(self, *args, **kwargs):
        super(StepFilterControlWidget, self).__init__(*args, **kwargs)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    controller = MainController()
    app.exec_()
    del app

问题与 pyqtgraph(使用 PyQt4)和 PyQt5 导入有关。 pyqtgraph 试图使用属于 PyQt4 的东西,它被 PyQt5 导入覆盖。这会触发分段错误。