Skip to content
Advertisement

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

I am creating a desktop app using PyQt5 and QtDesginer. I have a login page connected to a database, and the user is asked to enter username and password. In the designer, I created a window that opens a certain link. The following code is running. But when inserting it into the second code it gives 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_())

Advertisement

Answer

The source of the problem mostly resides in the fact that only one and unique QApplication instance should ever exist for each process.

When you import Load_exam a QApplication instance already exists and is being executed, and that scipt will try to execute the last three lines (since there’s no if __name__ == '__main__' check), hence the crash.

Before providing the solution, consider the following two aspects:

  • import statements should always happen in the beginning of the scripts, as importing within the code (especially within a function) is usually unnecessary and often leads to unexpected problems; for common uses, the only accepted exception is to do that in the __name__ check;
  • editing of files generated by pyuic is considered bad practice and should never, ever be done unless you really know what you’re doing (and if you do know, you will probably not do it); those files are inteded to be exclusively imported, as explained in the official guidelines about using Designer;

With the above points in mind, recreate the ui for Ui_Form with pyuic (the following assumes the generated file is named loginForm.py), and create a new script like this:

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

Since you’re already using uic for the browser window, you can just skip the pyuic part, and just use the same on the login window as well:

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

    # ...
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement