0

I want to create a QMainWindow with PyQt5 in spyder environment. To do so, I wrote the following script :

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()

I saw in several sources such as here and here that because spyder creates an instance of QApplication, I need to test if this instance exists. However, I get two different behaviours when I launch this script in two different computers. In the first one (Windows 10), the error "kernel died, restarting" occurs as expected when I comment the lines 74 to 76 (to create the QApplication instance if it does not already exist) and the lines 86 to 95 (to import the required packages). In the second one (Windows 7) I don't get this error and the window shows up and I can use it.

When I launch spyder in a console with .\spyder.exe --show-console as suggested here, no log error is displayed.

I checked the versions of spyder, python and Qt and they all matches : spyder 3.2.4, Python 3.6.3, Qt 5.6.2. A also checked the modules, and the only difference is that openCV is installed only in the Windows 7 machine but not in the Windows 10 but I don't use it.

Is this difference caused by the operating system or is there something different making the script work in one case?

ractiv
  • 712
  • 8
  • 19
  • 1
    (*Spyder maintainer here*) I don't know why you're getting different behaviors and I really don't have time to help you debug that. Please simply use our recommendations to work with Qt applications inside Spyder and you'll be fine (not just on Windows but also on Linux and macOS). – Carlos Cordoba Nov 05 '18 at 19:06
  • Hi Carlos and thank you for your answer. I will do that, it seems to be the best way to ensure the script to work. – ractiv Nov 06 '18 at 17:00
  • Yeah, that was my point. Thanks for understanding and sorry for not being more helpful. – Carlos Cordoba Nov 06 '18 at 19:19

0 Answers0