Friday, 16 November 2018

PyQT5 program returning error only during first compile run

The following example, when run in a new ipython console (spyder) returns "-1", but only at the first try. After that it seems to work fine.

from PyQt5 import QtWidgets, QtGui, QtCore

from GUI import Ui_MainWindow  # importing our generated file

import sys
import numpy as np

class mywindow(QtWidgets.QMainWindow):

    def __init__(self):

        super(mywindow, self).__init__()

        self.ui = Ui_MainWindow()

        self.ui.setupUi(self)

app = QtCore.QCoreApplication.instance()
if app is None:
    app = QtWidgets.QApplication(sys.argv)

application = mywindow()

application.show()

sys.exit(app.exec())

The error is as follows:

>runfile('C:/Users/xxx.py', wdir='C:/Users/xx/Documents/3dPackaging') An exception has occurred, use %tb to see the full traceback.

SystemExit: -1

C:\Users\xx\Documents\Anaconda\lib\site-packages\IPython\core\interactiveshell.py:2969: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D. warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)

%tb Traceback (most recent call last):

File "", line 1, in runfile('C:/Users/xxx.py', wdir='C:/Users/xx')

File "C:\Users\xx\Documents\Anaconda\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 668, in runfile execfile(filename, namespace)

File "C:\Users\xx\Documents\Anaconda\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/xx/Documents/3dPackaging/VersandkartonsPacken.py", line 64, in sys.exit(app.exec())

SystemExit: -1

I have no idea what this could be, but as this is supposed to be a standalone program one day, I'm afraid it might be messy then, so i'd rather fix it. Anything I should try?

It's not reproducible without the GUI.py, so here it is:

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1127, 910)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton_schliessen = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_schliessen.setGeometry(QtCore.QRect(930, 770, 75, 23))
        self.pushButton_schliessen.setObjectName("pushButton_schliessen")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(440, 20, 421, 31))
        font = QtGui.QFont()
        font.setPointSize(20)
        self.label.setFont(font)
        self.label.setObjectName("label")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(170, 770, 75, 23))
        self.pushButton.setObjectName("pushButton")
        self.tableWidget_input = QtWidgets.QTableWidget(self.centralwidget)
        self.tableWidget_input.setGeometry(QtCore.QRect(160, 80, 421, 671))
        self.tableWidget_input.setObjectName("tableWidget_input")
        self.tableWidget_input.setColumnCount(0)
        self.tableWidget_input.setRowCount(0)
        self.tableWidget_output = QtWidgets.QTableWidget(self.centralwidget)
        self.tableWidget_output.setGeometry(QtCore.QRect(650, 80, 441, 671))
        self.tableWidget_output.setObjectName("tableWidget_output")
        self.tableWidget_output.setColumnCount(0)
        self.tableWidget_output.setRowCount(0)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 1127, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton_schliessen.setText(_translate("MainWindow", "Schliessen"))
        self.label.setText(_translate("MainWindow", "Versandkartons Packen"))
        self.pushButton.setText(_translate("MainWindow", "Berechnen"))

It seems to be related to this, app is never none but already returns a QCoreApplication in the first run. However , moving it into a function doesn't work. And I can't reproduce in the console the behaviour that actually makes it work in the second run. Something seems to change the QCorEApplication into a working one.



from PyQT5 program returning error only during first compile run

No comments:

Post a Comment