簡體   English   中英

進程已完成,退出代碼為 -1073740791 (0xC0000409) 錯誤,無法打開網站

[英]Process finished with exit code -1073740791 (0xC0000409) error not opening a website

我正在使用 PyQt5 和 QtDesginer 創建桌面應用程序。 我有一個連接到數據庫的登錄頁面,要求用戶輸入用戶名和密碼。 在設計器中,我創建了一個打開某個鏈接的window。 以下代碼正在運行。 但是,當將它插入到第二個代碼中時,它會給出 Process finished with exit code -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__'檢查),因此崩潰。

在提供解決方案之前,請考慮以下兩個方面:

  • import 語句應該始終出現在腳本的開頭,因為在代碼中(尤其是在函數中)導入通常是不必要的,並且經常會導致意想不到的問題; 對於常見用途,唯一可接受的例外是在__name__檢查中執行此操作;
  • 編輯pyuic生成的文件被認為是不好的做法,除非您真的知道自己在做什么(如果知道,您可能不會這樣做),否則永遠不應該這樣做; 這些文件旨在專門導入,如有關使用 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_())

由於您已經在瀏覽器 window 中使用uic ,因此您可以跳過pyuic部分,並在登錄 window 上使用相同的部分:

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

    # ...

我有同樣的問題,我已經解決了我在下面顯示的方式:在你的代碼中你應該改變下半部分如下`... # Change this: def showBrowser(self): browser.show()

if name == ' main ': import sys app = QtWidgets.QApplication(sys.argv)

# Add this
browser = BrowserWindow()
#
loginWindow = LoginWindow()
loginWindow.show()
sys.exit(app.exec_())`

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM