进程已完成,退出代码为 -1073740791 (0xC0000409) 错误,无法打开网站
Process finished with exit code -1073740791 (0xC0000409) error not opening a website
我正在使用 PyQt5 和 QtDesginer 创建桌面应用程序。我有一个连接到数据库的登录页面,要求用户输入用户名和密码。在设计器中,我创建了一个window打开某个link。下面的代码是运行。但是当将它插入到第二个代码中时,它给出了 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_())
问题的根源主要在于每个进程只应存在 one 和 unique QApplication 实例。
当您导入 Load_exam
时,QApplication 实例已经存在并正在执行,并且该 scipt 将尝试执行最后三行(因为没有 if __name__ == '__main__'
检查),因此崩溃。
在提供解决方案之前,请考虑以下两个方面:
- import 语句应该总是出现在脚本的开头,因为在代码中(尤其是在函数中)导入通常是不必要的,而且常常会导致意想不到的问题;对于常见用途,唯一可接受的例外是在
__name__
检查中执行此操作;
- 编辑
pyuic
生成的文件被认为是不好的 做法,永远不应该,永远 除非你真的知道你在做什么(如果你做知道,你可能不会做);这些文件是专门导入的,如官方 guidelines about 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_())
由于您已经在浏览器 window 中使用 uic
,因此您可以跳过 pyuic
部分,并在登录 window 中使用相同的部分还有:
class LoginWindow(<b>QtWidgets.QWidget</b>):
def __init__(self):
super().__init__()
uic.loadUi('loginForm.ui', self)
self.pushButton.clicked.connect(self.login)
# ...
我正在使用 PyQt5 和 QtDesginer 创建桌面应用程序。我有一个连接到数据库的登录页面,要求用户输入用户名和密码。在设计器中,我创建了一个window打开某个link。下面的代码是运行。但是当将它插入到第二个代码中时,它给出了 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_())
问题的根源主要在于每个进程只应存在 one 和 unique QApplication 实例。
当您导入 Load_exam
时,QApplication 实例已经存在并正在执行,并且该 scipt 将尝试执行最后三行(因为没有 if __name__ == '__main__'
检查),因此崩溃。
在提供解决方案之前,请考虑以下两个方面:
- import 语句应该总是出现在脚本的开头,因为在代码中(尤其是在函数中)导入通常是不必要的,而且常常会导致意想不到的问题;对于常见用途,唯一可接受的例外是在
__name__
检查中执行此操作; - 编辑
pyuic
生成的文件被认为是不好的 做法,永远不应该,永远 除非你真的知道你在做什么(如果你做知道,你可能不会做);这些文件是专门导入的,如官方 guidelines about 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_())
由于您已经在浏览器 window 中使用 uic
,因此您可以跳过 pyuic
部分,并在登录 window 中使用相同的部分还有:
class LoginWindow(<b>QtWidgets.QWidget</b>):
def __init__(self):
super().__init__()
uic.loadUi('loginForm.ui', self)
self.pushButton.clicked.connect(self.login)
# ...