写一个用 python+tkinter+达梦数据库 的学生管理系统
时间: 2023-08-04 09:12:48 浏览: 196
由于达梦数据库与Python的连接库较少,本文以MySQL数据库为例,演示如何使用Python和Tkinter构建一个简单的学生管理系统。
首先,我们需要安装必要的库:
```python
pip install pymysql
```
然后,我们需要创建一个名为“student_management”的数据库,包含以下两个表格:
- 学生信息表:
| 字段名 | 数据类型 |
| ------ | -------- |
| ID | int |
| name | varchar |
| age | int |
| gender | varchar |
| grade | varchar |
- 学生成绩表:
| 字段名 | 数据类型 |
| ------ | -------- |
| ID | int |
| course | varchar |
| score | int |
接下来,我们来实现学生管理系统的主要功能:增删改查。
```python
import tkinter as tk
import pymysql
# 创建数据库连接
db = pymysql.connect(host='localhost', user='root', password='password', database='student_management', charset='utf8')
cursor = db.cursor()
# 添加学生信息
def add_student():
# 获取输入的学生信息
id = int(id_entry.get())
name = name_entry.get()
age = int(age_entry.get())
gender = gender_entry.get()
grade = grade_entry.get()
# 在学生信息表中插入数据
sql = "INSERT INTO student_info(ID, name, age, gender, grade) VALUES (%s, %s, %s, %s, %s)"
val = (id, name, age, gender, grade)
cursor.execute(sql, val)
db.commit()
# 在学生成绩表中插入数据
for i in range(5):
course = course_list[i]
score = int(score_entries[i].get())
sql = "INSERT INTO student_score(ID, course, score) VALUES (%s, %s, %s)"
val = (id, course, score)
cursor.execute(sql, val)
db.commit()
# 清空输入框
id_entry.delete(0, tk.END)
name_entry.delete(0, tk.END)
age_entry.delete(0, tk.END)
gender_entry.delete(0, tk.END)
grade_entry.delete(0, tk.END)
for entry in score_entries:
entry.delete(0, tk.END)
# 查询学生信息
def search_student():
# 获取输入的学生ID
id = int(id_entry.get())
# 在学生信息表中查询数据
sql = "SELECT * FROM student_info WHERE ID = %s"
val = (id,)
cursor.execute(sql, val)
result = cursor.fetchone()
if result:
name_entry.delete(0, tk.END)
age_entry.delete(0, tk.END)
gender_entry.delete(0, tk.END)
grade_entry.delete(0, tk.END)
name_entry.insert(0, result[1])
age_entry.insert(0, result[2])
gender_entry.insert(0, result[3])
grade_entry.insert(0, result[4])
# 在学生成绩表中查询数据
for i in range(5):
course = course_list[i]
sql = "SELECT score FROM student_score WHERE ID = %s AND course = %s"
val = (id, course)
cursor.execute(sql, val)
result = cursor.fetchone()
if result:
score_entries[i].delete(0, tk.END)
score_entries[i].insert(0, result[0])
# 删除学生信息
def delete_student():
# 获取输入的学生ID
id = int(id_entry.get())
# 在学生信息表中删除数据
sql = "DELETE FROM student_info WHERE ID = %s"
val = (id,)
cursor.execute(sql, val)
db.commit()
# 在学生成绩表中删除数据
sql = "DELETE FROM student_score WHERE ID = %s"
val = (id,)
cursor.execute(sql, val)
db.commit()
# 清空输入框
id_entry.delete(0, tk.END)
name_entry.delete(0, tk.END)
age_entry.delete(0, tk.END)
gender_entry.delete(0, tk.END)
grade_entry.delete(0, tk.END)
for entry in score_entries:
entry.delete(0, tk.END)
# 修改学生信息
def update_student():
# 获取输入的学生信息
id = int(id_entry.get())
name = name_entry.get()
age = int(age_entry.get())
gender = gender_entry.get()
grade = grade_entry.get()
# 在学生信息表中更新数据
sql = "UPDATE student_info SET name = %s, age = %s, gender = %s, grade = %s WHERE ID = %s"
val = (name, age, gender, grade, id)
cursor.execute(sql, val)
db.commit()
# 在学生成绩表中更新数据
for i in range(5):
course = course_list[i]
score = int(score_entries[i].get())
sql = "UPDATE student_score SET score = %s WHERE ID = %s AND course = %s"
val = (score, id, course)
cursor.execute(sql, val)
db.commit()
# 清空输入框
id_entry.delete(0, tk.END)
name_entry.delete(0, tk.END)
age_entry.delete(0, tk.END)
gender_entry.delete(0, tk.END)
grade_entry.delete(0, tk.END)
for entry in score_entries:
entry.delete(0, tk.END)
# 创建窗口
window = tk.Tk()
window.title("学生管理系统")
# 创建标签和输入框
id_label = tk.Label(window, text="学生ID:")
id_label.grid(row=0, column=0, padx=10, pady=5)
id_entry = tk.Entry(window)
id_entry.grid(row=0, column=1, padx=10, pady=5)
name_label = tk.Label(window, text="姓名:")
name_label.grid(row=1, column=0, padx=10, pady=5)
name_entry = tk.Entry(window)
name_entry.grid(row=1, column=1, padx=10, pady=5)
age_label = tk.Label(window, text="年龄:")
age_label.grid(row=2, column=0, padx=10, pady=5)
age_entry = tk.Entry(window)
age_entry.grid(row=2, column=1, padx=10, pady=5)
gender_label = tk.Label(window, text="性别:")
gender_label.grid(row=3, column=0, padx=10, pady=5)
gender_entry = tk.Entry(window)
gender_entry.grid(row=3, column=1, padx=10, pady=5)
grade_label = tk.Label(window, text="年级:")
grade_label.grid(row=4, column=0, padx=10, pady=5)
grade_entry = tk.Entry(window)
grade_entry.grid(row=4, column=1, padx=10, pady=5)
score_label = tk.Label(window, text="成绩:")
score_label.grid(row=5, column=0, padx=10, pady=5)
course_list = ["语文", "数学", "英语", "物理", "化学"]
score_entries = []
for i in range(5):
course_label = tk.Label(window, text=course_list[i])
course_label.grid(row=5 + i, column=0, padx=10, pady=5)
score_entry = tk.Entry(window)
score_entry.grid(row=5 + i, column=1, padx=10, pady=5)
score_entries.append(score_entry)
# 创建按钮
add_button = tk.Button(window, text="添加", command=add_student)
add_button.grid(row=0, column=2, padx=10, pady=5)
search_button = tk.Button(window, text="查询", command=search_student)
search_button.grid(row=1, column=2, padx=10, pady=5)
delete_button = tk.Button(window, text="删除", command=delete_student)
delete_button.grid(row=2, column=2, padx=10, pady=5)
update_button = tk.Button(window, text="修改", command=update_student)
update_button.grid(row=3, column=2, padx=10, pady=5)
# 显示窗口
window.mainloop()
# 关闭数据库连接
db.close()
```
这样,我们就完成了一个简单的学生管理系统。当然,这只是一个基础的版本,在实际应用中还需要进行更多的功能扩展和优化。
阅读全文