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?