Very basic pyqt5 dialog app quits with exit code -1073740791

-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:

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(My_Dialog, self).__init__()
        loadUi("dialog.ui", self)
        self.mid_label.setText(My_Window.mid_label_nexttext)

class My_Window(QMainWindow):
    def __init__(self):
        super(My_Window, self).__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:

# -*- 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:

# -*- 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 fixing this error, and on why it occured.

python
class
user-interface
pyqt
pyqt5
asked on Stack Overflow Aug 25, 2020 by b_dani

1 Answer

0

You are trying to access an attribute that does not exist:

self.mid_label.setText(My_Window.mid_label_nexttext)

My_Window is a class, while mid_label_nexttext was assigned to the instance of that class (self is always the reference to the current instance).

If you want to set the text for that label from a "parent" window, you either add an extra argument to the __init__ that allows to get the text, or you set it from the main window.

Use the text as init argument

class My_Dialog(QDialog):
    def __init__(self, text):
        super(My_Dialog, self).__init__()
        loadUi("dialog.ui", self)
        # ensure that "text" is a valid string, you can't use setText(None)
        if text:
            self.mid_label.setText(text)


class My_Window(QMainWindow):
    # ...
    def onMyPushButtonClick(self):
        dlg = My_Dialog(self.mid_label_nexttext)
        # ...

Set the text from the parent

class My_Dialog(QDialog):
    def __init__(self):
        super(My_Dialog, self).__init__()
        loadUi("dialog.ui", self)
        # NO setText() here!!!

class My_Window(QMainWindow):
    # ...
    def onMyPushButtonClick(self):
        dlg = My_Dialog()
        if self.mid_label_nexttext:
            dlg.mid_label.setText(self.mid_label_nexttext)
        # ...

Note that the first method is usually better, mostly for modularity reasons: let's say that you create that dialog from different classes in different situations, whenever you need to change the object name of the label (or the whole interface structure) you can easily change its reference in the dialog subclass, otherwise you'll need to change every reference in your code.

Note: the call to self.update() is useless; if you want to update the label on the dialog whenever the spinbox value is changed, you need to directly access the label (like in the last example) or use signals. Also, you don't need to use lambda if you're using the same argument parameter, just connect to the function.

answered on Stack Overflow Aug 25, 2020 by musicamante

User contributions licensed under CC BY-SA 3.0