要求使用Python的图形化工具+数据库系统完成学生信息管理系统。数据库建议使用Mysql数据库系统。 2、实现满足学生信息管理系统数据库表的设计。 3、实现学生信息的添加、修改、删除和查询功能,可以参考上学期学生管理系统的所要求的功能。
时间: 2023-07-16 16:16:22 浏览: 116
好的,这个任务我可以帮您完成。首先,我们需要安装Python的图形化工具,我推荐使用Tkinter库,它是Python自带的GUI库,使用简单方便。然后我们需要安装MySQL数据库系统,可以到官网下载安装。
接下来,我们需要设计学生信息管理系统的数据库表。假设我们需要存储学生的姓名、学号、性别、年龄、班级等信息,我们可以设计如下的表结构:
```
CREATE TABLE `students` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`student_id` varchar(50) NOT NULL,
`gender` varchar(10) NOT NULL,
`age` int(11) NOT NULL,
`class` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
```
接下来,我们可以编写Python程序,使用Tkinter库实现学生信息管理系统的添加、修改、删除和查询功能。示例代码如下:
```python
import tkinter as tk
import mysql.connector
# 连接MySQL数据库
db = mysql.connector.connect(
host="localhost",
user="root",
password="password",
database="students"
)
# 创建游标对象
cursor = db.cursor()
# 添加学生信息
def add_student():
name = name_entry.get()
student_id = student_id_entry.get()
gender = gender_entry.get()
age = age_entry.get()
class_name = class_entry.get()
sql = "INSERT INTO students (name, student_id, gender, age, class) VALUES (%s, %s, %s, %s, %s)"
val = (name, student_id, gender, age, class_name)
cursor.execute(sql, val)
db.commit()
clear_entries()
show_message("添加学生信息成功!")
# 修改学生信息
def update_student():
id = id_entry.get()
name = name_entry.get()
student_id = student_id_entry.get()
gender = gender_entry.get()
age = age_entry.get()
class_name = class_entry.get()
sql = "UPDATE students SET name=%s, student_id=%s, gender=%s, age=%s, class=%s WHERE id=%s"
val = (name, student_id, gender, age, class_name, id)
cursor.execute(sql, val)
db.commit()
clear_entries()
show_message("修改学生信息成功!")
# 删除学生信息
def delete_student():
id = id_entry.get()
sql = "DELETE FROM students WHERE id=%s"
val = (id,)
cursor.execute(sql, val)
db.commit()
clear_entries()
show_message("删除学生信息成功!")
# 查询学生信息
def search_student():
id = id_entry.get()
sql = "SELECT * FROM students WHERE id=%s"
val = (id,)
cursor.execute(sql, val)
result = cursor.fetchone()
if result:
name_entry.delete(0, tk.END)
name_entry.insert(0, result[1])
student_id_entry.delete(0, tk.END)
student_id_entry.insert(0, result[2])
gender_entry.delete(0, tk.END)
gender_entry.insert(0, result[3])
age_entry.delete(0, tk.END)
age_entry.insert(0, result[4])
class_entry.delete(0, tk.END)
class_entry.insert(0, result[5])
else:
show_message("未找到该学生信息!")
# 清空输入框
def clear_entries():
id_entry.delete(0, tk.END)
name_entry.delete(0, tk.END)
student_id_entry.delete(0, tk.END)
gender_entry.delete(0, tk.END)
age_entry.delete(0, tk.END)
class_entry.delete(0, tk.END)
# 显示消息
def show_message(message):
message_label.config(text=message)
# 创建窗口
window = tk.Tk()
window.title("学生信息管理系统")
# 创建标签和输入框
id_label = tk.Label(window, text="学生编号:")
id_label.grid(row=0, column=0)
id_entry = tk.Entry(window)
id_entry.grid(row=0, column=1)
name_label = tk.Label(window, text="学生姓名:")
name_label.grid(row=1, column=0)
name_entry = tk.Entry(window)
name_entry.grid(row=1, column=1)
student_id_label = tk.Label(window, text="学生学号:")
student_id_label.grid(row=2, column=0)
student_id_entry = tk.Entry(window)
student_id_entry.grid(row=2, column=1)
gender_label = tk.Label(window, text="学生性别:")
gender_label.grid(row=3, column=0)
gender_entry = tk.Entry(window)
gender_entry.grid(row=3, column=1)
age_label = tk.Label(window, text="学生年龄:")
age_label.grid(row=4, column=0)
age_entry = tk.Entry(window)
age_entry.grid(row=4, column=1)
class_label = tk.Label(window, text="学生班级:")
class_label.grid(row=5, column=0)
class_entry = tk.Entry(window)
class_entry.grid(row=5, column=1)
# 创建按钮
add_button = tk.Button(window, text="添加", command=add_student)
add_button.grid(row=6, column=0)
update_button = tk.Button(window, text="修改", command=update_student)
update_button.grid(row=6, column=1)
delete_button = tk.Button(window, text="删除", command=delete_student)
delete_button.grid(row=6, column=2)
search_button = tk.Button(window, text="查询", command=search_student)
search_button.grid(row=6, column=3)
clear_button = tk.Button(window, text="清空", command=clear_entries)
clear_button.grid(row=6, column=4)
# 创建消息标签
message_label = tk.Label(window, bg="yellow", fg="red", font=("Arial", 12), width=50, height=2)
message_label.grid(row=7, column=0, columnspan=5)
window.mainloop()
# 关闭数据库连接
db.close()
```
以上代码实现了学生信息管理系统的添加、修改、删除和查询功能,并且使用了MySQL数据库存储学生信息。您可以根据自己的需求修改代码,实现更多功能。
阅读全文