我正在使用 PyQt5 和 QtDesginer 创建一个桌面应用程序。我有一个连接到数据库的登录页面,要求用户输入用户名和密码。在设计器中,我创建了一个打开某个链接的窗口。以下代码正在运行。但是当将它插入到第二个代码中时,它会给出 Process 完成,退出代码为 -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_())
答案 0 :(得分:0)
问题的根源主要在于每个进程应该只存在一个和唯一的 QApplication 实例。
当您导入 Load_exam
时,QApplication 实例已经存在并正在执行,并且该 scipt 将尝试执行最后三行(因为没有 if __name__ == '__main__'
检查),因此崩溃。
在提供解决方案之前,请考虑以下两个方面:
__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(QtWidgets.QWidget):
def __init__(self):
super().__init__()
uic.loadUi('loginForm.ui', self)
self.pushButton.clicked.connect(self.login)
# ...