繁体   English   中英

非常基本的 pyqt5 对话框应用程序以退出代码 -1073740791 退出

[英]Very basic pyqt5 dialog app quits with exit code -1073740791

我试图设计一个非常基本的 GUI 应用程序,它在对话框中显示输入的高度,但是在我按下主窗口上的“确定”按钮后,整个程序崩溃并且过程以以下退出代码结束:

进程完成,退出代码 -1073740791 (0xC0000409)

这是应用程序的完整代码,UI 文件如下:

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

主窗口的用户界面:

# -*- 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."))

对话框的用户界面:

# -*- 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."))

我将不胜感激有关修复此错误及其发生原因的帮助。

您正在尝试访问不存在的属性:

self.mid_label.setText(My_Window.mid_label_nexttext)

My_Window是一个,而mid_label_nexttext被分配给该类的实例self始终是对当前实例的引用)。

如果您想从“父”窗口设置该标签的文本,您可以向__init__添加一个额外的参数以允许获取文本,或者从主窗口设置它。

使用文本作为初始化参数

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)
        # ...

设置来自父级的文本

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)
        # ...

请注意,第一种方法通常更好,主要是出于模块化原因:假设您在不同情况下从不同的类创建该对话框,每当您需要更改标签的对象名称(或整个界面结构)时,您都可以轻松更改它在对话框子类中的引用,否则您需要更改代码中的每个引用。

注意:调用self.update()是没有用的; 如果您想在更改 Spinbox 值时更新对话框上的标签,您需要直接访问标签(如上一个示例)或使用信号。 此外,如果您使用相同的参数参数,则不需要使用lambda ,只需连接到函数即可。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM