帮我用python编写一个简单的学生信息管理系统,该系统包括4张表:专业表、学生表、课程表和成绩表,要求使用tkinter/wxPython进行GUI编程,使用SQlite数据库进行信息存储,可实现对专业信息、学生信息、课程信息和成绩的综合管理。
时间: 2023-12-10 16:42:24 浏览: 72
好的,以下是一个简单的学生信息管理系统的Python代码:
```python
import sqlite3
import tkinter as tk
from tkinter import ttk
# 连接数据库
conn = sqlite3.connect('student.db')
c = conn.cursor()
# 创建专业表
c.execute('''CREATE TABLE IF NOT EXISTS major
(id INTEGER PRIMARY KEY,
name TEXT NOT NULL)''')
# 创建学生表
c.execute('''CREATE TABLE IF NOT EXISTS student
(id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
gender TEXT NOT NULL,
major_id INTEGER NOT NULL,
FOREIGN KEY (major_id) REFERENCES major(id))''')
# 创建课程表
c.execute('''CREATE TABLE IF NOT EXISTS course
(id INTEGER PRIMARY KEY,
name TEXT NOT NULL)''')
# 创建成绩表
c.execute('''CREATE TABLE IF NOT EXISTS score
(id INTEGER PRIMARY KEY,
student_id INTEGER NOT NULL,
course_id INTEGER NOT NULL,
grade INTEGER NOT NULL,
FOREIGN KEY (student_id) REFERENCES student(id),
FOREIGN KEY (course_id) REFERENCES course(id))''')
# 插入测试数据
c.execute("INSERT INTO major (id, name) VALUES (1, '计算机科学')")
c.execute("INSERT INTO major (id, name) VALUES (2, '数学')")
c.execute("INSERT INTO student (id, name, gender, major_id) VALUES (1, '张三', '男', 1)")
c.execute("INSERT INTO student (id, name, gender, major_id) VALUES (2, '李四', '女', 2)")
c.execute("INSERT INTO course (id, name) VALUES (1, 'Python')")
c.execute("INSERT INTO course (id, name) VALUES (2, '数据结构')")
c.execute("INSERT INTO score (id, student_id, course_id, grade) VALUES (1, 1, 1, 90)")
c.execute("INSERT INTO score (id, student_id, course_id, grade) VALUES (2, 1, 2, 85)")
c.execute("INSERT INTO score (id, student_id, course_id, grade) VALUES (3, 2, 1, 95)")
c.execute("INSERT INTO score (id, student_id, course_id, grade) VALUES (4, 2, 2, 80)")
conn.commit()
# 创建主窗口
root = tk.Tk()
root.title('学生信息管理系统')
# 创建专业管理模块
major_frame = ttk.Frame(root)
major_table = ttk.Treeview(major_frame, columns=('id', 'name'), show='headings')
major_table.heading('id', text='ID')
major_table.heading('name', text='名称')
major_table.column('id', width=100)
major_table.column('name', width=100)
major_table.pack(side='left', fill='both', expand=True)
major_scrollbar = ttk.Scrollbar(major_frame, orient='vertical', command=major_table.yview)
major_scrollbar.pack(side='right', fill='y')
major_table.configure(yscrollcommand=major_scrollbar.set)
def add_major():
id = major_id_entry.get()
name = major_name_entry.get()
if id and name:
c.execute("INSERT INTO major (id, name) VALUES (?, ?)", (id, name))
conn.commit()
major_table.insert('', 'end', values=(id, name))
def delete_major():
item = major_table.selection()[0]
id = major_table.item(item)['values'][0]
c.execute("DELETE FROM major WHERE id=?", (id,))
conn.commit()
major_table.delete(item)
major_id_label = ttk.Label(major_frame, text='ID')
major_id_label.pack()
major_id_entry = ttk.Entry(major_frame)
major_id_entry.pack()
major_name_label = ttk.Label(major_frame, text='名称')
major_name_label.pack()
major_name_entry = ttk.Entry(major_frame)
major_name_entry.pack()
add_major_button = ttk.Button(major_frame, text='添加', command=add_major)
add_major_button.pack()
delete_major_button = ttk.Button(major_frame, text='删除', command=delete_major)
delete_major_button.pack()
for row in c.execute("SELECT * FROM major"):
major_table.insert('', 'end', values=row)
major_frame.pack(side='left', fill='both', expand=True)
# 创建学生管理模块
student_frame = ttk.Frame(root)
student_table = ttk.Treeview(student_frame, columns=('id', 'name', 'gender', 'major_id'), show='headings')
student_table.heading('id', text='ID')
student_table.heading('name', text='姓名')
student_table.heading('gender', text='性别')
student_table.heading('major_id', text='专业')
student_table.column('id', width=100)
student_table.column('name', width=100)
student_table.column('gender', width=100)
student_table.column('major_id', width=100)
student_table.pack(side='left', fill='both', expand=True)
student_scrollbar = ttk.Scrollbar(student_frame, orient='vertical', command=student_table.yview)
student_scrollbar.pack(side='right', fill='y')
student_table.configure(yscrollcommand=student_scrollbar.set)
def add_student():
id = student_id_entry.get()
name = student_name_entry.get()
gender = student_gender_entry.get()
major_id = student_major_id_entry.get()
if id and name and gender and major_id:
c.execute("INSERT INTO student (id, name, gender, major_id) VALUES (?, ?, ?, ?)", (id, name, gender, major_id))
conn.commit()
major_name = c.execute("SELECT name FROM major WHERE id=?", (major_id,)).fetchone()[0]
student_table.insert('', 'end', values=(id, name, gender, major_name))
def delete_student():
item = student_table.selection()[0]
id = student_table.item(item)['values'][0]
c.execute("DELETE FROM student WHERE id=?", (id,))
conn.commit()
student_table.delete(item)
student_id_label = ttk.Label(student_frame, text='ID')
student_id_label.pack()
student_id_entry = ttk.Entry(student_frame)
student_id_entry.pack()
student_name_label = ttk.Label(student_frame, text='姓名')
student_name_label.pack()
student_name_entry = ttk.Entry(student_frame)
student_name_entry.pack()
student_gender_label = ttk.Label(student_frame, text='性别')
student_gender_label.pack()
student_gender_entry = ttk.Entry(student_frame)
student_gender_entry.pack()
student_major_id_label = ttk.Label(student_frame, text='专业ID')
student_major_id_label.pack()
student_major_id_entry = ttk.Entry(student_frame)
student_major_id_entry.pack()
add_student_button = ttk.Button(student_frame, text='添加', command=add_student)
add_student_button.pack()
delete_student_button = ttk.Button(student_frame, text='删除', command=delete_student)
delete_student_button.pack()
for row in c.execute("SELECT * FROM student"):
major_name = c.execute("SELECT name FROM major WHERE id=?", (row[3],)).fetchone()[0]
student_table.insert('', 'end', values=(row[0], row[1], row[2], major_name))
student_frame.pack(side='left', fill='both', expand=True)
# 创建课程管理模块
course_frame = ttk.Frame(root)
course_table = ttk.Treeview(course_frame, columns=('id', 'name'), show='headings')
course_table.heading('id', text='ID')
course_table.heading('name', text='名称')
course_table.column('id', width=100)
course_table.column('name', width=100)
course_table.pack(side='left', fill='both', expand=True)
course_scrollbar = ttk.Scrollbar(course_frame, orient='vertical', command=course_table.yview)
course_scrollbar.pack(side='right', fill='y')
course_table.configure(yscrollcommand=course_scrollbar.set)
def add_course():
id = course_id_entry.get()
name = course_name_entry.get()
if id and name:
c.execute("INSERT INTO course (id, name) VALUES (?, ?)", (id, name))
conn.commit()
course_table.insert('', 'end', values=(id, name))
def delete_course():
item = course_table.selection()[0]
id = course_table.item(item)['values'][0]
c.execute("DELETE FROM course WHERE id=?", (id,))
conn.commit()
course_table.delete(item)
course_id_label = ttk.Label(course_frame, text='ID')
course_id_label.pack()
course_id_entry = ttk.Entry(course_frame)
course_id_entry.pack()
course_name_label = ttk.Label(course_frame, text='名称')
course_name_label.pack()
course_name_entry = ttk.Entry(course_frame)
course_name_entry.pack()
add_course_button = ttk.Button(course_frame, text='添加', command=add_course)
add_course_button.pack()
delete_course_button = ttk.Button(course_frame, text='删除', command=delete_course)
delete_course_button.pack()
for row in c.execute("SELECT * FROM course"):
course_table.insert('', 'end', values=row)
course_frame.pack(side='left', fill='both', expand=True)
# 创建成绩管理模块
score_frame = ttk.Frame(root)
score_table = ttk.Treeview(score_frame, columns=('id', 'student_id', 'course_id', 'grade'), show='headings')
score_table.heading('id', text='ID')
score_table.heading('student_id', text='学生ID')
score_table.heading('course_id', text='课程ID')
score_table.heading('grade', text='成绩')
score_table.column('id', width=100)
score_table.column('student_id', width=100)
score_table.column('course_id', width=100)
score_table.column('grade', width=100)
score_table.pack(side='left', fill='both', expand=True)
score_scrollbar = ttk.Scrollbar(score_frame, orient='vertical', command=score_table.yview)
score_scrollbar.pack(side='right', fill='y')
score_table.configure(yscrollcommand=score_scrollbar.set)
def add_score():
student_id = score_student_id_entry.get()
course_id = score_course_id_entry.get()
grade = score_grade_entry.get()
if student_id and course_id and grade:
c.execute("INSERT INTO score (student_id, course_id, grade) VALUES (?, ?, ?)", (student_id, course_id, grade))
conn.commit()
student_name = c.execute("SELECT name FROM student WHERE id=?", (student_id,)).fetchone()[0]
course_name = c.execute("SELECT name FROM course WHERE id=?", (course_id,)).fetchone()[0]
score_table.insert('', 'end', values=(c.lastrowid, student_id, course_id, grade))
def delete_score():
item = score_table.selection()[0]
id = score_table.item(item)['values'][0]
c.execute("DELETE FROM score WHERE id=?", (id,))
conn.commit()
score_table.delete(item)
score_student_id_label = ttk.Label(score_frame, text='学生ID')
score_student_id_label.pack()
score_student_id_entry = ttk.Entry(score_frame)
score_student_id_entry.pack()
score_course_id_label = ttk.Label(score_frame, text='课程ID')
score_course_id_label.pack()
score_course_id_entry = ttk.Entry(score_frame)
score_course_id_entry.pack()
score_grade_label = ttk.Label(score_frame, text='成绩')
score_grade_label.pack()
score_grade_entry = ttk.Entry(score_frame)
score_grade_entry.pack()
add_score_button = ttk.Button(score_frame, text='添加', command=add_score)
add_score_button.pack()
delete_score_button = ttk.Button(score_frame, text='删除', command=delete_score)
delete_score_button.pack()
for row in c.execute("SELECT * FROM score"):
student_name = c.execute("SELECT name FROM student WHERE id=?", (row[1],)).fetchone()[0]
course_name = c.execute("SELECT name FROM course WHERE id=?", (row[2],)).fetchone()[0]
score_table.insert('', 'end', values=(row[0], row[1], row[2], row[3]))
score_frame.pack(side='left', fill='both', expand=True)
root.mainloop()
```
此代码使用了Tkinter库构建了一个简单的GUI界面,使用SQLite数据库进行信息存储,并实现对专业信息、学生信息、课程信息和成绩的综合管理。
阅读全文