首页 > 解决方案 > Spyder 内核在 Windows 10 上死掉了,但在 Windows 7 上没有

问题描述

我想在 spyder 环境中使用 PyQt5 创建一个 QMainWindow。为此,我编写了以下脚本:

try:
    from PyQt4.QtCore import QEventLoop
except:
    try:
        from PyQt5.QtCore import QEventLoop
    except:
        pass

def launchGUI(windowTitle="Title"):
    # Import the recquired PyQt packages
    try:
        from PyQt4.QtGui import (
            QPushButton, QMainWindow, QVBoxLayout, QWidget)
    except:
        try:
            from PyQt5.QtWidgets import (
                QPushButton, QMainWindow, QVBoxLayout, QWidget)
        except:
            print("you need PyQt4 or PyQt5 to display images")
            return

    # ---- Main window class
    class MainWnd(QMainWindow):

        def __init__(self, title, parent=None):
            super(MainWnd, self).__init__(parent)

            self.eventLoop = QEventLoop()

            # Main widget, containing the entire window
            centralWidget = QWidget()
            self.setCentralWidget(centralWidget)
            mainLayout = QVBoxLayout()
            centralWidget.setLayout(mainLayout)

            # Validation button
            self.buttonOK = QPushButton("Ok")
            self.buttonOK.clicked.connect(self.buttonOKClicked)
            mainLayout.addWidget(self.buttonOK)

            # Display the window
            self.setWindowTitle(title)
            self.show()

        def buttonOKClicked(self):
            # Mark the threshold as validated and close the window
            self.bValidated = True
            self.close()

        def closeEvent(self, *args, **kwargs):
            # Stop the event loop
            self.eventLoop.exit()

        def exec(self):
            # Loop to wait the window closure
            self.eventLoop.exec()

    # Create and display the Qt window and wait for its closure
    w = MainWnd(windowTitle)
    w.exec()

def main():
    app = QtCore.QCoreApplication.instance()
    if app is None:
        app = QApplication([])

    launchGUI()
#    app.exec_()

if __name__ == "__main__":

    # import necessary Qt modules
    # display an error message and abort if importation fails
    bPyQtLoadSuccessful = True
    try:
        from PyQt4 import QtCore
        from PyQt4.QtGui import QApplication
    except:
        try:
            from PyQt5 import QtCore
            from PyQt5.QtWidgets import QApplication
        except:
            print("you need PyQt4 or PyQt5 to display images")
            bPyQtLoadSuccessful = False

    if bPyQtLoadSuccessful:
        main()

我在herehere等几个来源中看到,因为spyder创建了一个QApplication实例,我需要测试这个实例是否存在。但是,当我在两台不同的计算机上启动此脚本时,我会得到两种不同的行为。在第一个(Windows 10)中,"kernel died, restarting"当我注释第 74 到 76 行(如果 QApplication 实例不存在时创建它)和第 86 到 95 行(导入所需的包)时,错误按预期发生。在第二个(Windows 7)中,我没有收到此错误,并且窗口出现并且我可以使用它。

当我按照此处.\spyder.exe --show-console的建议在控制台中启动 spyder 时,不会显示任何日志错误。

我检查了 spyder、python 和 Qt 的版本,它们都匹配:spyder 3.2.4、Python 3.6.3、Qt 5.6.2。A 还检查了模块,唯一的区别是 openCV 只安装在 Windows 7 机器上而不是在 Windows 10 中但我不使用它。

这种差异是由操作系统引起的,还是在某种情况下使脚本工作的不同之处?

标签: pythonpyqtanacondaspyder

解决方案


推荐阅读