Process finishes with weird exit code (Very basic dialog app)

Heads up! You've already completed this tutorial.

9buzadani6114 | 2020-08-25 06:28:14 UTC | #1

I tried to design a very basic GUI app that shows the entered height on a dialog, but after I press the 'OK' button on the main window, the whole program crashes and the process finishes with this exit code:

Process finished with exit code -1073740791 (0xC0000409)

Here's the full code for the app, the UI files are below:

python
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.uic import *

class My_Dialog(QDialog):
    def __init__(self):
        super().__init__()
        loadUi("dialog.ui", self)
        self.mid_label.setText(My_Window.mid_label_nexttext)

class My_Window(QMainWindow):
    def __init__(self):
        super().__init__()
        loadUi("mainwindow.ui", self)

        self.mid_label_nexttext = None
        self.height_selecter_spinbox.textChanged.connect(lambda x: self.spin_changed(x))

        self.pushButton.clicked.connect(self.onMyPushButtonClick)

    def onMyPushButtonClick(self):
        dlg = My_Dialog()
        if dlg.exec_():
            print("Success!")
        else:
            print("Cancel!")

    def spin_changed(self, s):
        self.mid_label_nexttext = s
        self.update()


def main():
    app = QApplication(sys.argv)
    window = My_Window()
    window.show()
    app.exec_()

if __name__ == "__main__":
    main()

The main window's UI:

python
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'mainwindow.ui'
#
# Created by: PyQt5 UI code generator 5.15.0
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(513, 171)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(310, 80, 75, 23))
        self.pushButton.setObjectName("pushButton")
        self.layoutWidget = QtWidgets.QWidget(self.centralwidget)
        self.layoutWidget.setGeometry(QtCore.QRect(90, 40, 209, 29))
        self.layoutWidget.setObjectName("layoutWidget")
        self.layout = QtWidgets.QHBoxLayout(self.layoutWidget)
        self.layout.setContentsMargins(0, 0, 0, 0)
        self.layout.setObjectName("layout")
        self.label_firstpart = QtWidgets.QLabel(self.layoutWidget)
        font = QtGui.QFont()
        font.setPointSize(15)
        self.label_firstpart.setFont(font)
        self.label_firstpart.setObjectName("label_firstpart")
        self.layout.addWidget(self.label_firstpart)
        self.height_selecter_spinbox = QtWidgets.QSpinBox(self.layoutWidget)
        font = QtGui.QFont()
        font.setPointSize(13)
        self.height_selecter_spinbox.setFont(font)
        self.height_selecter_spinbox.setMinimum(100)
        self.height_selecter_spinbox.setMaximum(250)
        self.height_selecter_spinbox.setProperty("value", 175)
        self.height_selecter_spinbox.setObjectName("height_selecter_spinbox")
        self.layout.addWidget(self.height_selecter_spinbox)
        self.label_lastpart = QtWidgets.QLabel(self.layoutWidget)
        font = QtGui.QFont()
        font.setPointSize(15)
        self.label_lastpart.setFont(font)
        self.label_lastpart.setObjectName("label_lastpart")
        self.layout.addWidget(self.label_lastpart)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 513, 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.setText(_translate("MainWindow", "OK"))
        self.label_firstpart.setText(_translate("MainWindow", "My height is "))
        self.label_lastpart.setText(_translate("MainWindow", "cm."))

The dialog's UI:

python
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'dialog.ui'
#
# Created by: PyQt5 UI code generator 5.15.0
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(400, 300)
        self.dialog_buttonbox = QtWidgets.QDialogButtonBox(Dialog)
        self.dialog_buttonbox.setGeometry(QtCore.QRect(30, 240, 341, 32))
        self.dialog_buttonbox.setOrientation(QtCore.Qt.Horizontal)
        self.dialog_buttonbox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
        self.dialog_buttonbox.setObjectName("dialog_buttonbox")
        self.widget = QtWidgets.QWidget(Dialog)
        self.widget.setGeometry(QtCore.QRect(80, 90, 151, 41))
        self.widget.setObjectName("widget")
        self.dialog_layout = QtWidgets.QHBoxLayout(self.widget)
        self.dialog_layout.setContentsMargins(0, 0, 0, 0)
        self.dialog_layout.setObjectName("dialog_layout")
        self.left_label = QtWidgets.QLabel(self.widget)
        font = QtGui.QFont()
        font.setPointSize(12)
        self.left_label.setFont(font)
        self.left_label.setObjectName("left_label")
        self.dialog_layout.addWidget(self.left_label)
        self.mid_label = QtWidgets.QLabel(self.widget)
        font = QtGui.QFont()
        font.setPointSize(12)
        self.mid_label.setFont(font)
        self.mid_label.setObjectName("mid_label")
        self.dialog_layout.addWidget(self.mid_label)
        self.right_label = QtWidgets.QLabel(self.widget)
        font = QtGui.QFont()
        font.setPointSize(12)
        self.right_label.setFont(font)
        self.right_label.setObjectName("right_label")
        self.dialog_layout.addWidget(self.right_label)

        self.retranslateUi(Dialog)
        self.dialog_buttonbox.accepted.connect(Dialog.accept)
        self.dialog_buttonbox.rejected.connect(Dialog.reject)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.left_label.setText(_translate("Dialog", "You are"))
        self.mid_label.setText(_translate("Dialog", "100"))
        self.right_label.setText(_translate("Dialog", "cm tall."))

I would appreciate some help on how to fix this error.:slight_smile:

Create GUI Applications with Python & Qt5 by Martin Fitzpatrick — (PyQt5 Edition) The hands-on guide to making apps with Python — Over 10,000 copies sold!

More info Get the book


Eolinwen | 2020-08-25 17:11:32 UTC | #2

Hi,

At first look, I run into an issue when teh ui file is loading.

python
$python superapp.py
Traceback (most recent call last):
  File "superapp.py", line 54, in <module>
    window = My_Window()
  File "superapp.py", line 25, in __init__
    loadUi(ui_file, self)
  File "/usr/lib/python3.8/site-packages/PyQt5/uic/__init__.py", line 238, in loadUi
    return DynamicUILoader(package).loadUi(uifile, baseinstance, resource_suffix)
  File "/usr/lib/python3.8/site-packages/PyQt5/uic/Loader/loader.py", line 66, in loadUi
    return self.parse(filename, resource_suffix)
  File "/usr/lib/python3.8/site-packages/PyQt5/uic/uiparser.py", line 1020, in parse
    document = parse(filename)
  File "/usr/lib/python3.8/xml/etree/ElementTree.py", line 1202, in parse
    tree.parse(source, parser)
  File "/usr/lib/python3.8/xml/etree/ElementTree.py", line 595, in parse
    self._root = parser._parse_whole(source)
xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 1, column 1

Did you have all copy correctly ?

I have done that too specifying the loading path. I think that at the same level than yours ui files ?

python
import sys
import os
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.uic import *


class My_Dialog(QDialog):
    def __init__(self):
        super().__init__()
        real_path = "dialog.ui"
        current_dir = os.path.dirname(os.path.realpath(__file__))
        ui_file = os.path.join(current_dir, real_path)
        loadUi(u_file, self)
        self.mid_label.setText(My_Window.mid_label_nexttext)


class My_Window(QMainWindow):
    def __init__(self):
        super().__init__()
        real_path = "mainwindow.ui"
        current_dir = os.path.dirname(os.path.realpath(__file__))
        ui_file = os.path.join(current_dir, real_path)
        loadUi(ui_file, self)

    self.mid_label_nexttext = None
    self.height_selecter_spinbox.textChanged.connect(lambda x: self.spin_changed(x))

    self.pushButton.clicked.connect(self.onMyPushButtonClick)

def onMyPushButtonClick(self):
    dlg = My_Dialog()
    if dlg.exec_():
        print("Success!")
    else:
        print("Cancel!")

def spin_changed(self, s):
    self.mid_label_nexttext = s
    self.update()


# def main():
    # app = QApplication(sys.argv)
    # window = My_Window()
    # window.show()
    # # app.exec_()
    # sys.exit(app.exec_())

if __name__ == "__main__":
    # main()
    app = QApplication(sys.argv)
    window = My_Window()
    window.show()
    # app.exec_()
    sys.exit(app.exec_())

9buzadani6114 | 2020-08-26 09:04:35 UTC | #3

Thanks for your answer, @Eolinwen, I tried it with your version of the code, but still got the same problem. All the files are in the same directory and the copying is also correct.


Eolinwen | 2020-08-27 15:25:15 UTC | #4

I have a bit more free time today than previously. :stuck_out_tongue_winking_eye:

I have tried two or three things without any success.

And this kind of error, is annoying. So, I have tried to open yours files (the both) in Qt Designer (to check if they are okay) and I can not open them. I get this error and confirm what my terminal shout me. They are not very well formatted, something is missing. Look.

[url=http://pix.toile-libre.org/?img=1598546405.png][/url]


The complete guide to packaging Python GUI applications with PyInstaller.
[[ discount.discount_pc ]]% OFF for the next [[ discount.duration ]] [[discount.description ]] with the code [[ discount.coupon_code ]]

Purchasing Power Parity

Developers in [[ country ]] get [[ discount.discount_pc ]]% OFF on all books & courses with code [[ discount.coupon_code ]]
Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak

Process finishes with weird exit code (Very basic dialog app) was written by Martin Fitzpatrick .

Martin Fitzpatrick has been developing Python/Qt apps for 8 years. Building desktop applications to make data-analysis tools more user-friendly, Python was the obvious choice. Starting with Tk, later moving to wxWidgets and finally adopting PyQt.