所以我正在尝试使用PyQt5和Qt设计器构建MySQL数据库编辑器 并且我已经定义了一个按钮以运行用于连接的自定义方法,但是每次按此按钮都不会发生连接时,应用会关闭,然后我得到“处理结束,退出代码为-1073740791(0xC0000409)”
我尝试过的事情:
from PyQt5 import QtCore, QtGui, QtWidgets
from mysql.connector import connect
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(350, 200)
Form.setMinimumSize(QtCore.QSize(350, 200))
Form.setMaximumSize(QtCore.QSize(350, 200))
Form.setSizeIncrement(QtCore.QSize(0, 0))
Form.setBaseSize(QtCore.QSize(0, 0))
Form.setMouseTracking(False)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("proxy.duckduckgo.com.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
Form.setWindowIcon(icon)
self.gridLayout = QtWidgets.QGridLayout(Form)
self.gridLayout.setObjectName("gridLayout")
self.PortLabel = QtWidgets.QLabel(Form)
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setPointSize(10)
self.PortLabel.setFont(font)
self.PortLabel.setObjectName("PortLabel")
self.gridLayout.addWidget(self.PortLabel, 1, 0, 1, 1)
self.CancelBtn = QtWidgets.QPushButton(Form)
self.CancelBtn.setObjectName("CancelBtn")
self.CancelBtn.clicked.connect(self.exit)
self.gridLayout.addWidget(self.CancelBtn, 5, 2, 1, 1)
self.DBLabel = QtWidgets.QLabel(Form)
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setPointSize(10)
self.DBLabel.setFont(font)
self.DBLabel.setObjectName("DBLabel")
self.gridLayout.addWidget(self.DBLabel, 4, 0, 1, 1)
self.UserLabel = QtWidgets.QLabel(Form)
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setPointSize(10)
self.UserLabel.setFont(font)
self.UserLabel.setObjectName("UserLabel")
self.gridLayout.addWidget(self.UserLabel, 2, 0, 1, 1)
self.HostLabel = QtWidgets.QLabel(Form)
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setPointSize(10)
self.HostLabel.setFont(font)
self.HostLabel.setObjectName("HostLabel")
self.gridLayout.addWidget(self.HostLabel, 0, 0, 1, 1)
self.PassLabel = QtWidgets.QLabel(Form)
font = QtGui.QFont()
font.setFamily("Segoe UI")
font.setPointSize(10)
self.PassLabel.setFont(font)
self.PassLabel.setObjectName("PassLabel")
self.gridLayout.addWidget(self.PassLabel, 3, 0, 1, 1)
self.SubmitBtn = QtWidgets.QPushButton(Form)
self.SubmitBtn.setObjectName("SubmitBtn")
self.SubmitBtn.clicked.connect(self.connect_to_database)
self.gridLayout.addWidget(self.SubmitBtn, 5, 3, 1, 1)
self.HostLineEdit = QtWidgets.QLineEdit(Form)
self.HostLineEdit.setObjectName("HostLineEdit")
self.gridLayout.addWidget(self.HostLineEdit, 0, 1, 1, 3)
self.PortLineEdit = QtWidgets.QLineEdit(Form)
self.PortLineEdit.setObjectName("PortLineEdit")
self.gridLayout.addWidget(self.PortLineEdit, 1, 1, 1, 3)
self.UserLineEdit = QtWidgets.QLineEdit(Form)
self.UserLineEdit.setObjectName("UserLineEdit")
self.gridLayout.addWidget(self.UserLineEdit, 2, 1, 1, 3)
self.PassLineEdit = QtWidgets.QLineEdit(Form)
self.PassLineEdit.setObjectName("PassLineEdit")
self.gridLayout.addWidget(self.PassLineEdit, 3, 1, 1, 3)
self.DBLineEdit = QtWidgets.QLineEdit(Form)
self.DBLineEdit.setObjectName("DBLineEdit")
self.gridLayout.addWidget(self.DBLineEdit, 4, 1, 1, 3)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Connection Details"))
self.PortLabel.setText(_translate("Form", "<html><head/><body><p align=\"center\">Port</p></body></html>"))
self.CancelBtn.setText(_translate("Form", "Cancel"))
self.DBLabel.setText(_translate("Form", "<html><head/><body><p align=\"center\">Database</p></body></html>"))
self.UserLabel.setText(_translate("Form", "<html><head/><body><p align=\"center\">Username</p></body></html>"))
self.HostLabel.setText(_translate("Form", "<html><head/><body><p align=\"center\">Host</p></body></html>"))
self.PassLabel.setText(_translate("Form", "<html><head/><body><p align=\"center\">Password</p></body></html>"))
self.SubmitBtn.setText(_translate("Form", "Submit"))
def connect_to_database(self):
connect(host=self.HostLineEdit,
port=self.PortLineEdit,
user=self.UserLineEdit,
passwd=self.PassLineEdit,
database=self.DBLineEdit)
print('Connected')
def exit(self):
sys.exit()
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_())