学生管理系统Python版
立即下载
资源介绍:
两份不同的学生管理系统,用到了tk写了GUI,编程语言为Python,其中一份在我的一篇博客中也有代码
import tkinter as tk
from tkinter import scrolledtext, messagebox
import os
students = [
{"学号": 1, "姓名": "小明", "年龄": 20, "成绩": 66, "性别": "男", "电话": 123456},
{"学号": 2, "姓名": "小红", "年龄": 21, "成绩": 69, "性别": "女", "电话": 130555}
]
path = os.getenv('temp')
filename = os.path.join(path, 'info.txt')
# 登录窗口
def login_window():
login_root = tk.Tk()
login_root.title("登录")
login_root.geometry('800x500')
# 居中布局
frame = tk.Frame(login_root)
frame.place(relx=0.5, rely=0.5, anchor=tk.CENTER)
labelName = tk.Label(frame, text='用户名:', font=('楷体', 30, ''), justify=tk.RIGHT, anchor='e')
labelName.grid(row=0, column=0, padx=10, pady=10)
varName = tk.StringVar(login_root, value='')
entryName = tk.Entry(frame, font=('楷体', 30, ''), textvariable=varName)
entryName.grid(row=0, column=1, padx=10, pady=10)
labelPwd = tk.Label(frame, text='用户密码:', font=('楷体', 30, ''), justify=tk.RIGHT, anchor='e')
labelPwd.grid(row=1, column=0, padx=10, pady=10)
varPwd = tk.StringVar(login_root, value='')
entryPwd = tk.Entry(frame, font=('楷体', 30, ''), show='*', textvariable=varPwd)
entryPwd.grid(row=1, column=1, padx=10, pady=10)
# 尝试自动填写用户名和密码
try:
with open(filename) as fp:
n, p = fp.read().strip().split(' ')
varName.set(n)
varPwd.set(p)
except:
pass
rememberMe = tk.IntVar(login_root, value=1)
checkRemember = tk.Checkbutton(frame, text='记住用户名和密码', variable=rememberMe, font=('楷体', 20))
checkRemember.grid(row=2, columnspan=2, pady=10)
def login():
name = varName.get()
pwd = varPwd.get()
if name == "admin" and pwd == "123456":
if rememberMe.get() == 1:
with open(filename, 'w') as fp:
fp.write(f"{name} {pwd}")
messagebox.showinfo("登录成功", "欢迎,管理员!")
login_root.destroy()
main_window()
else:
messagebox.showerror("登录失败", "用户名或密码错误")
btnLogin = tk.Button(frame, text='登录', command=login, font=('楷体', 30))
btnLogin.grid(row=3, columnspan=2, pady=20)
login_root.mainloop()
# 主功能窗口
def main_window():
root = tk.Tk()
root.title("学生管理系统")
root.geometry('300x500') # 更改窗口大小以适应竖直排列的按钮
def add_student():
def save_student():
sid = int(entryID.get())
name = entryName.get()
age = int(entryAge.get())
score = int(entryScore.get())
gender = entryGender.get()
phone = int(entryPhone.get())
# 检查学号是否已存在
existing_student = next((student for student in students if student["学号"] == sid), None)
if existing_student:
messagebox.showinfo("提示",
f"已存在学号为{sid}的学生:\n姓名:{existing_student['姓名']}\n年龄:{existing_student['年龄']}\n成绩:{existing_student['成绩']}\n性别:{existing_student['性别']}\n电话:{existing_student['电话']}")
return
students.append({"学号": sid, "姓名": name, "年龄": age, "成绩": score, "性别": gender, "电话": phone})
messagebox.showinfo("成功", "学生信息已添加")
add_root.destroy()
add_root = tk.Tk()
add_root.title("添加学生信息")
labels = ["学号", "姓名", "年龄", "成绩", "性别", "电话"]
entries = []
for idx, label_text in enumerate(labels):
label = tk.Label(add_root, text=label_text, font=('楷体', 20, ''), width=10)
label.grid(row=idx, column=0, padx=10, pady=10)
entry = tk.Entry(add_root, font=('楷体', 20, ''), width=20)
entry.grid(row=idx, column=1, padx=10, pady=10)
entries.append(entry)
entryID, entryName, entryAge, entryScore, entryGender, entryPhone = entries
btnSave = tk.Button(add_root, text="保存", command=save_student, font=('楷体', 20))
btnSave.grid(row=len(labels), column=0, columnspan=2, pady=20)
add_root.mainloop()
def display_students():
all_students_text = "\n".join([
f"学号: {student['学号']}, 姓名: {student['姓名']}, 年龄: {student['年龄']}, 成绩: {student['成绩']}, 性别: {student['性别']}, 电话: {student['电话']}"
for student in students])
root6 = tk.Tk()
root6.title("全部学生信息")
text_widget = scrolledtext.ScrolledText(root6, height=30, width=60)
text_widget.pack(fill="both", expand=True)
text_widget.delete(1.0, tk.END)
text_widget.insert(tk.END, all_students_text)
# 删除功能
def delete_student():
# 获取要删除的学生ID
def delete():
# 获取要删除的学生ID
student_id = delete_var.get()
if not student_id:
messagebox.showerror("Error", "请输入学生ID!")
return
try:
# 转换为整数,并检查ID是否在学生列表中
student_id = int(student_id)
student_to_delete = next((s for s in students if s["学号"] == student_id), None)
if student_to_delete:
students.remove(student_to_delete)
messagebox.showinfo("Success", f"学生ID {student_id} 已删除!")
# 清除输入框并更新列表
delete_var.set('') # 清空输入框
else:
messagebox.showerror("Error", "学生ID不存在!")
except ValueError:
messagebox.showerror("Error", "学生ID必须是数字!")
# 创建tkinter窗口
delete_window = tk.Toplevel(root)
delete_window.title("删除学生")
delete_window.geometry('300x100')
delete_label = tk.Label(delete_window, text="请输入学号:")
delete_label.grid(row=0, column=0)
delete_var = tk.StringVar() # 创建一个StringVar用于获取输入框中的值
delete_entry = tk.Entry(delete_window, textvariable=delete_var)
delete_entry.grid(row=0, column=1)
delete_button = tk.Button(delete_window, text="删除学生", command=delete)
delete_button.grid(row=1, column=0, columnspan=2)
# 修改功能
def modify_student():
selected_student_index = None
def save_changes():
nonlocal selected_student_index
if selected_student_index is None:
messagebox.showerror("错误", "请选择要修改的学生.")
return
# 更新学生信息
students[selected_student_index]["姓名"] = name_var.get()
students[selected_student_index]["年龄"] = int(age_var.get())
students[selected_student_index]["成绩"] = int(grade_var.get())
students[selected_student_index]["性别"] = gender_var.get()
students[selected_student_index]["电话"] = int(tel_var.get())
messagebox.showinfo("成功", "学生信息修改成功!")
display_students() # 更新显示
modify_window.destroy()
# 创建主窗口
modify_window = tk.Toplevel(root)
modify_window.title("管理学生")
modify_window.geometry("600x600")
# 添加提示语句
tk.Label(modify_window, text="请在下方选择你�