python - 进程已完成,退出代码 -1073740791 (0xC0000409) 错误,无法打开网站

标签 python pyqt5 qt-designer qwebengineview

我正在使用 PyQt5 和 QtDesginer 创建桌面应用程序。我有一个连接到数据库的登录页面,要求用户输入用户名和密码。在设计器中,我创建了一个打开特定链接的窗口。以下代码正在运行。但是当将其插入第二个代码时,它会给出 Process finish 并退出代码 -1073740791 (0xC0000409)。

from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5 import uic
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView
import sys


class UI(QWidget):
    def __init__(self):
        super().__init__()
        uic.loadUi("website.ui", self)

        self.show()


app = QApplication(sys.argv)
window = UI()
app.exec_()
from PyQt5 import QtCore, QtGui, QtWidgets
import mysql.connector as mc
from PyQt5.QtWidgets import QDialog


class Ui_Form(object):

    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(500, 193)
        self.verticalLayout_2 = QtWidgets.QVBoxLayout(Form)
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.label = QtWidgets.QLabel(Form)
        self.label.setObjectName("label")
        self.horizontalLayout.addWidget(self.label)
        self.lineEditEmail = QtWidgets.QLineEdit(Form)
        self.lineEditEmail.setObjectName("lineEditEmail")
        self.horizontalLayout.addWidget(self.lineEditEmail)
        self.verticalLayout_2.addLayout(self.horizontalLayout)
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.label_2 = QtWidgets.QLabel(Form)
        self.label_2.setObjectName("label_2")
        self.horizontalLayout_2.addWidget(self.label_2)
        self.lineEditPassword = QtWidgets.QLineEdit(Form)
        self.lineEditPassword.setEchoMode(QtWidgets.QLineEdit.Password)
        self.lineEditPassword.setObjectName("lineEditPassword")
        self.horizontalLayout_2.addWidget(self.lineEditPassword)
        self.verticalLayout_2.addLayout(self.horizontalLayout_2)
        spacerItem = QtWidgets.QSpacerItem(20, 40,
                                           QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
        self.verticalLayout_2.addItem(spacerItem)
        self.verticalLayout = QtWidgets.QVBoxLayout()
        self.verticalLayout.setObjectName("verticalLayout")
        self.pushButton = QtWidgets.QPushButton(Form)
        self.pushButton.setObjectName("pushButton")

        # this is the signal that we have already connected
        self.pushButton.clicked.connect(self.login)
        self.verticalLayout.addWidget(self.pushButton)
        self.labelResult = QtWidgets.QLabel(Form)
        font = QtGui.QFont()
        font.setPointSize(14)
        font.setBold(True)
        font.setWeight(75)
        self.labelResult.setFont(font)
        self.labelResult.setText("")
        self.labelResult.setObjectName("labelResult")
        self.verticalLayout.addWidget(self.labelResult)
        self.verticalLayout_2.addLayout(self.verticalLayout)

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

    def login(self):
        try:
            email = self.lineEditEmail.text()
            password = self.lineEditPassword.text()

            mydb = mc.connect(
                host="localhost",
                user="root",
                password="",
                database="program"

            )

            mycursor = mydb.cursor()
            query = "SELECT username,password from user where username " \
                    "like '" + email + "'and password like '" \
                    + password + "'"
            mycursor.execute(query)
            result = mycursor.fetchone()

            if result == None:
                self.labelResult.setText("Incorrect email or password")

            else:
                self.labelResult.setText("You are logged in")
                import Load_exam



        except mc.Error as e:
            self.labelResult.setText("Error")

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.label.setText(_translate("Form", "Email:"))
        self.label_2.setText(_translate("Form", "Password:"))
        self.pushButton.setText(_translate("Form", "Login"))


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    Form = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

最佳答案

问题的根源主要在于每个进程只应该存在一个唯一的QApplication实例。

当您导入 Load_exam 时,QApplication 实例已经存在并正在执行,并且该 scipt 将尝试执行最后三行(因为没有 if __name__ == '__main__' 检查),因此崩溃。

在提供解决方案之前,请考虑以下两个方面:

  • 导入语句应始终出现在脚本的开头,因为在代码内(尤其是在函数内)导入通常是不必要的,并且常常会导致意外的问题;对于常见用途,唯一接受的异常(exception)是在 __name__ 检查中执行此操作;
  • 编辑 pyuic 生成的文件被认为是一种不好的做法,并且永远、永远都不应该这样做,除非您真的知道自己在做什么(如果您确实知道,您可能不会这样做);这些文件旨在专门导入,如关于 using Designer 的官方指南中所述。 ;

考虑到上述几点,使用 pyuic 重新创建 Ui_Form 的 ui(以下假设生成的文件名为 loginForm.py),并创建一个新脚本像这样:

from PyQt5 import QtCore, QtGui, QtWidgets, uic
from loginForm import Ui_Form
import mysql.connector as mc


class BrowserWindow(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        uic.loadUi("website.ui", self)


class LoginWindow(QtWidgets.QWidget, Ui_Form):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.pushButton.clicked.connect(self.login)

    def login(self):
        try:
            email = self.lineEditEmail.text()
            password = self.lineEditPassword.text()

            mydb = mc.connect(
                host="localhost",
                user="root",
                password="",
                database="program"

            )

            mycursor = mydb.cursor()
            query = "SELECT username,password from user where username " \
                    "like '" + email + "'and password like '" \
                    + password + "'"
            mycursor.execute(query)
            result = mycursor.fetchone()

            if result == None:
                self.labelResult.setText("Incorrect email or password")

            else:
                self.labelResult.setText("You are logged in")
                self.showBrowser()

        except mc.Error as e:
            self.labelResult.setText("Error")

    def showBrowser(self):
        self.browser = BrowserWindow()
        self.browser.show()


if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    loginWindow = LoginWindow()
    loginWindow.show()
    sys.exit(app.exec_())

由于您已经在浏览器窗口中使用 uic,因此您可以跳过 pyuic 部分,并在登录窗口上使用相同的内容:

class LoginWindow(<b>QtWidgets.QWidget</b>):
    def __init__(self):
        super().__init__()
        uic.loadUi('loginForm.ui', self)
        self.pushButton.clicked.connect(self.login)

    # ...

关于python - 进程已完成,退出代码 -1073740791 (0xC0000409) 错误,无法打开网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65993234/

相关文章:

python - Pandas - 使用 np.where() 有条件地更新列

python - PYQT - 多层次嵌套小部件和布局

python - 如何将 .ui 文件转换为 .py 文件

c++ - Qt - 依赖类

css - Qt css背景半透明

python - 如果字符串不在一个或多个列表中则引发错误

python - 张量板错误 'Can not convert a AdamOptimizer into a Tensor or Operation.'

python - Pandas 在可变列上合并

python - PyQt Tableview 行背景颜色基于单元格值

python - MATLAB 生成的 Python 包与 Ubuntu 上的 PyQt5 冲突——可能是库问题