import tkinter as tk
from tkinter import messagebox
from tkinter import ttk
# 添加学生信息
def add_student_info():
name = entry_name.get()
student_id = entry_id.get()
gpa = entry_gpa.get()
with open("student.txt", "a") as file:
file.write(f"姓名: {name}, 学号: {student_id}, 绩点: {gpa}\n")
messagebox.showinfo("成功", "学生信息添加成功!")
entry_name.delete(0, tk.END)
entry_id.delete(0, tk.END)
entry_gpa.delete(0, tk.END)
# 删除学生信息
def delete_student_info():
student_id_to_delete = entry_delete.get()
lines = []
with open("student.txt", "r") as file:
lines = file.readlines()
found = False
with open("student.txt", "w") as file:
for line in lines:
if f"学号: {student_id_to_delete}" not in line:
file.write(line)
else:
found = True
entry_delete.delete(0, tk.END)
if found:
messagebox.showinfo("成功", "学生信息删除成功!")
else:
messagebox.showerror("错误", "该学生不存在!")
# 修改学生信息
def modify_student_info():
student_id_to_modify = entry_modify_id.get()
found = False
updated_lines = []
with open("student.txt", "r") as file:
lines = file.readlines()
for line in lines:
if f"学号: {student_id_to_modify}" in line:
found = True
updated_lines.append(f"姓名: {entry_modify_name.get()}, 学号: {entry_modify_student_id.get()}, 绩点: {entry_modify_gpa.get()}\n")
else:
updated_lines.append(line)
with open("student.txt", "w") as file:
for line in updated_lines:
file.write(line)
entry_modify_id.delete(0, tk.END)
entry_modify_name.delete(0, tk.END)
entry_modify_student_id.delete(0, tk.END)
entry_modify_gpa.delete(0, tk.END)
if found:
messagebox.showinfo("成功", "学生信息修改成功!")
else:
messagebox.showerror("错误", "该学生不存在!")
# 查询学生信息
def find_student_info_by_id():
student_id = entry_search.get()
found = False
with open("student.txt", "r") as file:
lines = file.readlines()
for line in lines:
if f"学号: {student_id}" in line:
found = True
messagebox.showinfo("成功", f"找到对应学生信息:\n{line.strip()}")
break
if not found:
messagebox.showerror("错误", "未找到该学生信息,请检查输入的学号是否正确。")
# 根据绩点排序
def sort_students_by_gpa():
with open("student.txt", "r") as file:
lines = file.readlines()
students = []
for line in lines:
parts = line.strip().split(", ")
name = parts[0].split(": ")[1]
student_id = parts[1].split(": ")[1]
gpa = float(parts[2].split(": ")[1])
students.append({'name': name, 'id': student_id, 'gpa': gpa})
sorted_students = sorted(students, key=lambda x: x['gpa'], reverse=True)
sorted_info = ""
for student_info in sorted_students:
sorted_info += f"姓名: {student_info['name']} 学号: {student_info['id']} 绩点: {student_info['gpa']}\n"
messagebox.showinfo("学生信息按绩点降序排序", sorted_info)
def student():
root = tk.Tk()
root.title("学生信息管理系统")
notebook = ttk.Notebook(root)
notebook.pack()
# 创建选项卡
tab1 = ttk.Frame(notebook)
tab2 = ttk.Frame(notebook)
tab3 = ttk.Frame(notebook)
tab4 = ttk.Frame(notebook)
tab5 = ttk.Frame(notebook)
notebook.add(tab1, text="添加学生信息")
notebook.add(tab2, text="删除学生信息")
notebook.add(tab3, text="修改学生信息")
notebook.add(tab4, text="查询学生信息")
notebook.add(tab5, text="按绩点排序")
# 添加学生信息选项卡
tk.Label(tab1, text="姓名:").pack()
global entry_name
entry_name = tk.Entry(tab1)
entry_name.pack()
tk.Label(tab1, text="学号:").pack()
global entry_id
entry_id = tk.Entry(tab1)
entry_id.pack()
tk.Label(tab1, text="绩点:").pack()
global entry_gpa
entry_gpa = tk.Entry(tab1)
entry_gpa.pack()
tk.Button(tab1, text="添加", command=add_student_info).pack()
# 删除学生信息选项卡
tk.Label(tab2, text="输入要删除的学生学号:").pack()
global entry_delete
entry_delete = tk.Entry(tab2)
entry_delete.pack()
tk.Button(tab2, text="删除", command=delete_student_info).pack()
# 修改学生信息选项卡
tk.Label(tab3, text="输入要修改的学生学号:").pack()
global entry_modify_id
entry_modify_id = tk.Entry(tab3)
entry_modify_id.pack()
tk.Label(tab3, text="新姓名:").pack()
global entry_modify_name
entry_modify_name = tk.Entry(tab3)
entry_modify_name.pack()
tk.Label(tab3, text="新学号:").pack()
global entry_modify_student_id
entry_modify_student_id = tk.Entry(tab3)
entry_modify_student_id.pack()
tk.Label(tab3, text="新绩点:").pack()
global entry_modify_gpa
entry_modify_gpa = tk.Entry(tab3)
entry_modify_gpa.pack()
tk.Button(tab3, text="确认修改", command=modify_student_info).pack()
# 查询学生信息选项卡
tk.Label(tab4, text="请输入学生学号:").pack()
global entry_search
entry_search = tk.Entry(tab4)
entry_search.pack()
tk.Button(tab4, text="查询", command=find_student_info_by_id).pack()
# 按绩点排序选项卡
tk.Button(tab5, text="显示", command=sort_students_by_gpa).pack()
root.mainloop()import os
import tkinter as tk
from tkinter import messagebox
def login():
username = username_entry.get()
password = password_entry.get()
current_directory = os.path.dirname(os.path.abspath(__file__))
account_file_path = os.path.join(current_directory, 'Account.txt')
with open(account_file_path, 'r') as f:
accounts = [line.split() for line in f.readlines()]
for account in accounts:
if len(account) == 2 and account[0] == username and account[1] == password:
messagebox.showinfo("成功", "登录成功")
root.destroy() # 关闭当前窗口
from Student import student
student()
return
messagebox.showerror("错误", "用户名或密码错误,请重新输入")
def register():
username = username_entry.get() # 获取输入的用户名
password = password_entry.get() # 获取输入的密码
# 读取现有文件中的账号信息
accounts = []
with open('Account.txt', 'r') as f:
accounts = [line.strip().split() for line in f]
# 检查账号是否已存在
for account in accounts:
if account[0] == username:
messagebox.showerror("错误", "该账号已存在")
return
with open('Account.txt', 'a') as f:
f.write(f"{username} {password}\n")
messagebox.showinfo("成功", "注册成功")
root.destroy() # 关闭当前窗口
from Student import student
student()
return
# 创建 Tkinter 窗口
root = tk.Tk()
root.title("登录/注册")
root.geometry("300x200")
# 创建登录和注册的输入框
username_label = tk.Label(root, text="用户名:")
username_label.pack()
username_entry = tk.Entry(root)
username_entry.pack()
password_label = tk.Label(root, text="密码:")
password_label.pack()
password_entry = tk.Entry(root, show="*")
password_entry.pack()
# 创建登录和注册按钮
login_button = tk.Button(root, text="登录", command=login)
login_button.pack()
register_button = tk.Button(root, text="注册", command=register)
register_button.pack()
root.mainloop()
最新发布