用Python中的sqlite3库和tkinter库编写一个学生信息管理系统,包括姓名,性别,学号,手机号,专业,班级
时间: 2024-06-10 12:06:12 浏览: 121
好的,我能为您提供帮助,以下是实现学生信息管理系统的Python代码:
```
import sqlite3
import tkinter as tk
# 创建一个数据库连接
conn = sqlite3.connect('students.db')
# 创建学生信息表
conn.execute('''CREATE TABLE STUDENTS
(ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
GENDER TEXT NOT NULL,
STUDENT_ID TEXT NOT NULL,
PHONE_NUMBER TEXT NOT NULL,
MAJOR TEXT NOT NULL,
CLASS TEXT NOT NULL);''')
# 插入一些样本数据
conn.execute("INSERT INTO STUDENTS (ID,NAME,GENDER,STUDENT_ID,PHONE_NUMBER,MAJOR,CLASS) \
VALUES (1, '张三', '男', '10001', '13812345678', '计算机科学', '计科1班')")
# 定义一个函数:在GUI中显示学生信息
def show_students():
cursor = conn.execute("SELECT ID, NAME, GENDER, STUDENT_ID, PHONE_NUMBER, MAJOR, CLASS from STUDENTS")
for row in cursor:
print("ID = ", row[0])
print("NAME = ", row[1])
print("GENDER = ", row[2])
print("STUDENT_ID = ", row[3])
print("PHONE_NUMBER = ", row[4])
print("MAJOR = ", row[5])
print("CLASS = ", row[6])
# 定义一个函数:在GUI中添加学生信息
def add_student():
id_val = id_entry.get()
name_val = name_entry.get()
gender_val = gender_entry.get()
student_id_val = student_id_entry.get()
phone_number_val = phone_number_entry.get()
major_val = major_entry.get()
class_val = class_entry.get()
conn.execute("INSERT INTO STUDENTS (ID, NAME, GENDER, STUDENT_ID, PHONE_NUMBER, MAJOR, CLASS) \
VALUES (?, ?, ?, ?, ?, ?, ?)", (id_val, name_val, gender_val, student_id_val, phone_number_val, major_val, class_val))
conn.commit()
print("学生信息已添加成功!")
# 创建GUI界面并添加控件
window = tk.Tk()
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)
gender_label = tk.Label(window, text="性别")
gender_label.grid(row=2, column=0)
gender_entry = tk.Entry(window)
gender_entry.grid(row=2, column=1)
student_id_label = tk.Label(window, text="学号")
student_id_label.grid(row=3, column=0)
student_id_entry = tk.Entry(window)
student_id_entry.grid(row=3, column=1)
phone_number_label = tk.Label(window, text="手机号")
phone_number_label.grid(row=4, column=0)
phone_number_entry = tk.Entry(window)
phone_number_entry.grid(row=4, column=1)
major_label = tk.Label(window, text="专业")
major_label.grid(row=5, column=0)
major_entry = tk.Entry(window)
major_entry.grid(row=5, column=1)
class_label = tk.Label(window, text="班级")
class_label.grid(row=6, column=0)
class_entry = tk.Entry(window)
class_entry.grid(row=6, column=1)
show_students_button = tk.Button(window, text="显示学生信息", command=show_students)
show_students_button.grid(row=7, column=0)
add_student_button = tk.Button(window, text="添加学生信息", command=add_student)
add_student_button.grid(row=7, column=1)
window.mainloop()
```
以上代码实现了一个基本的学生信息管理系统,你可以将其运行在你的本地计算机上,将SQLite数据库存储的数据显示在GUI界面上,并且可以通过相应的输入框添加学生信息。更多关于Python Tkinter 和 SQLite库的使用可以查看官方文档。
阅读全文