【问题标题】:Segmentation fault when exiting PyQT app退出 PyQT 应用程序时出现分段错误
【发布时间】:2017-04-14 08:44:31
【问题描述】:

首先,我已尽力在这里和其他地方找到解决此问题的方法,并且我对问题所在有一个大致的了解,但我不清楚如何解决它。

基本问题是当我按下标准的“x”按钮关闭我的应用程序时出现分段错误。

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

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

当我关闭窗口时,终端会按照 MainController.close_event() 中的说明打印出来:

> here
> closed
> accepted
> Segmentation fault: 11

我已经在网上尝试了许多建议,我相当确定这是由于 python 没有关闭所有窗口(可能是由于它们被关闭的顺序 - 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

【问题讨论】:

  • 只是想 - QApplication.closeAllWindows() 关闭窗口,而 ev.accept() 试图关闭关闭的窗口,对吗?但我认为分段错误发生在底部的“del app”代码块中。
  • 感谢您的回复。我尝试将您提到的所有三行都注释掉,但无论如何都会发生同样的事情......
  • 有趣。当我尝试运行代码时,我得到 我不知道你是如何处理这个问题的,但也许重点就是这个。 (我认为是因为 pyqtgraph 而出现此错误)
  • 嗯,这很有趣;我必须覆盖一个标志才能在 Sierra 上安装 pyqtgraph。也许这就是问题所在。
  • 是的,当我注释掉 pyqtgraph 行时,就没有问题了。

标签: python pyqt pyqtgraph


【解决方案1】:

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

【讨论】:

  • 更新:我从 3.5.2 升级到 Python 3.6,这不再是问题。
  • 可能 pyqtgraph 在 python3 或更新版本中使用 pyqt5
猜你喜欢
  • 2017-02-21
  • 2012-08-14
  • 2017-12-09
  • 1970-01-01
  • 2021-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-27
相关资源
最近更新 更多