import tkinter as tk
from tkinter import messagebox
import sqlite3
# 数据库连接和游标
conn = sqlite3.connect('class_info.db')
cursor = conn.cursor()
# 创建表(如果尚不存在)
cursor.execute('''CREATE TABLE IF NOT EXISTS students
(id INTEGER PRIMARY KEY AUTOINCREMENT, student_id TEXT, class_name TEXT, name TEXT)''')
conn.commit()
def register_student():
student_id_entry = entry_student_id.get()
class_name_entry = entry_class_name.get()
name_entry = entry_name.get()
if not all([student_id_entry, class_name_entry, name_entry]):
messagebox.showerror("错误", "所有字段都必须填写!")
return
cursor.execute("INSERT INTO students (student_id, class_name, name) VALUES (?, ?, ?)",
(student_id_entry, class_name_entry, name_entry))
conn.commit()
messagebox.showinfo("成功", "学生信息已注册!")
clear_entries()
def query_students():
class_name_to_query = entry_query_class.get()
cursor.execute("SELECT student_id FROM students WHERE class_name=?", (class_name_to_query,))
student_ids = cursor.fetchall()
if not student_ids:
messagebox.showinfo("查询结果", "没有找到该班级的学生信息。")
else:
result_str = "\n".join(f"学号: {student_id[0]}" for student_id in student_ids)
messagebox.showinfo("查询结果", result_str)
def clear_entries():
entry_student_id.delete(0, tk.END)
entry_class_name.delete(0, tk.END)
entry_name.delete(0, tk.END)
entry_query_class.delete(0, tk.END)
# GUI 设置
root = tk.Tk()
root.title("班级信息收集")
tk.Label(root, text="学号:").grid(row=0, column=0)
entry_student_id = tk.Entry(root)
entry_student_id.grid(row=0, column=1)
tk.Label(root, text="班级:").grid(row=1, column=0)
entry_class_name = tk.Entry(root)
entry_class_name.grid(row=1, column=1)
tk.Label(root, text="姓名:").grid(row=2, column=0)
entry_name = tk.Entry(root)
entry_name.grid(row=2, column=1)
tk.Button(root, text="注册", command=register_student).grid(row=3, column=0, columnspan=2)
tk.Label(root, text="查询班级:")