当前位置:   article > 正文

Python实现学生管理系统v2.3(面向对象+MySQL+PyQT5)_python 基于pyqt的管理系统

python 基于pyqt的管理系统

实现功能

  • 添加学生
  • 删除学生
  • 修改学生
  • 查询学生
  • 显示所有学生信息
  • 图形界面

简述

        在之前通过使用MySQL和对象结合的方式设计学生管理系统的基础上进一步对系统的界面进行优化,使用了PyQT5设计图形界面完成了管理系统的升级和迭代。使用工具包括: PyCharm、pyuic插件、QT Designer等,具体代码如下,仅供参考。

运行截图

 

 

 

 

 代码

 本次由于存在3个窗口,代码分为3部分,分别为主窗口、添加学生信息窗口、修改学生信息窗口

·主窗口

  

  1. import sys
  2. from PyQt5.QtCore import Qt
  3. from PyQt5.QtGui import QStandardItem, QStandardItemModel
  4. from PyQt5.QtWidgets import QApplication, QMainWindow, QHeaderView, QAbstractItemView, QTableView, QPushButton, \
  5. QVBoxLayout, QWidget, QDialog, QMessageBox, QLabel, QTextEdit
  6. # 数据库处理模块
  7. # 导入pymysql模块
  8. import pymysql
  9. # 连接database
  10. from PyQt5.uic.properties import QtWidgets, QtCore
  11. from add_frame import Ui_Form
  12. from modify import UI_Modify
  13. conn = pymysql.connect(host="localhost", user="root",password="yan19991001",database="test")
  14. # 得到一个可以执行SQL语句的光标对象
  15. cursor = conn.cursor()
  16. rowIndex = 0 #记录行数
  17. class My_Window:
  18. def __init__(self, mainWindow):
  19. # 创建一个 widget 组件
  20. widget = QWidget()
  21. mainWindow.setCentralWidget(widget)
  22. # 创建一个垂直盒布局,并放在widget里面
  23. self.vboxlayout = QVBoxLayout(widget)
  24. # 函数调用 初始化 tableView
  25. self.tableView_init()
  26. # 调用 button 组件初始化函数
  27. self.button_init()
  28. # 展示主窗口
  29. mainWindow.show()
  30. def button_init(self):
  31. # 创建一个按钮
  32. self.refresh = QPushButton('刷新数据')
  33. # 将按钮添加到垂直盒布局里
  34. self.vboxlayout.addWidget(self.refresh)
  35. # 按钮组件连接槽函数
  36. self.refresh.clicked.connect(self.showStu)
  37. # 创建一个按钮
  38. self.button1 = QPushButton('删除学生信息')
  39. # 将按钮添加到垂直盒布局里
  40. self.vboxlayout.addWidget(self.button1)
  41. # 按钮组件连接槽函数
  42. self.button1.clicked.connect(self.delStu)
  43. # 创建一个按钮
  44. self.button3 = QPushButton('添加学生信息')
  45. # 将按钮添加到垂直盒布局里
  46. self.vboxlayout.addWidget(self.button3)
  47. # 按钮组件连接槽函数
  48. self.button3.clicked.connect(self.addStu)
  49. # 创建一个按钮
  50. self.modify = QPushButton('修改学生信息')
  51. # 将按钮添加到垂直盒布局里
  52. self.vboxlayout.addWidget(self.modify)
  53. # 按钮组件连接槽函数
  54. self.modify.clicked.connect(self.modStu)
  55. # 创建Label
  56. self.label = QLabel()
  57. self.label.setText("请输入要查询的学号:")
  58. # 创建EditText
  59. self.textEdit = QTextEdit()
  60. self.textEdit.setObjectName("textEdit")
  61. # 将按钮添加到垂直盒布局里
  62. self.vboxlayout.addWidget(self.label)
  63. self.vboxlayout.addWidget(self.textEdit)
  64. # 创建一个按钮
  65. self.search = QPushButton('查询学生信息')
  66. # 将按钮添加到垂直盒布局里
  67. self.vboxlayout.addWidget(self.search)
  68. # 按钮组件连接槽函数
  69. self.search.clicked.connect(self.seaStu)
  70. def tableView_init(self):
  71. # 设置数据层次结构,4行4列
  72. self.model = QStandardItemModel(0, 6)
  73. # 设置水平方向四个头标签文本内容
  74. self.model.setHorizontalHeaderLabels(['编号', '姓名', '性别', '年龄', '院系', '专业'])
  75. # 创建 tableView 组件
  76. self.tableview = QTableView()
  77. # 将 tableView 添加到垂直盒布局里
  78. self.vboxlayout.addWidget(self.tableview)
  79. # tableView 组件 设置模型
  80. self.tableview.setModel(self.model)
  81. self.tableview.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch) # 所有列自动拉伸,充满界面
  82. self.tableview.setSelectionMode(QAbstractItemView.SingleSelection) # 设置只能选中整行
  83. self.tableview.setEditTriggers(QTableView.NoEditTriggers) # 不可编辑
  84. self.tableview.setSelectionBehavior(QAbstractItemView.SelectRows) # 设置只能选中一行
  85. def tableView_clear(self):
  86. # 会全部清空,包括那个标准表头
  87. self.model.clear()
  88. # 所以重新设置标准表头 自己将一下代码注释 尝试
  89. self.model.setHorizontalHeaderLabels(['编号', '姓名', '性别', '年龄', '院系', '专业'])
  90. # 刷新现有数据
  91. def showStu(self):
  92. sql = str("select * from students")
  93. # 执行SQL语句
  94. cursor.execute(sql)
  95. results = cursor.fetchall() # 获取查询结果
  96. conn.commit()
  97. # 清除表内数据
  98. self.model.clear()
  99. # 所以重新设置标准表头 自己将一下代码注释 尝试
  100. self.model.setHorizontalHeaderLabels(['编号', '姓名', '性别', '年龄', '院系', '专业'])
  101. for row in results:
  102. self.model.appendRow([QStandardItem('%s' % row[0]),
  103. QStandardItem('%s' % row[1]),
  104. QStandardItem('%s' % row[2]),
  105. QStandardItem('%s' % row[3]),
  106. QStandardItem('%s' % row[4]),
  107. QStandardItem('%s' % row[5])
  108. ])
  109. # 从数据库删除当前已选中行 - 根据编号
  110. def delStu(self):
  111. index = self.model.itemData(self.model.index(self.tableview.currentIndex().row(), 0)) # 选中行的第一列数据,也即编号 返回的为字典类型
  112. sql = str("delete from students where id = %s" % index.get(0)) #获取字典的第一条数据
  113. # 执行SQL语句
  114. cursor.execute(sql)
  115. conn.commit()
  116. # 删除表格上的数据
  117. index = self.tableview.currentIndex() # 取得当前选中行的index
  118. self.model.removeRow(index.row()) # 通过index的row()操作得到行数进行删除
  119. self.show_message() # 打印提示信息
  120. print("删除成功!")
  121. def addStu(self):
  122. form = QDialog()
  123. ui2 = Ui_Form()
  124. ui2.setupUi(form)
  125. form.setWindowModality(Qt.NonModal) # 非模态,可与其他窗口交互
  126. # form.setWindowModality(Qt.WindowModal) # 窗口模态,当前未处理完,阻止与父窗口交互
  127. # form.setWindowModality(Qt.ApplicationModal) # 应用程序模态,阻止与任何其他窗口交互
  128. form.show()
  129. form.exec_()
  130. def modStu(self):
  131. form = QDialog()
  132. ui2 = UI_Modify()
  133. ui2.setupUi(form)
  134. form.setWindowModality(Qt.NonModal) # 非模态,可与其他窗口交互
  135. # form.setWindowModality(Qt.WindowModal) # 窗口模态,当前未处理完,阻止与父窗口交互
  136. # form.setWindowModality(Qt.ApplicationModal) # 应用程序模态,阻止与任何其他窗口交互
  137. form.show()
  138. form.exec_()
  139. def show_message(self):
  140. msg_box = QMessageBox(QMessageBox.Information, '提示', '删除成功!')
  141. msg_box.exec_()
  142. def seaStu(self):
  143. sql = str("select * from students where id = %s" % self.textEdit.toPlainText())
  144. # 执行SQL语句
  145. cursor.execute(sql)
  146. results = cursor.fetchall() # 获取查询结果
  147. conn.commit()
  148. # 清除表内数据
  149. self.model.clear()
  150. # 所以重新设置标准表头 自己将一下代码注释 尝试
  151. self.model.setHorizontalHeaderLabels(['编号', '姓名', '性别', '年龄', '院系', '专业'])
  152. for row in results:
  153. self.model.appendRow([QStandardItem('%s' % row[0]),
  154. QStandardItem('%s' % row[1]),
  155. QStandardItem('%s' % row[2]),
  156. QStandardItem('%s' % row[3]),
  157. QStandardItem('%s' % row[4]),
  158. QStandardItem('%s' % row[5])
  159. ])
  160. if __name__ == '__main__':
  161. app = QApplication(sys.argv)
  162. # 现在这创建 主窗口 (不然会闪退) 不然就继承
  163. mainWindow = QMainWindow()
  164. mainWindow.setWindowTitle('学生管理系统v2.3')
  165. mainWindow.resize(500, 600)
  166. # 一定要创建个实例出来 要有个 a 名字随便你取
  167. a = My_Window(mainWindow)
  168. sys.exit(app.exec_())

 ·添加学生信息

  1. # -*- coding: utf-8 -*-
  2. # Form implementation generated from reading ui file 'form.ui'
  3. #
  4. # Created by: PyQt5 UI code generator 5.15.4
  5. #
  6. # WARNING: Any manual changes made to this file will be lost when pyuic5 is
  7. # run again. Do not edit this file unless you know what you are doing.
  8. import sys
  9. from PyQt5 import QtCore, QtGui, QtWidgets
  10. # 数据库处理模块
  11. # 导入pymysql模块
  12. import pymysql
  13. # 连接database
  14. from PyQt5.QtWidgets import QMessageBox, QApplication
  15. conn = pymysql.connect(host="localhost", user="root",password="yan19991001",database="test")
  16. # 得到一个可以执行SQL语句的光标对象
  17. cursor = conn.cursor()
  18. class Ui_Form(object):
  19. def setupUi(self, Form):
  20. Form.setObjectName("Form")
  21. Form.resize(445, 371)
  22. self.textEdit = QtWidgets.QTextEdit(Form)
  23. self.textEdit.setGeometry(QtCore.QRect(180, 30, 131, 31))
  24. self.textEdit.setObjectName("textEdit")
  25. self.textEdit_4 = QtWidgets.QTextEdit(Form)
  26. self.textEdit_4.setGeometry(QtCore.QRect(180, 190, 131, 31))
  27. self.textEdit_4.setObjectName("textEdit_4")
  28. self.label_6 = QtWidgets.QLabel(Form)
  29. self.label_6.setGeometry(QtCore.QRect(130, 230, 41, 21))
  30. self.label_6.setObjectName("label_6")
  31. self.textEdit_2 = QtWidgets.QTextEdit(Form)
  32. self.textEdit_2.setGeometry(QtCore.QRect(180, 70, 131, 31))
  33. self.textEdit_2.setObjectName("textEdit_2")
  34. self.textEdit_5 = QtWidgets.QTextEdit(Form)
  35. self.textEdit_5.setGeometry(QtCore.QRect(180, 230, 131, 31))
  36. self.textEdit_5.setObjectName("textEdit_5")
  37. self.label_4 = QtWidgets.QLabel(Form)
  38. self.label_4.setGeometry(QtCore.QRect(130, 190, 41, 21))
  39. self.label_4.setObjectName("label_4")
  40. self.label_2 = QtWidgets.QLabel(Form)
  41. self.label_2.setGeometry(QtCore.QRect(130, 110, 41, 21))
  42. self.label_2.setObjectName("label_2")
  43. self.label_5 = QtWidgets.QLabel(Form)
  44. self.label_5.setGeometry(QtCore.QRect(130, 150, 41, 21))
  45. self.label_5.setObjectName("label_5")
  46. self.textEdit_3 = QtWidgets.QTextEdit(Form)
  47. self.textEdit_3.setGeometry(QtCore.QRect(180, 110, 131, 31)) #设置位置
  48. self.textEdit_3.setObjectName("textEdit_3")
  49. self.label = QtWidgets.QLabel(Form)
  50. self.label.setGeometry(QtCore.QRect(130, 30, 41, 21))
  51. self.label.setObjectName("label")
  52. self.label_3 = QtWidgets.QLabel(Form)
  53. self.label_3.setGeometry(QtCore.QRect(130, 70, 41, 21))
  54. self.label_3.setObjectName("label_3")
  55. self.textEdit_6 = QtWidgets.QTextEdit(Form)
  56. self.textEdit_6.setGeometry(QtCore.QRect(180, 150, 131, 31))
  57. self.textEdit_6.setObjectName("textEdit_6")
  58. self.pushButton = QtWidgets.QPushButton(Form)
  59. self.pushButton.setGeometry(QtCore.QRect(130, 300, 75, 23))
  60. self.pushButton.setObjectName("pushButton")
  61. # 按钮组件连接槽函数
  62. self.pushButton.clicked.connect(self.addStudent)
  63. self.pushButton_2 = QtWidgets.QPushButton(Form)
  64. self.pushButton_2.setGeometry(QtCore.QRect(230, 300, 75, 23))
  65. self.pushButton_2.setObjectName("pushButton_2")
  66. # 按钮组件连接槽函数
  67. self.pushButton_2.clicked.connect(self.refresh)
  68. self.retranslateUi(Form)
  69. QtCore.QMetaObject.connectSlotsByName(Form)
  70. def retranslateUi(self, Form):
  71. _translate = QtCore.QCoreApplication.translate
  72. Form.setWindowTitle(_translate("Form", "添加学生信息"))
  73. self.label_6.setText(_translate("Form", "专业"))
  74. self.label_4.setText(_translate("Form", "院系"))
  75. self.label_2.setText(_translate("Form", "性别"))
  76. self.label_5.setText(_translate("Form", "年龄"))
  77. self.label.setText(_translate("Form", "学号"))
  78. self.label_3.setText(_translate("Form", "姓名"))
  79. self.pushButton.setText(_translate("Form", "确定"))
  80. self.pushButton_2.setText(_translate("Form", "重置"))
  81. # textEdit textEdit_2 textEdit_3 textEdit_6 textEdit_4 textEdit_5
  82. # 由于生成的时候打乱了,以上为实际录入的数据顺序
  83. def addStudent(self):
  84. sql = str("insert into students values('%s','%s','%s','%s','%s','%s')" % (
  85. self.textEdit.toPlainText(), self.textEdit_2.toPlainText(), self.textEdit_3.toPlainText(), self.textEdit_6.toPlainText(), self.textEdit_4.toPlainText(), self.textEdit_5.toPlainText()))
  86. print(sql)
  87. # 执行SQL语句
  88. cursor.execute(sql)
  89. conn.commit()
  90. self.show_message() # 打印提示信息
  91. def show_message(self):
  92. msg_box = QMessageBox(QMessageBox.Information, '提示', '添加成功!')
  93. msg_box.exec_()
  94. def refresh(self):
  95. self.textEdit.setText("")
  96. self.textEdit_2.setText("")
  97. self.textEdit_3.setText("")
  98. self.textEdit_4.setText("")
  99. self.textEdit_5.setText("")
  100. self.textEdit_6.setText("")
  101. if __name__ == "__main__":
  102. import sys
  103. app = QtWidgets.QApplication(sys.argv)
  104. mainWindow = QtWidgets.QMainWindow()
  105. ui = Ui_Form()
  106. ui.setupUi(mainWindow)
  107. mainWindow.show()
  108. sys.exit(app.exec_())

·修改学生信息

  1. # -*- coding: utf-8 -*-
  2. # Form implementation generated from reading ui file 'form.ui'
  3. #
  4. # Created by: PyQt5 UI code generator 5.15.4
  5. #
  6. # WARNING: Any manual changes made to this file will be lost when pyuic5 is
  7. # run again. Do not edit this file unless you know what you are doing.
  8. import sys
  9. from PyQt5 import QtCore, QtGui, QtWidgets
  10. # 数据库处理模块
  11. # 导入pymysql模块
  12. import pymysql
  13. # 连接database
  14. from PyQt5.QtWidgets import QMessageBox, QApplication
  15. conn = pymysql.connect(host="localhost", user="root",password="yan19991001",database="test")
  16. # 得到一个可以执行SQL语句的光标对象
  17. cursor = conn.cursor()
  18. class UI_Modify(object):
  19. def setupUi(self, Form):
  20. Form.setObjectName("Form")
  21. Form.resize(445, 371)
  22. self.textEdit = QtWidgets.QTextEdit(Form)
  23. self.textEdit.setGeometry(QtCore.QRect(180, 30, 131, 31))
  24. self.textEdit.setObjectName("textEdit")
  25. self.textEdit_4 = QtWidgets.QTextEdit(Form)
  26. self.textEdit_4.setGeometry(QtCore.QRect(180, 190, 131, 31))
  27. self.textEdit_4.setObjectName("textEdit_4")
  28. self.label_6 = QtWidgets.QLabel(Form)
  29. self.label_6.setGeometry(QtCore.QRect(130, 230, 41, 21))
  30. self.label_6.setObjectName("label_6")
  31. self.textEdit_2 = QtWidgets.QTextEdit(Form)
  32. self.textEdit_2.setGeometry(QtCore.QRect(180, 70, 131, 31))
  33. self.textEdit_2.setObjectName("textEdit_2")
  34. self.textEdit_5 = QtWidgets.QTextEdit(Form)
  35. self.textEdit_5.setGeometry(QtCore.QRect(180, 230, 131, 31))
  36. self.textEdit_5.setObjectName("textEdit_5")
  37. self.label_4 = QtWidgets.QLabel(Form)
  38. self.label_4.setGeometry(QtCore.QRect(130, 190, 41, 21))
  39. self.label_4.setObjectName("label_4")
  40. self.label_2 = QtWidgets.QLabel(Form)
  41. self.label_2.setGeometry(QtCore.QRect(130, 110, 41, 21))
  42. self.label_2.setObjectName("label_2")
  43. self.label_5 = QtWidgets.QLabel(Form)
  44. self.label_5.setGeometry(QtCore.QRect(130, 150, 41, 21))
  45. self.label_5.setObjectName("label_5")
  46. self.textEdit_3 = QtWidgets.QTextEdit(Form)
  47. self.textEdit_3.setGeometry(QtCore.QRect(180, 110, 131, 31)) #设置位置
  48. self.textEdit_3.setObjectName("textEdit_3")
  49. self.label = QtWidgets.QLabel(Form)
  50. self.label.setGeometry(QtCore.QRect(130, 30, 41, 21))
  51. self.label.setObjectName("label")
  52. self.label_3 = QtWidgets.QLabel(Form)
  53. self.label_3.setGeometry(QtCore.QRect(130, 70, 41, 21))
  54. self.label_3.setObjectName("label_3")
  55. self.textEdit_6 = QtWidgets.QTextEdit(Form)
  56. self.textEdit_6.setGeometry(QtCore.QRect(180, 150, 131, 31))
  57. self.textEdit_6.setObjectName("textEdit_6")
  58. self.pushButton = QtWidgets.QPushButton(Form)
  59. self.pushButton.setGeometry(QtCore.QRect(130, 300, 75, 23))
  60. self.pushButton.setObjectName("pushButton")
  61. # 按钮组件连接槽函数
  62. self.pushButton.clicked.connect(self.modStudent)
  63. self.pushButton_2 = QtWidgets.QPushButton(Form)
  64. self.pushButton_2.setGeometry(QtCore.QRect(230, 300, 75, 23))
  65. self.pushButton_2.setObjectName("pushButton_2")
  66. # 按钮组件连接槽函数
  67. self.pushButton_2.clicked.connect(self.refresh)
  68. self.retranslateUi(Form)
  69. QtCore.QMetaObject.connectSlotsByName(Form)
  70. def retranslateUi(self, Form):
  71. _translate = QtCore.QCoreApplication.translate
  72. Form.setWindowTitle(_translate("Form", "修改学生信息"))
  73. self.label_6.setText(_translate("Form", "专业"))
  74. self.label_4.setText(_translate("Form", "院系"))
  75. self.label_2.setText(_translate("Form", "性别"))
  76. self.label_5.setText(_translate("Form", "年龄"))
  77. self.label.setText(_translate("Form", "学号"))
  78. self.label_3.setText(_translate("Form", "姓名"))
  79. self.pushButton.setText(_translate("Form", "确定"))
  80. self.pushButton_2.setText(_translate("Form", "重置"))
  81. # textEdit textEdit_2 textEdit_3 textEdit_6 textEdit_4 textEdit_5
  82. # 由于生成的时候打乱了,以上为实际录入的数据顺序
  83. def modStudent(self):
  84. sql = str("update students set name = %s,sex = %s,age = %s,depart = %s,major = %s where id = %s" % (
  85. self.textEdit_2.toPlainText(), self.textEdit_3.toPlainText(),self.textEdit_6.toPlainText(), self.textEdit_4.toPlainText(), self.textEdit_5.toPlainText() , self.textEdit.toPlainText()))
  86. print(sql)
  87. # 执行SQL语句
  88. cursor.execute(sql)
  89. conn.commit()
  90. self.show_message() # 打印提示信息
  91. def show_message(self):
  92. msg_box = QMessageBox(QMessageBox.Information, '提示', '修改成功!')
  93. msg_box.exec_()
  94. def refresh(self):
  95. self.textEdit.setText("")
  96. self.textEdit_2.setText("")
  97. self.textEdit_3.setText("")
  98. self.textEdit_4.setText("")
  99. self.textEdit_5.setText("")
  100. self.textEdit_6.setText("")
  101. if __name__ == "__main__":
  102. import sys
  103. app = QtWidgets.QApplication(sys.argv)
  104. mainWindow = QtWidgets.QMainWindow()
  105. ui = UI_Modify()
  106. ui.setupUi(mainWindow)
  107. mainWindow.show()
  108. sys.exit(app.exec_())

总结

       PyQT5 实质上是QT对Python的专用版,相似于C++的MFC编程,它可以使用QT Designer等UI设计工具设计界面,大大简化手动书写代码的繁琐度,提高开发效率。

        开发该系统大概耗费6小时左右,学习难度适中。由于没有系统学习,可能对PyQT的了解不深,代码质量欠佳,望多多指正,谢谢大家。


        如果有小伙伴需要该项目,以上三部分代码创建三个py文件分别存放运行即可。如果有其他想法,欢迎评论或者联系我哦。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/307810
推荐阅读
相关标签
  

闽ICP备14008679号