赞
踩
在之前通过使用MySQL和对象结合的方式设计学生管理系统的基础上进一步对系统的界面进行优化,使用了PyQT5设计图形界面完成了管理系统的升级和迭代。使用工具包括: PyCharm、pyuic插件、QT Designer等,具体代码如下,仅供参考。
本次由于存在3个窗口,代码分为3部分,分别为主窗口、添加学生信息窗口、修改学生信息窗口
·主窗口
- import sys
-
- from PyQt5.QtCore import Qt
- from PyQt5.QtGui import QStandardItem, QStandardItemModel
- from PyQt5.QtWidgets import QApplication, QMainWindow, QHeaderView, QAbstractItemView, QTableView, QPushButton, \
- QVBoxLayout, QWidget, QDialog, QMessageBox, QLabel, QTextEdit
- # 数据库处理模块
- # 导入pymysql模块
- import pymysql
- # 连接database
- from PyQt5.uic.properties import QtWidgets, QtCore
-
- from add_frame import Ui_Form
- from modify import UI_Modify
-
- conn = pymysql.connect(host="localhost", user="root",password="yan19991001",database="test")
- # 得到一个可以执行SQL语句的光标对象
- cursor = conn.cursor()
-
- rowIndex = 0 #记录行数
-
- class My_Window:
- def __init__(self, mainWindow):
- # 创建一个 widget 组件
- widget = QWidget()
- mainWindow.setCentralWidget(widget)
- # 创建一个垂直盒布局,并放在widget里面
- self.vboxlayout = QVBoxLayout(widget)
-
- # 函数调用 初始化 tableView
- self.tableView_init()
- # 调用 button 组件初始化函数
- self.button_init()
- # 展示主窗口
- mainWindow.show()
-
- def button_init(self):
- # 创建一个按钮
- self.refresh = QPushButton('刷新数据')
- # 将按钮添加到垂直盒布局里
- self.vboxlayout.addWidget(self.refresh)
- # 按钮组件连接槽函数
- self.refresh.clicked.connect(self.showStu)
-
- # 创建一个按钮
- self.button1 = QPushButton('删除学生信息')
- # 将按钮添加到垂直盒布局里
- self.vboxlayout.addWidget(self.button1)
- # 按钮组件连接槽函数
- self.button1.clicked.connect(self.delStu)
-
- # 创建一个按钮
- self.button3 = QPushButton('添加学生信息')
- # 将按钮添加到垂直盒布局里
- self.vboxlayout.addWidget(self.button3)
- # 按钮组件连接槽函数
- self.button3.clicked.connect(self.addStu)
-
- # 创建一个按钮
- self.modify = QPushButton('修改学生信息')
- # 将按钮添加到垂直盒布局里
- self.vboxlayout.addWidget(self.modify)
- # 按钮组件连接槽函数
- self.modify.clicked.connect(self.modStu)
-
- # 创建Label
- self.label = QLabel()
- self.label.setText("请输入要查询的学号:")
- # 创建EditText
- self.textEdit = QTextEdit()
- self.textEdit.setObjectName("textEdit")
- # 将按钮添加到垂直盒布局里
- self.vboxlayout.addWidget(self.label)
- self.vboxlayout.addWidget(self.textEdit)
-
- # 创建一个按钮
- self.search = QPushButton('查询学生信息')
- # 将按钮添加到垂直盒布局里
- self.vboxlayout.addWidget(self.search)
- # 按钮组件连接槽函数
- self.search.clicked.connect(self.seaStu)
-
- def tableView_init(self):
- # 设置数据层次结构,4行4列
- self.model = QStandardItemModel(0, 6)
- # 设置水平方向四个头标签文本内容
- self.model.setHorizontalHeaderLabels(['编号', '姓名', '性别', '年龄', '院系', '专业'])
-
- # 创建 tableView 组件
- self.tableview = QTableView()
- # 将 tableView 添加到垂直盒布局里
- self.vboxlayout.addWidget(self.tableview)
-
- # tableView 组件 设置模型
- self.tableview.setModel(self.model)
-
- self.tableview.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch) # 所有列自动拉伸,充满界面
- self.tableview.setSelectionMode(QAbstractItemView.SingleSelection) # 设置只能选中整行
- self.tableview.setEditTriggers(QTableView.NoEditTriggers) # 不可编辑
- self.tableview.setSelectionBehavior(QAbstractItemView.SelectRows) # 设置只能选中一行
-
- def tableView_clear(self):
- # 会全部清空,包括那个标准表头
- self.model.clear()
- # 所以重新设置标准表头 自己将一下代码注释 尝试
- self.model.setHorizontalHeaderLabels(['编号', '姓名', '性别', '年龄', '院系', '专业'])
-
- # 刷新现有数据
- def showStu(self):
- sql = str("select * from students")
- # 执行SQL语句
- cursor.execute(sql)
- results = cursor.fetchall() # 获取查询结果
- conn.commit()
- # 清除表内数据
- self.model.clear()
- # 所以重新设置标准表头 自己将一下代码注释 尝试
- self.model.setHorizontalHeaderLabels(['编号', '姓名', '性别', '年龄', '院系', '专业'])
-
- for row in results:
- self.model.appendRow([QStandardItem('%s' % row[0]),
- QStandardItem('%s' % row[1]),
- QStandardItem('%s' % row[2]),
- QStandardItem('%s' % row[3]),
- QStandardItem('%s' % row[4]),
- QStandardItem('%s' % row[5])
- ])
-
-
-
- # 从数据库删除当前已选中行 - 根据编号
- def delStu(self):
- index = self.model.itemData(self.model.index(self.tableview.currentIndex().row(), 0)) # 选中行的第一列数据,也即编号 返回的为字典类型
- sql = str("delete from students where id = %s" % index.get(0)) #获取字典的第一条数据
- # 执行SQL语句
- cursor.execute(sql)
- conn.commit()
-
- # 删除表格上的数据
- index = self.tableview.currentIndex() # 取得当前选中行的index
- self.model.removeRow(index.row()) # 通过index的row()操作得到行数进行删除
- self.show_message() # 打印提示信息
- print("删除成功!")
-
- def addStu(self):
- form = QDialog()
- ui2 = Ui_Form()
- ui2.setupUi(form)
- form.setWindowModality(Qt.NonModal) # 非模态,可与其他窗口交互
- # form.setWindowModality(Qt.WindowModal) # 窗口模态,当前未处理完,阻止与父窗口交互
- # form.setWindowModality(Qt.ApplicationModal) # 应用程序模态,阻止与任何其他窗口交互
- form.show()
- form.exec_()
-
- def modStu(self):
- form = QDialog()
- ui2 = UI_Modify()
- ui2.setupUi(form)
- form.setWindowModality(Qt.NonModal) # 非模态,可与其他窗口交互
- # form.setWindowModality(Qt.WindowModal) # 窗口模态,当前未处理完,阻止与父窗口交互
- # form.setWindowModality(Qt.ApplicationModal) # 应用程序模态,阻止与任何其他窗口交互
- form.show()
- form.exec_()
-
- def show_message(self):
- msg_box = QMessageBox(QMessageBox.Information, '提示', '删除成功!')
- msg_box.exec_()
-
- def seaStu(self):
- sql = str("select * from students where id = %s" % self.textEdit.toPlainText())
- # 执行SQL语句
- cursor.execute(sql)
- results = cursor.fetchall() # 获取查询结果
- conn.commit()
- # 清除表内数据
- self.model.clear()
- # 所以重新设置标准表头 自己将一下代码注释 尝试
- self.model.setHorizontalHeaderLabels(['编号', '姓名', '性别', '年龄', '院系', '专业'])
-
- for row in results:
- self.model.appendRow([QStandardItem('%s' % row[0]),
- QStandardItem('%s' % row[1]),
- QStandardItem('%s' % row[2]),
- QStandardItem('%s' % row[3]),
- QStandardItem('%s' % row[4]),
- QStandardItem('%s' % row[5])
- ])
-
-
-
- if __name__ == '__main__':
- app = QApplication(sys.argv)
-
- # 现在这创建 主窗口 (不然会闪退) 不然就继承
- mainWindow = QMainWindow()
- mainWindow.setWindowTitle('学生管理系统v2.3')
- mainWindow.resize(500, 600)
-
- # 一定要创建个实例出来 要有个 a 名字随便你取
- a = My_Window(mainWindow)
-
- sys.exit(app.exec_())
·添加学生信息
- # -*- coding: utf-8 -*-
-
- # Form implementation generated from reading ui file 'form.ui'
- #
- # Created by: PyQt5 UI code generator 5.15.4
- #
- # WARNING: Any manual changes made to this file will be lost when pyuic5 is
- # run again. Do not edit this file unless you know what you are doing.
- import sys
-
- from PyQt5 import QtCore, QtGui, QtWidgets
- # 数据库处理模块
- # 导入pymysql模块
- import pymysql
- # 连接database
- from PyQt5.QtWidgets import QMessageBox, QApplication
-
- conn = pymysql.connect(host="localhost", user="root",password="yan19991001",database="test")
- # 得到一个可以执行SQL语句的光标对象
- cursor = conn.cursor()
-
-
- class Ui_Form(object):
- def setupUi(self, Form):
- Form.setObjectName("Form")
- Form.resize(445, 371)
- self.textEdit = QtWidgets.QTextEdit(Form)
- self.textEdit.setGeometry(QtCore.QRect(180, 30, 131, 31))
- self.textEdit.setObjectName("textEdit")
- self.textEdit_4 = QtWidgets.QTextEdit(Form)
- self.textEdit_4.setGeometry(QtCore.QRect(180, 190, 131, 31))
- self.textEdit_4.setObjectName("textEdit_4")
- self.label_6 = QtWidgets.QLabel(Form)
- self.label_6.setGeometry(QtCore.QRect(130, 230, 41, 21))
- self.label_6.setObjectName("label_6")
- self.textEdit_2 = QtWidgets.QTextEdit(Form)
- self.textEdit_2.setGeometry(QtCore.QRect(180, 70, 131, 31))
- self.textEdit_2.setObjectName("textEdit_2")
- self.textEdit_5 = QtWidgets.QTextEdit(Form)
- self.textEdit_5.setGeometry(QtCore.QRect(180, 230, 131, 31))
- self.textEdit_5.setObjectName("textEdit_5")
- self.label_4 = QtWidgets.QLabel(Form)
- self.label_4.setGeometry(QtCore.QRect(130, 190, 41, 21))
- self.label_4.setObjectName("label_4")
- self.label_2 = QtWidgets.QLabel(Form)
- self.label_2.setGeometry(QtCore.QRect(130, 110, 41, 21))
- self.label_2.setObjectName("label_2")
- self.label_5 = QtWidgets.QLabel(Form)
- self.label_5.setGeometry(QtCore.QRect(130, 150, 41, 21))
- self.label_5.setObjectName("label_5")
- self.textEdit_3 = QtWidgets.QTextEdit(Form)
- self.textEdit_3.setGeometry(QtCore.QRect(180, 110, 131, 31)) #设置位置
- self.textEdit_3.setObjectName("textEdit_3")
- self.label = QtWidgets.QLabel(Form)
- self.label.setGeometry(QtCore.QRect(130, 30, 41, 21))
- self.label.setObjectName("label")
- self.label_3 = QtWidgets.QLabel(Form)
- self.label_3.setGeometry(QtCore.QRect(130, 70, 41, 21))
- self.label_3.setObjectName("label_3")
- self.textEdit_6 = QtWidgets.QTextEdit(Form)
- self.textEdit_6.setGeometry(QtCore.QRect(180, 150, 131, 31))
- self.textEdit_6.setObjectName("textEdit_6")
-
- self.pushButton = QtWidgets.QPushButton(Form)
- self.pushButton.setGeometry(QtCore.QRect(130, 300, 75, 23))
- self.pushButton.setObjectName("pushButton")
- # 按钮组件连接槽函数
- self.pushButton.clicked.connect(self.addStudent)
-
- self.pushButton_2 = QtWidgets.QPushButton(Form)
- self.pushButton_2.setGeometry(QtCore.QRect(230, 300, 75, 23))
- self.pushButton_2.setObjectName("pushButton_2")
- # 按钮组件连接槽函数
- self.pushButton_2.clicked.connect(self.refresh)
-
- self.retranslateUi(Form)
- QtCore.QMetaObject.connectSlotsByName(Form)
-
- def retranslateUi(self, Form):
- _translate = QtCore.QCoreApplication.translate
- Form.setWindowTitle(_translate("Form", "添加学生信息"))
- self.label_6.setText(_translate("Form", "专业"))
- self.label_4.setText(_translate("Form", "院系"))
- self.label_2.setText(_translate("Form", "性别"))
- self.label_5.setText(_translate("Form", "年龄"))
- self.label.setText(_translate("Form", "学号"))
- self.label_3.setText(_translate("Form", "姓名"))
- self.pushButton.setText(_translate("Form", "确定"))
- self.pushButton_2.setText(_translate("Form", "重置"))
-
- # textEdit textEdit_2 textEdit_3 textEdit_6 textEdit_4 textEdit_5
- # 由于生成的时候打乱了,以上为实际录入的数据顺序
- def addStudent(self):
- sql = str("insert into students values('%s','%s','%s','%s','%s','%s')" % (
- self.textEdit.toPlainText(), self.textEdit_2.toPlainText(), self.textEdit_3.toPlainText(), self.textEdit_6.toPlainText(), self.textEdit_4.toPlainText(), self.textEdit_5.toPlainText()))
- print(sql)
- # 执行SQL语句
- cursor.execute(sql)
- conn.commit()
- self.show_message() # 打印提示信息
-
- def show_message(self):
- msg_box = QMessageBox(QMessageBox.Information, '提示', '添加成功!')
- msg_box.exec_()
-
- def refresh(self):
- self.textEdit.setText("")
- self.textEdit_2.setText("")
- self.textEdit_3.setText("")
- self.textEdit_4.setText("")
- self.textEdit_5.setText("")
- self.textEdit_6.setText("")
-
- if __name__ == "__main__":
- import sys
- app = QtWidgets.QApplication(sys.argv)
- mainWindow = QtWidgets.QMainWindow()
- ui = Ui_Form()
- ui.setupUi(mainWindow)
- mainWindow.show()
- sys.exit(app.exec_())
·修改学生信息
- # -*- coding: utf-8 -*-
-
- # Form implementation generated from reading ui file 'form.ui'
- #
- # Created by: PyQt5 UI code generator 5.15.4
- #
- # WARNING: Any manual changes made to this file will be lost when pyuic5 is
- # run again. Do not edit this file unless you know what you are doing.
- import sys
-
- from PyQt5 import QtCore, QtGui, QtWidgets
- # 数据库处理模块
- # 导入pymysql模块
- import pymysql
- # 连接database
- from PyQt5.QtWidgets import QMessageBox, QApplication
-
- conn = pymysql.connect(host="localhost", user="root",password="yan19991001",database="test")
- # 得到一个可以执行SQL语句的光标对象
- cursor = conn.cursor()
-
-
- class UI_Modify(object):
- def setupUi(self, Form):
- Form.setObjectName("Form")
- Form.resize(445, 371)
- self.textEdit = QtWidgets.QTextEdit(Form)
- self.textEdit.setGeometry(QtCore.QRect(180, 30, 131, 31))
- self.textEdit.setObjectName("textEdit")
- self.textEdit_4 = QtWidgets.QTextEdit(Form)
- self.textEdit_4.setGeometry(QtCore.QRect(180, 190, 131, 31))
- self.textEdit_4.setObjectName("textEdit_4")
- self.label_6 = QtWidgets.QLabel(Form)
- self.label_6.setGeometry(QtCore.QRect(130, 230, 41, 21))
- self.label_6.setObjectName("label_6")
- self.textEdit_2 = QtWidgets.QTextEdit(Form)
- self.textEdit_2.setGeometry(QtCore.QRect(180, 70, 131, 31))
- self.textEdit_2.setObjectName("textEdit_2")
- self.textEdit_5 = QtWidgets.QTextEdit(Form)
- self.textEdit_5.setGeometry(QtCore.QRect(180, 230, 131, 31))
- self.textEdit_5.setObjectName("textEdit_5")
- self.label_4 = QtWidgets.QLabel(Form)
- self.label_4.setGeometry(QtCore.QRect(130, 190, 41, 21))
- self.label_4.setObjectName("label_4")
- self.label_2 = QtWidgets.QLabel(Form)
- self.label_2.setGeometry(QtCore.QRect(130, 110, 41, 21))
- self.label_2.setObjectName("label_2")
- self.label_5 = QtWidgets.QLabel(Form)
- self.label_5.setGeometry(QtCore.QRect(130, 150, 41, 21))
- self.label_5.setObjectName("label_5")
- self.textEdit_3 = QtWidgets.QTextEdit(Form)
- self.textEdit_3.setGeometry(QtCore.QRect(180, 110, 131, 31)) #设置位置
- self.textEdit_3.setObjectName("textEdit_3")
- self.label = QtWidgets.QLabel(Form)
- self.label.setGeometry(QtCore.QRect(130, 30, 41, 21))
- self.label.setObjectName("label")
- self.label_3 = QtWidgets.QLabel(Form)
- self.label_3.setGeometry(QtCore.QRect(130, 70, 41, 21))
- self.label_3.setObjectName("label_3")
- self.textEdit_6 = QtWidgets.QTextEdit(Form)
- self.textEdit_6.setGeometry(QtCore.QRect(180, 150, 131, 31))
- self.textEdit_6.setObjectName("textEdit_6")
-
- self.pushButton = QtWidgets.QPushButton(Form)
- self.pushButton.setGeometry(QtCore.QRect(130, 300, 75, 23))
- self.pushButton.setObjectName("pushButton")
- # 按钮组件连接槽函数
- self.pushButton.clicked.connect(self.modStudent)
-
- self.pushButton_2 = QtWidgets.QPushButton(Form)
- self.pushButton_2.setGeometry(QtCore.QRect(230, 300, 75, 23))
- self.pushButton_2.setObjectName("pushButton_2")
- # 按钮组件连接槽函数
- self.pushButton_2.clicked.connect(self.refresh)
-
- self.retranslateUi(Form)
- QtCore.QMetaObject.connectSlotsByName(Form)
-
- def retranslateUi(self, Form):
- _translate = QtCore.QCoreApplication.translate
- Form.setWindowTitle(_translate("Form", "修改学生信息"))
- self.label_6.setText(_translate("Form", "专业"))
- self.label_4.setText(_translate("Form", "院系"))
- self.label_2.setText(_translate("Form", "性别"))
- self.label_5.setText(_translate("Form", "年龄"))
- self.label.setText(_translate("Form", "学号"))
- self.label_3.setText(_translate("Form", "姓名"))
- self.pushButton.setText(_translate("Form", "确定"))
- self.pushButton_2.setText(_translate("Form", "重置"))
-
- # textEdit textEdit_2 textEdit_3 textEdit_6 textEdit_4 textEdit_5
- # 由于生成的时候打乱了,以上为实际录入的数据顺序
- def modStudent(self):
- sql = str("update students set name = %s,sex = %s,age = %s,depart = %s,major = %s where id = %s" % (
- self.textEdit_2.toPlainText(), self.textEdit_3.toPlainText(),self.textEdit_6.toPlainText(), self.textEdit_4.toPlainText(), self.textEdit_5.toPlainText() , self.textEdit.toPlainText()))
- print(sql)
- # 执行SQL语句
- cursor.execute(sql)
- conn.commit()
- self.show_message() # 打印提示信息
-
- def show_message(self):
- msg_box = QMessageBox(QMessageBox.Information, '提示', '修改成功!')
- msg_box.exec_()
-
- def refresh(self):
- self.textEdit.setText("")
- self.textEdit_2.setText("")
- self.textEdit_3.setText("")
- self.textEdit_4.setText("")
- self.textEdit_5.setText("")
- self.textEdit_6.setText("")
-
- if __name__ == "__main__":
- import sys
- app = QtWidgets.QApplication(sys.argv)
- mainWindow = QtWidgets.QMainWindow()
- ui = UI_Modify()
- ui.setupUi(mainWindow)
- mainWindow.show()
- sys.exit(app.exec_())
PyQT5 实质上是QT对Python的专用版,相似于C++的MFC编程,它可以使用QT Designer等UI设计工具设计界面,大大简化手动书写代码的繁琐度,提高开发效率。
开发该系统大概耗费6小时左右,学习难度适中。由于没有系统学习,可能对PyQT的了解不深,代码质量欠佳,望多多指正,谢谢大家。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。