我创建了一个QInputDialog,在用户按下(删除按钮)后出现,通过这个对话框屏幕,用户可以输入要删除的学号。如何获取用户输入的值(学号)?
代码: 从 PyQt5 导入 MySQLdb 作为 mdb 从 PyQt5.QtWidgets 导入 QtCore、QtGui、QtWidgets、uic 从 PyQt5.QtCore 导入 QWidget、QPushButton、QLineEdit、QInputDialog、QApplication、QLabel 从 PyQt5.QtGui 导入 QSize 从 PyQt5.QtGui 导入 QIcon 导入 sys
类 Ui_StudentRecords(对象):
def add_student_record(self):
student_num=self.lineEdit.text()
student_name=self.lineEdit_2.text()
student_avg=self.lineEdit_3.text()
db=mdb.connect('localhost','ahmadsarairah','Tuned@2022','studentrecord')
cursor1=db.cursor()
sql='insert into studentrecords(id,name,avg)values(%s,%s,%s)'
val=(student_num,student_name,student_avg)
cursor1.execute(sql,val)
db.commit()
cursor1.close()
db.close()
self.msg=QtWidgets.QMessageBox()
self.msg.setIcon(QMessageBox.Information)
self.msg.setText("Student Added Successfully")
self.msg.setWindowTitle("Add Student ")
self.msg.setStandardButtons(QMessageBox.Ok)
self.msg.exec()
def show_Delete_dialog(self):
self.dialog=QtWidgets.QInputDialog()
self.dialog.setWindowTitle("QInputDialog")
self.dialog.setLabelText("Enter Value")
self.dialog.setOkButtonText("OK")
self.dialog.setCancelButtonText("Cancel")
self.dialog.show()
def exit_studentrecords_window(self):
self.window=QtWidgets.QMainWindow()
self.window.destroyed()
def clear_texts(self):
self.lineEdit.clear()
self.lineEdit_2.clear()
self.lineEdit_3.clear()
def setupUi(self, StudentRecords):
StudentRecords.setObjectName("StudentRecords")
StudentRecords.resize(550, 600)
StudentRecords.setMinimumSize(QtCore.QSize(550, 600))
StudentRecords.setMaximumSize(QtCore.QSize(550, 600))
StudentRecords.setStyleSheet("background-color: rgb(211, 211, 158);")
self.centralwidget = QtWidgets.QWidget(StudentRecords)
self.centralwidget.setObjectName("centralwidget")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(110, 100, 300, 50))
font = QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.lineEdit.setFont(font)
self.lineEdit.setStyleSheet("color: rgb(0, 0, 0);\n"
"color: rgb(0, 0, 0);")
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(110, 200, 301, 50))
font = QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.lineEdit_2.setFont(font)
self.lineEdit_2.setObjectName("lineEdit_2")
self.lineEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_3.setGeometry(QtCore.QRect(110, 300, 300, 50))
font = QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.lineEdit_3.setFont(font)
self.lineEdit_3.setObjectName("lineEdit_3")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(170, 30, 281, 20))
font = QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(18)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setStyleSheet("color: rgb(0, 0, 0);")
self.label.setObjectName("label")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(110, 390, 125, 30))
font = QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.pushButton.setFont(font)
self.pushButton.setStyleSheet("color: rgb(0, 0, 0);")
self.pushButton.setObjectName("pushButton")
self.pushButton.clicked.connect(self.add_student_record)
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(290, 390, 125, 30))
font = QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.pushButton_2.setFont(font)
self.pushButton_2.setStyleSheet("color: rgb(0, 0, 0);")
self.pushButton_2.setObjectName("pushButton_2")
self.pushButton_2.clicked.connect(self.show_Delete_dialog)
self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_3.setGeometry(QtCore.QRect(110, 450, 125, 30))
font = QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.pushButton_3.setFont(font)
self.pushButton_3.setStyleSheet("color: rgb(0, 0, 0);")
self.pushButton_3.setObjectName("pushButton_3")
self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_4.setGeometry(QtCore.QRect(290, 450, 125, 30))
font = QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.pushButton_4.setFont(font)
self.pushButton_4.setStyleSheet("color: rgb(0, 0, 0);")
self.pushButton_4.setObjectName("pushButton_4")
self.pushButton_5 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_5.setGeometry(QtCore.QRect(109, 510, 125, 30))
font = QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.pushButton_5.setFont(font)
self.pushButton_5.setStyleSheet("color: rgb(0, 0, 0);")
self.pushButton_5.setObjectName("pushButton_5")
self.pushButton_5.clicked.connect(self.exit_studentrecords_window)
self.pushButton_6 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_6.setGeometry(QtCore.QRect(290, 510, 125, 30))
font = QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.pushButton_6.setFont(font)
self.pushButton_6.setStyleSheet("color: rgb(0, 0, 0);")
self.pushButton_6.setObjectName("pushButton_6")
self.pushButton_6.clicked.connect(self.clear_texts)
StudentRecords.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(StudentRecords)
self.menubar.setGeometry(QtCore.QRect(0, 0, 550, 21))
self.menubar.setObjectName("menubar")
StudentRecords.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(StudentRecords)
self.statusbar.setObjectName("statusbar")
StudentRecords.setStatusBar(self.statusbar)
self.retranslateUi(StudentRecords)
QtCore.QMetaObject.connectSlotsByName(StudentRecords)
def retranslateUi(self, StudentRecords):
_translate = QtCore.QCoreApplication.translate
StudentRecords.setWindowTitle(_translate("StudentRecords", "StudentRecords"))
self.lineEdit.setPlaceholderText(_translate("StudentRecords", "Enter Student Number"))
self.lineEdit_2.setPlaceholderText(_translate("StudentRecords", "Enter Student Name"))
self.lineEdit_3.setPlaceholderText(_translate("StudentRecords", "Enter Student Average"))
self.label.setText(_translate("StudentRecords", "Student Records"))
self.pushButton.setText(_translate("StudentRecords", "Add Student Info"))
self.pushButton_2.setText(_translate("StudentRecords", "Delete Student info"))
self.pushButton_3.setText(_translate("StudentRecords", "Update Student Info"))
self.pushButton_4.setText(_translate("StudentRecords", "Display All students"))
self.pushButton_5.setText(_translate("StudentRecords", "Exit"))
self.pushButton_6.setText(_translate("StudentRecords", "Clear"))
if name == " main ": import sys app = QtWidgets.QApplication(sys.argv) StudentRecords = QtWidgets.QMainWindow() ui = Ui_StudentRecords() ui.setupUi(StudentRecords) StudentRecords.show() sys.exit(app.exec_ ())