2

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_())
musicamante
  • 41,230
  • 6
  • 33
  • 58
Midoz
  • 31
  • 1
  • 7
  • When does that error occur? What does `Load_exam` do? – musicamante Feb 01 '21 at 13:18
  • Load exam is the first code: 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_() – Midoz Feb 01 '21 at 13:22
  • The error occurs after logging in – Midoz Feb 01 '21 at 13:23

2 Answers2

2

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)

    # ...
musicamante
  • 41,230
  • 6
  • 33
  • 58
  • I'm sorry I never heard of pyuic and I just started learning PyQt. So, I created the LoginWindow class, but concerning loading the LoginForm from design, should'nt I look for push button; for example: button = `self.findChild(QPushButton, 'pushButton') `. Ans same thing goes for lineEditEmail and lineEditPassword. Please I need your help. – Midoz Feb 01 '21 at 14:46
  • I want to point at something else, in my initial code instead of opening a browser, I just put a push button, and the window opened without any error, if O try to open the browser if gives an error – Midoz Feb 01 '21 at 14:54
  • @Midoz The code you posted with the `Ui_Form` has been created with pyuic. "should'nt I look for push button": have you tried my code? No, you don't need to use `self.findChild` (as lots of tutorials unfortunately suggest, and they are *wrong*): using `uic.loadUi` or the inheritance with `setupUi()` approach results in creating all instance attributes for each widget exactly as they are listed in designer. – musicamante Feb 01 '21 at 14:54
  • Thank you so much :) – Midoz Feb 01 '21 at 19:00
0

I have the same problem and I've resolved in way I'm showing below: In your code you should change the lower part as follows ` ... # 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_())`
Kancho Iliev
  • 701
  • 5
  • 13