Add files via upload

This commit is contained in:
Billts-noo 2024-07-11 12:54:29 +08:00 committed by GitHub
parent b2e74fb9ea
commit 503b9dc12b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 340 additions and 0 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

38
functions.py Normal file
View File

@ -0,0 +1,38 @@
"""管理系统需要使用到的函数"""
from student import Student
# 获得主程序的列表供函数使用
stu_names_list, stu_info_list = [], []
def share_lists(
names_list,
info_list,
):
"""接受主程序传递的列表"""
global stu_names_list, stu_info_list
stu_names_list = names_list
stu_info_list = info_list
# 功能函数
def return_student_info(name):
"""根据名字返回学生信息"""
for student in stu_info_list:
if student.name == name:
return student
def add_student(name, gender):
"""添加学生到列表"""
new_student_info = Student(name, gender)
stu_info_list.append(new_student_info)
stu_names_list.append(name)
print("添加成功!")
def delete_student(info, name):
"""根据名字删除列表中的学生"""
stu_info_list.remove(info)
stu_names_list.remove(name)

138
gui.py Normal file
View File

@ -0,0 +1,138 @@
import tkinter as tk
import ttkbootstrap as ttk
import functions as fc
import main
class StudentManagementGUI:
def __init__(self, root):
global screen_height, screen_width, root_width, root_height, add_width, add_height
self.root = root
self.root.title("学生管理系统")
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
root_width = 600
root_height = 500
add_width = 400
add_height = 150
self.root.geometry(
f"{root_width}x{root_height}+{int((screen_width-root_width)/2)}+{int((screen_height-root_height)/2)}"
)
# Student List Label
self.list_label = ttk.Label(root, text="学生列表:")
self.list_label.pack()
# Frame for student list
self.list_box = tk.Listbox(root)
self.list_box.pack(pady=10, fill=tk.BOTH, expand=False)
# Buttons Frame
self.buttons_frame = ttk.Frame(root)
self.buttons_frame.pack(pady=10)
# “添加学生”按钮
self.add_button = ttk.Button(
self.buttons_frame, text="添加学生", command=self.add_student
)
self.add_button.grid(row=0, column=0, padx=5)
# “删除学生”按钮
self.del_button = ttk.Button(
self.buttons_frame, text="删除学生", command=self.del_student
)
self.del_button.grid(row=0, column=1, padx=5)
# Find Student Button
# self.find_button = ttk.Button(
# self.buttons_frame, text="查找学生", command=self.find_student
# )
# self.find_button.grid(row=0, column=2, padx=5)
# “保存并退出”按钮
self.save_quit_button = ttk.Button(
self.root, text="保存并退出", command=self.save_and_quit
)
self.save_quit_button.pack()
# 导入学生数据
main._load_data()
fc.share_lists(main.stu_names_list, main.stu_info_list) # 向模块传递列表
self.update_student_list()
def add_student(self):
def add():
name = name_var.get()
gender = gender_var.get()
fc.add_student(name, gender)
self.update_student_list()
window.destroy()
window = ttk.Toplevel()
window.title("添加学生")
window.geometry(
f"{add_width}x{add_height}+{int((screen_width-add_width)/2)}+{int((screen_height-add_height)/2)}"
)
window.resizable(0, 0)
name_var = ttk.StringVar(window)
gender_var = ttk.StringVar(window)
label1 = ttk.Label(window, text="学生姓名:")
entry1 = ttk.Entry(window, textvariable=name_var)
button1 = ttk.Button(window, text="添加", command=add)
c1 = ttk.Checkbutton(
window, width=4, variable=gender_var, onvalue="", text=""
)
c2 = ttk.Checkbutton(
window, width=4, variable=gender_var, onvalue="", text=""
)
label1.grid(row=1, column=1)
entry1.grid(row=1, column=2, pady=10)
c1.place(x=150, y=60)
c2.place(x=250, y=60)
button1.grid(row=2, column=1)
window.mainloop()
def del_student(self):
stu_name = main.stu_names_list[self.list_box.index("active")]
stu_info = main.stu_info_list[self.list_box.index("active")]
def _del():
fc.delete_student(stu_info, stu_name)
window.destroy()
self.update_student_list()
window = ttk.Toplevel()
label = ttk.Label(window, text=f"确定要删除{stu_name}吗?")
button = ttk.Button(window, text="确定", command=_del)
label.pack()
button.pack()
window.mainloop()
# def find_student(self):
# window = ttk.Toplevel()
# str_var = ttk.StringVar(window)
# l1 = ttk.Label(window,text="要查找的学生姓名:")
# e1 = ttk.Entry(window,textvariable=str_var)
# window.mainloop()
def save_and_quit(self):
main._save_data()
self.root.destroy()
def update_student_list(self):
self.list_box.delete(0, tk.END)
if main.stu_info_list:
for stu_info in main.stu_info_list:
self.list_box.insert(tk.END, str(stu_info))
else:
self.list_box.insert(tk.END, "无学生")
# self.list_text.delete(1.0, tk.END)
# if main.stu_info_list:
# for stu_info in main.stu_info_list:
# self.list_text.insert(tk.END, str(stu_info) + "\n")
# else:
# self.list_text.insert(tk.END, "无学生")
if __name__ == "__main__":
root = ttk.Window(themename="darkly")
app = StudentManagementGUI(root)
root.mainloop()

164
main.py Normal file
View File

@ -0,0 +1,164 @@
import functions as fc
from student import Student
import os
import pickle
# 变量名缩写:
# stu(s) = student(s)
# info = information
stu_names_list, stu_info_list = [], []
active = True
input_message = "输入数字以执行对应操作:"
stu_info_data = "students info.json"
stu_names_data = "students names.json"
def _load_data():
global stu_info_list, stu_names_list
if os.path.exists(stu_info_data): # 判断数据文件是否存在
with open(stu_info_data, "rb") as info_data:
stu_info_list = pickle.load(info_data) # 如果存在则导入数据
print("已找到文件students info.json!")
else:
print(
"""找不到文件students info.json
不过这不影响系统的正常运行系统退出后会自动生成"""
)
if os.path.exists(stu_names_data):
with open(stu_names_data, "rb") as name_data:
stu_names_list = pickle.load(name_data)
print("已找到文件students names.json!")
else:
print(
"""找不到文件students names.json
不过这不影响系统的正常运行系统退出后会自动生成"""
)
def _show_list():
if stu_info_list:
print(f"\n学生列表共 {len(stu_info_list)} 名学生:")
for stu_info in stu_info_list:
print(stu_info)
else:
print("\n无学生")
def _add_student():
new_stu_name = input("请输入要添加的学生的姓名:")
if new_stu_name:
while True:
new_stu_gender = input("0 男\n1 女\n请选择新学生的性别:")
if new_stu_gender == "0":
fc.add_student(new_stu_name, "")
break
elif new_stu_gender == "1":
fc.add_student(new_stu_name, "")
break
else:
print("别瞎jb乱搞")
else:
print("别瞎jb乱搞")
def _del_student():
while True:
del_stu_name = input("请输入要删除的学生的姓名:")
del_stu_info = fc.return_student_info(del_stu_name)
if del_stu_info:
fc.delete_student(del_stu_info, del_stu_name)
print(f"学生{del_stu_name}已被删除!")
break
elif not del_stu_name:
print("已退出!")
break
else:
print(f"未找到学生{del_stu_name}!请检查输入是否正确")
def _find_student():
while True:
find_stu_name = input("请输入要查找的学生的姓名:(按回车退出)")
find_stu_info = fc.return_student_info(find_stu_name)
if find_stu_info:
print("\n查询结果如下:")
print(find_stu_info)
way1 = input(f'''
0 修改信息 1 退出
{input_message}
''')
if way1 == '0':
changed_name,changed_gender = input('请依次输入修改后的姓名、性别:\n').split()
find_stu_info.change_info(changed_name,changed_gender)
print('修改成功!')
if way1 == '1':
None
_show_list()
break
elif not find_stu_name:
print("已退出!")
break
else:
print(f"未找到学生{find_stu_name}!请检查输入是否正确")
def _save_data():
pickle.dump(stu_info_list, file=open(stu_info_data, "wb"))
pickle.dump(stu_names_list, open(stu_names_data, "wb"))
print("数据保存成功!")
if __name__ == "__main__":
_load_data()
fc.share_lists(stu_names_list, stu_info_list) # 向模块传递列表
# 简介
print(
"""
学生管理系统Beta
By Billts_noo"""
)
# 主程序
while active:
main_way = input(
f"""
主菜单
0 显示学生列表
1 保存并退出
{input_message}"""
)
# 主菜单
# 显示学生列表
if main_way == "0":
_show_list()
while True:
way = input(
f"""
学生列表菜单
0 添加学生
1 删除学生
2 查找学生
任意输入 回到主菜单
{input_message}"""
)
# 学生列表菜单
if way == "0":
_add_student()
if way == "1":
_del_student()
if way == "2":
_find_student()
else:
break
# 退出
if main_way == "1":
# 保存数据
_save_data()
active = False

BIN
students info.json Normal file

Binary file not shown.

BIN
students names.json Normal file

Binary file not shown.