用pycharm制作一个学生管理系统,需要gui界面并能连接mySQL,全部代码
时间: 2024-03-20 19:44:23 浏览: 73
基于Python+MySQL 实现(GUI )学生信息管理系统【100010435】
5星 · 资源好评率100%
下面是一个使用PyQt5和MySQL实现的简单学生管理系统的完整代码:
```python
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton, QTableWidget, QTableWidgetItem
from PyQt5.QtSql import QSqlDatabase, QSqlQuery
from PyQt5.QtGui import QFont
class StudentManagementSystem(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('学生管理系统')
self.resize(800, 600)
self.font = QFont('Arial', 16)
self.initUI()
self.initDB()
def initUI(self):
# 初始化界面元素
self.id_label = QLabel('学号')
self.id_label.setFont(self.font)
self.id_edit = QLineEdit()
self.id_edit.setFont(self.font)
self.name_label = QLabel('姓名')
self.name_label.setFont(self.font)
self.name_edit = QLineEdit()
self.name_edit.setFont(self.font)
self.gender_label = QLabel('性别')
self.gender_label.setFont(self.font)
self.gender_edit = QLineEdit()
self.gender_edit.setFont(self.font)
self.class_label = QLabel('班级')
self.class_label.setFont(self.font)
self.class_edit = QLineEdit()
self.class_edit.setFont(self.font)
self.add_button = QPushButton('添加')
self.add_button.setFont(self.font)
self.add_button.clicked.connect(self.addStudent)
self.update_button = QPushButton('更新')
self.update_button.setFont(self.font)
self.update_button.clicked.connect(self.updateStudent)
self.delete_button = QPushButton('删除')
self.delete_button.setFont(self.font)
self.delete_button.clicked.connect(self.deleteStudent)
self.search_button = QPushButton('搜索')
self.search_button.setFont(self.font)
self.search_button.clicked.connect(self.searchStudent)
self.table = QTableWidget()
self.table.setColumnCount(4)
self.table.setHorizontalHeaderLabels(['学号', '姓名', '性别', '班级'])
self.table.setFont(self.font)
# 设置布局
id_layout = QHBoxLayout()
id_layout.addWidget(self.id_label)
id_layout.addWidget(self.id_edit)
name_layout = QHBoxLayout()
name_layout.addWidget(self.name_label)
name_layout.addWidget(self.name_edit)
gender_layout = QHBoxLayout()
gender_layout.addWidget(self.gender_label)
gender_layout.addWidget(self.gender_edit)
class_layout = QHBoxLayout()
class_layout.addWidget(self.class_label)
class_layout.addWidget(self.class_edit)
button_layout = QHBoxLayout()
button_layout.addWidget(self.add_button)
button_layout.addWidget(self.update_button)
button_layout.addWidget(self.delete_button)
button_layout.addWidget(self.search_button)
input_layout = QVBoxLayout()
input_layout.addLayout(id_layout)
input_layout.addLayout(name_layout)
input_layout.addLayout(gender_layout)
input_layout.addLayout(class_layout)
input_layout.addLayout(button_layout)
main_layout = QVBoxLayout()
main_layout.addLayout(input_layout)
main_layout.addWidget(self.table)
self.setLayout(main_layout)
def initDB(self):
# 初始化数据库连接
self.db = QSqlDatabase.addDatabase('QMYSQL')
self.db.setHostName('localhost')
self.db.setDatabaseName('student')
self.db.setUserName('root')
self.db.setPassword('password')
if not self.db.open():
print('无法连接数据库')
sys.exit(-1)
def addStudent(self):
# 添加学生信息
query = QSqlQuery()
query.prepare('INSERT INTO student (id, name, gender, class) VALUES (?, ?, ?, ?)')
query.addBindValue(self.id_edit.text())
query.addBindValue(self.name_edit.text())
query.addBindValue(self.gender_edit.text())
query.addBindValue(self.class_edit.text())
if query.exec():
self.clearInput()
self.loadStudents()
else:
print('添加学生信息失败')
def updateStudent(self):
# 更新学生信息
query = QSqlQuery()
query.prepare('UPDATE student SET name=?, gender=?, class=? WHERE id=?')
query.addBindValue(self.name_edit.text())
query.addBindValue(self.gender_edit.text())
query.addBindValue(self.class_edit.text())
query.addBindValue(self.id_edit.text())
if query.exec():
self.clearInput()
self.loadStudents()
else:
print('更新学生信息失败')
def deleteStudent(self):
# 删除学生信息
query = QSqlQuery()
query.prepare('DELETE FROM student WHERE id=?')
query.addBindValue(self.id_edit.text())
if query.exec():
self.clearInput()
self.loadStudents()
else:
print('删除学生信息失败')
def searchStudent(self):
# 搜索学生信息
query = QSqlQuery()
query.prepare('SELECT * FROM student WHERE id=?')
query.addBindValue(self.id_edit.text())
if query.exec():
if query.next():
self.name_edit.setText(query.value(1))
self.gender_edit.setText(query.value(2))
self.class_edit.setText(query.value(3))
else:
self.clearInput()
else:
print('搜索学生信息失败')
def loadStudents(self):
# 加载所有学生信息到表格中
query = QSqlQuery()
query.exec('SELECT * FROM student')
self.table.setRowCount(0)
while query.next():
row = self.table.rowCount()
self.table.insertRow(row)
self.table.setItem(row, 0, QTableWidgetItem(str(query.value(0))))
self.table.setItem(row, 1, QTableWidgetItem(query.value(1)))
self.table.setItem(row, 2, QTableWidgetItem(query.value(2)))
self.table.setItem(row, 3, QTableWidgetItem(query.value(3)))
def clearInput(self):
# 清空输入框
self.id_edit.clear()
self.name_edit.clear()
self.gender_edit.clear()
self.class_edit.clear()
if __name__ == '__main__':
app = QApplication(sys.argv)
system = StudentManagementSystem()
system.show()
system.loadStudents()
sys.exit(app.exec_())
```
这段代码实现了一个简单的学生管理系统,包括添加、删除、更新、搜索学生信息以及将所有学生信息加载到表格中的功能。在代码中还使用了PyQt5和MySQL进行界面设计和数据库操作。在使用之前,需要安装相应的库,例如:
```
pip install PyQt5 PyQt5-sql mysql-connector-python
```
同时,还需要创建一个名为`student`的MySQL数据库,并创建一个名为`student`的表,包含`id`、`name`、`gender`和`class`四个字段。在实际运行时,需要将代码中的数据库连接信息修改为实际的地址、用户名和密码。
阅读全文