这个项目还凑合,我准备发GitHub上,然后可能会用nuitka打包什么的
毕竟这是自己的第一个gui项目啦~~
(以后努力学js DOM的话,也算是gui吧)
这个项目有两个漏洞:
1.在删除按钮旁边输入0.8这样,或者其他乱七八糟的东西,可能会报错!-----这个错误我懒得整了
2.还有就是增加行或者删除行的时候有点小问题 ----这个我也不知道咋说,就那样吧
rom PyQt5 import QtCore,QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setEnabled(True)
MainWindow.resize(975, 726)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(10, 20, 111, 41))
self.label.setObjectName("label")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(130, 30, 101, 31))
self.lineEdit.setObjectName("lineEdit")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(280, 30, 72, 15))
self.label_2.setObjectName("label_2")
self.comboBox = QtWidgets.QComboBox(self.centralwidget)
self.comboBox.setGeometry(QtCore.QRect(340, 20, 151, 22))
self.comboBox.setObjectName("comboBox")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(541, 30, 151, 21))
self.label_3.setObjectName("label_3")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(700, 30, 251, 21))
self.lineEdit_2.setObjectName("lineEdit_2")
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(11, 100, 111, 20))
self.label_4.setObjectName("label_4")
self.lineEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_3.setGeometry(QtCore.QRect(130, 90, 101, 31))
self.lineEdit_3.setObjectName("lineEdit_3")
self.label_6 = QtWidgets.QLabel(self.centralwidget)
self.label_6.setGeometry(QtCore.QRect(640, 110, 72, 15))
self.label_6.setObjectName("label_6")
self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
self.textEdit.setGeometry(QtCore.QRect(700, 70, 251, 81))
self.textEdit.setObjectName("textEdit")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(370, 70, 71, 61))
self.pushButton.setObjectName("pushButton")
self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
self.tableWidget.setGeometry(QtCore.QRect(170, 180, 521, 491))
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(5)
self.tableWidget.setRowCount(1)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setVerticalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(1, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(2, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(3, item)
item = QtWidgets.QTableWidgetItem()
self.tableWidget.setHorizontalHeaderItem(4, item)
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setGeometry(QtCore.QRect(790, 450, 71, 71))
self.pushButton_2.setObjectName("pushButton_2")
self.lineEdit_4 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_4.setGeometry(QtCore.QRect(880, 400, 51, 31))
self.lineEdit_4.setObjectName("lineEdit_4")
self.label_5 = QtWidgets.QLabel(self.centralwidget)
self.label_5.setGeometry(QtCore.QRect(700, 400, 171, 31))
self.label_5.setObjectName("label_5")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 975, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
self.pushButton.clicked.connect(MainWindow.add) # type: ignore
self.pushButton_2.clicked.connect(MainWindow.dell) # type: ignore
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "姓名(仅字母)"))
self.label_2.setText(_translate("MainWindow", "性别"))
self.comboBox.setItemText(0, _translate("MainWindow", "男"))
self.comboBox.setItemText(1, _translate("MainWindow", "女"))
self.label_3.setText(_translate("MainWindow", "身份证号(仅数字)"))
self.label_4.setText(_translate("MainWindow", "专业(仅字母)"))
self.label_6.setText(_translate("MainWindow", "备注"))
self.pushButton.setText(_translate("MainWindow", "添加"))
item = self.tableWidget.verticalHeaderItem(0)
item.setText(_translate("MainWindow", "1"))
item = self.tableWidget.horizontalHeaderItem(0)
item.setText(_translate("MainWindow", "姓名"))
item = self.tableWidget.horizontalHeaderItem(1)
item.setText(_translate("MainWindow", "性别"))
item = self.tableWidget.horizontalHeaderItem(2)
item.setText(_translate("MainWindow", "专业"))
item = self.tableWidget.horizontalHeaderItem(3)
item.setText(_translate("MainWindow", "身份证号"))
item = self.tableWidget.horizontalHeaderItem(4)
item.setText(_translate("MainWindow", "备注"))
self.pushButton_2.setText(_translate("MainWindow", "删除"))
self.label_5.setText(_translate("MainWindow", "输入删除的行(仅数字)"))
import sys
from PyQt5.QtWidgets import *
from uach import *
class My(QMainWindow,Ui_MainWindow):
def init(self,parent=None):
super(My, self).init(parent)
self.setupUi(self)
def add(self):
a1 = self.lineEdit.text()
a2 = self.lineEdit_2.text()
a3 = self.lineEdit_3.text()
x = self.comboBox.currentText()
y = self.textEdit.toPlainText()
pp = [a1,x,a3,a2,y]
if not a1.isalpha():
QMessageBox.about(self, '输入姓名警告', '只能有字母(包括汉字)')
elif not a2.isdigit():
QMessageBox.about(self,'注意身份证号','只能输入数字')
elif not 5 < len(a2) <= 18:
QMessageBox.about(self,'注意身份证号','需要6~18位')
elif len(a3) > 5:
QMessageBox.about(self,'输入专业警告','不能超过5字符')
elif not a3.isalpha():
QMessageBox.about(self,'输入专业警告','只能有字母(包括汉字)')
else:
for i in range(0,5):
self.tableWidget.setItem(self.tableWidget.rowCount()-1, i,
QTableWidgetItem(pp[i]))
self.tableWidget.setRowCount(self.tableWidget.rowCount()+1)
def dell(self):
x = self.lineEdit_4.text()
# if (not x.isdigit()) or (x % 1 != 0):
# QMessageBox.about(self, '要求', '只能输入整数')
if not (0 < int(x) <= self.tableWidget.rowCount()):
# 把不是整数或数字的转换成int
QMessageBox.about(self, '要求!', '不能超出范围')
else:
#把那一行的值都设为空
# for i in range(0,5):
# self.tableWidget.setItem(,i,QTableWidgetItem(' '))
self.tableWidget.removeRow(int(x)-1)
if name == 'main':
app = QApplication(sys.argv)
m = My()
m.show()
sys.exit(app.exec())
那两个函数是我写了挺久的函数,算比较重要!