GUI
import pandas as pd
import tkinter as tk
from tkinter import messagebox
# 创建数据库表
# Excel表中必须包含学号、班级、姓名这三列
class_info = pd.read_excel('students.xlsx')
# 创建GUI程序
root = tk.Tk()
root.title('班级信息收集')
# 设置窗口大小
window_width = 400
window_height = 300
# 获取屏幕宽度和高度
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
# 计算窗口居中时的位置
x = (screen_width - window_width) // 2
y = (screen_height - window_height) // 2
# 设置窗口初始位置和大小
root.geometry(f'{window_width}x{window_height}+{x}+{y}')
# 注册函数
def register():
global class_info
student_id = entry_id.get()
class_name = entry_class.get()
student_name = entry_name.get()
if not student_id or not class_name or not student_name:
messagebox.showerror('错误', '请填写完整信息')
else:
# 检查学号是否已存在
if student_id in class_info['学号'].values:
messagebox.showerror('错误', '该学号已被注册')
else:
# 添加新记录
new_record = pd.DataFrame({'学号': [student_id], '班级': [class_name], '姓名': [student_name]})
class_info = class_info._append(new_record, ignore_index=True)
messagebox.showinfo('成功', '注册成功')
class_info.to_excel('students.xlsx', index=False)
# 查询函数
def query():
student_name = entry_query_name.get()
class_name = entry_query_class.get()
if not class_name or not student_name:
messagebox.showerror('错误', '请填写完整信息')
else:
result = class_info[(class_info['姓名'] == student_name) & (class_info['班级'] == class_name)]['学号']
if len(result) == 0:
messagebox.showinfo('查询结果', '未找到该同学')
else:
messagebox.showinfo('查询结果', f'学号为: {result.values[0]}')
# 创建注册表单
label_id = tk.Label(root, text='学号', font=('Arial', 12), padx=10, pady=5)
label_id.grid(row=0, column=0)
entry_id = tk.Entry(root, font=('Arial', 12))
entry_id.grid(row=0, column=1)
label_class = tk.Label(root, text='班级', font=('Arial', 12), padx=10, pady=5)
label_class.grid(row=1, column=0)
entry_class = tk.Entry(root, font=('Arial', 12))
entry_class.grid(row=1, column=1)
label_name = tk.Label(root, text='姓名', font=('Arial', 12), padx=10, pady=5)
label_name.grid(row=2, column=0)
entry_name = tk.Entry(root, font=('Arial', 12))
entry_name.grid(row=2, column=1)
button_register = tk.Button(root, text='注册', font=('Arial', 12), bg='white', fg='black', command=register)
button_register.grid(row=3, column=0, columnspan=2, padx=10, pady=5)
# 创建查询表单
label_query_class = tk.Label(root, text='查询班级', font=('Arial', 12), padx=10, pady=5)
label_query_class.grid(row=4, column=0)
entry_query_class = tk.Entry(root, font=('Arial', 12))
entry_query_class.grid(row=4, column=1)
label_query_name = tk.Label(root, text='查询姓名', font=('Arial', 12), padx=10, pady=5)
label_query_name.grid(row=5, column=0)
entry_query_name = tk.Entry(root, font=('Arial', 12))
entry_query_name.grid(row=5, column=1)
button_query = tk.Button(root, text='查询', font=('Arial', 12), bg='white', fg='black', command=query)
button_query.grid(row=6, column=0, columnspan=2, padx=10, pady=5)
root.mainloop()