python超详细实现完整学生成绩管理系统
作者:hacker707 时间:2022-08-25 08:59:08
学生成绩管理系统简介
一个带有登录界面具有增减改查功能的学生成绩管理系统(面向对象思想,利用tkinter库进行制作,利用.txt文件进行存储数据)
源代码
✅仅供学习参考,最好还是自己多敲多练习(实践是检验真理的唯一标准)
students.txt
用于存储数据
main.py
from tkinter import *
from Login import *
import tkinter as tk
root = tk.Tk()
root.title('欢迎进入学生成绩管理系统')
LoginPage(root)
root.mainloop()
Login.py
from tkinter import *
from tkinter.messagebox import *
from MenuPage import *
class LoginPage(object):
def __init__(self, master=None):
self.root = master # 定义内部变量root
self.root.geometry('%dx%d' % (300, 180)) # 设置窗口大小
self.username = StringVar()
self.password = StringVar()
self.createPage()
def createPage(self):
self.page = Frame(self.root) # 创建Frame
self.page.pack()
Label(self.page).grid(row=0, stick=W)
Label(self.page, text='账户: ').grid(row=1, stick=W, pady=10)
Entry(self.page, textvariable=self.username).grid(row=1, column=1, stick=E)
Label(self.page, text='密码: ').grid(row=2, stick=W, pady=10)
Entry(self.page, textvariable=self.password, show='*').grid(row=2, column=1, stick=E)
Button(self.page, text='登陆', command=self.loginCheck).grid(row=3, stick=W, pady=10)
Button(self.page, text='退出', command=self.page.quit).grid(row=3, column=1, stick=E)
def loginCheck(self):
name = self.username.get()
password = self.password.get()
if name == 'hacker707' and password == 'admin':
self.page.destroy()
MenuPage(self.root)
else:
showinfo(title='错误', message='账号或密码错误!')
db.py
import json
class StudentDB(object):
def __init__(self):
self.students = []
self._load_students_data()
def insert(self, student):
self.students.append(student)
print(self.students)
def all(self):
return self.students
def delete_by_name(self, name): # 删除数据
for student in self.students:
if name == student["name"]:
self.students.remove(student)
break
else:
return False
return True
# 查询
def search_by_name(self, name):
for student in self.students:
if name == student["name"]:
return student # 姓名+成绩
else:
return False
# 修改
def update(self, stu): # 修改数据
name = stu["name"]
for student in self.students:
if name == student["name"]:
student.update(stu)
return True
else:
return False
# 加载文件
def _load_students_data(self):
with open("students.txt", "r", encoding="utf-8") as f:
text = f.read()
if text:
self.students = json.loads(text)
# 保存数据
def save_data(self):
with open("students.txt", 'w', encoding="utf-8") as f:
text = json.dumps(self.students, ensure_ascii=False)
f.write(text)
db = StudentDB()
MenuPage.py
import tkinter as tk
from view import *
class MenuPage(object):
def __init__(self, master=None):
self.root = master
self.root.geometry('%dx%d' % (600, 400))
self.create_page()
self.input_page = InputFrame(self.root)
self.query_page = QuerryFrame(self.root)
self.delete_page = DeleteFrame(self.root)
self.update_page = UpdateFrame(self.root)
self.about_page = AboutFrame(self.root)
self.input_page.pack()
def create_page(self):
# 创建菜单对象
menubar = tk.Menu(self.root)
# add_command 添加
menubar.add_command(label="录入", command=self.input_data) # label
menubar.add_command(label="查询", command=self.query_data) # label
menubar.add_command(label="删除", command=self.delete_data) # label
menubar.add_command(label="修改", command=self.update_data) # label
menubar.add_command(label="关于", command=self.about_data) # label
# 设置菜单栏
self.root.config(menu=menubar)
# 切换界面
def input_data(self):
self.input_page.pack()
self.update_page.pack_forget()
self.delete_page.pack_forget()
self.about_page.pack_forget()
self.query_page.pack_forget()
def query_data(self):
self.input_page.pack_forget()
self.query_page.pack()
self.update_page.pack_forget()
self.delete_page.pack_forget()
self.about_page.pack_forget()
def update_data(self):
self.input_page.pack_forget()
self.update_page.pack()
self.delete_page.pack_forget()
self.about_page.pack_forget()
self.query_page.pack_forget()
def delete_data(self):
self.input_page.pack_forget()
self.update_page.pack_forget()
self.delete_page.pack()
self.about_page.pack_forget()
self.query_page.pack_forget()
def about_data(self):
self.input_page.pack_forget()
self.update_page.pack_forget()
self.delete_page.pack_forget()
self.about_page.pack()
self.query_page.pack_forget()
view.py
import tkinter as tk
from db import db
from tkinter import ttk
# 录入类
class InputFrame(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.root = master
self.name = tk.StringVar()
self.math = tk.StringVar()
self.chinese = tk.StringVar()
self.english = tk.StringVar()
self.status = tk.StringVar()
self.create_page()
def create_page(self):
tk.Label(self).grid(row=0, stick=tk.W, pady=10)
tk.Label(self, text="姓名:").grid(row=1, stick=tk.W, pady=10)
# 单行文本框 entry,textvariable绑定变量
tk.Entry(self, textvariable=self.name).grid(row=1, column=1, stick=tk.E)
tk.Label(self, text="数学:").grid(row=2, stick=tk.W, pady=10)
# 单行文本框 entry,textvariable绑定变量
tk.Entry(self, textvariable=self.math).grid(row=2, column=1, stick=tk.E)
tk.Label(self, text="语文:").grid(row=3, stick=tk.W, pady=10)
# 单行文本框 entry,textvariable绑定变量
tk.Entry(self, textvariable=self.chinese).grid(row=3, column=1, stick=tk.E)
tk.Label(self, text="英语:").grid(row=4, stick=tk.W, pady=10)
# 单行文本框 entry,textvariable绑定变量
tk.Entry(self, textvariable=self.english).grid(row=4, column=1, stick=tk.E)
tk.Button(self, text="录入", command=self.recode_student).grid(row=5, column=1, stick=tk.E, pady=10)
tk.Label(self, textvariable=self.status).grid(row=6, column=1, stick=tk.E, pady=10)
# 录入成绩
def recode_student(self):
student = {
"name": self.name.get(),
"math": self.math.get(),
"chinese": self.chinese.get(),
"english": self.english.get(),
} # 一个学生的成绩
db.insert(student)
# get()得到值
# set()设置值
self.status.set("插入数据成功!")
self._clear_data()
db.save_data()
# 清空文本数据
def _clear_data(self):
self.name.set("")
self.math.set("")
self.chinese.set("")
self.english.set("")
# 查询类
class QuerryFrame(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.root = master
self.create_page()
# 创建查询界面
def create_page(self):
self.create_tree_view()
self.show_data_frame()
# grid()
tk.Button(self, text="刷新数据", command=self.show_data_frame).pack(anchor=tk.E, pady=5)
# Treeview
def create_tree_view(self):
# 表头
columns = ("name", "chinese", "math", "english")
self.tree_view = ttk.Treeview(self, show='headings', columns=columns)
self.tree_view.column("name", width=80, anchor='center')
self.tree_view.column("chinese", width=80, anchor='center')
self.tree_view.column("math", width=80, anchor='center')
self.tree_view.column("english", width=80, anchor='center')
self.tree_view.heading("name", text='姓名')
self.tree_view.heading("chinese", text='语文')
self.tree_view.heading("math", text='数学')
self.tree_view.heading("english", text='英语')
self.tree_view.pack()
# 显示数据
def show_data_frame(self):
# 删除原节点 map(int,值)
for i in map(self.tree_view.delete, self.tree_view.get_children("")):
pass
# 拿到列表里面所有值、students[]
students = db.all()
# 同时拿到索引跟value值
for index, stu in enumerate(students):
self.tree_view.insert('', index, values=(stu["name"], stu["chinese"], stu
["math"], stu["english"]))
class DeleteFrame(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
tk.Label(self, text='删除数据').pack()
self.status = tk.StringVar()
self.de_name = tk.StringVar() # 获取删除学生的姓名
self.create_page()
# 创建界面
def create_page(self):
tk.Label(self, text="根据姓名删除信息").pack(anchor=tk.W, padx=20)
e1 = tk.Entry(self, textvariable=self.de_name)
e1.pack(side=tk.LEFT, padx=20, pady=5)
tk.Button(self, text='删除', command=self._delete).pack(side=tk.RIGHT)
tk.Label(self, textvariable=self.status).pack()
# 删除
def _delete(self):
name = self.de_name.get()
print(name)
result = db.delete_by_name(name)
if result:
self.status.set(f'{name}已经被删')
self.de_name.set("")
else:
self.status.set(f'{name}不存在')
class UpdateFrame(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.root = master
tk.Label(self, text='修改界面').pack()
self.change_frame = tk.Frame(self)
self.change_frame.pack()
self.name = tk.StringVar()
self.math = tk.StringVar()
self.chinese = tk.StringVar()
self.english = tk.StringVar()
self.status = tk.StringVar()
self.create_page()
def create_page(self):
tk.Label(self.change_frame).grid(row=0, stick=tk.W, pady=10)
tk.Label(self.change_frame, text="姓名:").grid(row=1, stick=tk.W, pady=10)
# 单行文本框 entry,textvariable绑定变量
tk.Entry(self.change_frame, textvariable=self.name).grid(row=1, column=1, stick=tk.E)
tk.Label(self.change_frame, text="数学:").grid(row=2, stick=tk.W, pady=10)
# 单行文本框 entry,textvariable绑定变量
tk.Entry(self.change_frame, textvariable=self.math).grid(row=2, column=1, stick=tk.E)
tk.Label(self.change_frame, text="语文:").grid(row=3, stick=tk.W, pady=10)
# 单行文本框 entry,textvariable绑定变量
tk.Entry(self.change_frame, textvariable=self.chinese).grid(row=3, column=1, stick=tk.E)
tk.Label(self.change_frame, text="英语:").grid(row=4, stick=tk.W, pady=10)
# 单行文本框 entry,textvariable绑定变量
tk.Entry(self.change_frame, textvariable=self.english).grid(row=4, column=1, stick=tk.E)
# 按钮
tk.Button(self.change_frame, text='查询', command=self._search).grid(row=6, column=0, stick=tk.W, pady=10)
tk.Button(self.change_frame, text='修改', command=self._change).grid(row=6, column=1, stick=tk.E, pady=10)
tk.Label(self.change_frame, textvariable=self.status).grid(row=7, column=1, stick=tk.E, pady=10)
# 查询
def _search(self):
name = self.name.get()
student = db.search_by_name(name)
if student:
self.math.set(student["math"])
self.chinese.set(student["chinese"])
self.english.set(student["english"])
self.status.set(f'查询到{name}同学的信息')
else:
self.status.set(f'没有查询到{name}同学的信息')
# 更改成绩
def _change(self):
name = self.name.get()
math = self.math.get()
chinese = self.chinese.get()
english = self.english.get()
stu = {
"name": name,
"math": math,
"chinese": chinese,
"english": english,
}
r = db.update(stu)
if r:
self.status.set(f"{name}同学的信息更新完毕")
else:
self.status.set(f"{name}同学的信息更新失败")
class AboutFrame(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.root = master
self.create_page()
def create_page(self):
tk.Label(self, text="关于本作品(人生苦短,我用python)").pack(anchor=tk.W)
🔥以上就是使用python实现学生成绩管理系统,如果有改进的建议,欢迎在评论区留言奥~
这篇文章参加了csdn的活动,还请大家多多三连支持一下博主,你们的支持就是我创作的动力💖
来源:https://blog.csdn.net/xqe777/article/details/123532947
标签:python,学生,成绩管理
0
投稿
猜你喜欢
python实现一组典型数据格式转换
2023-07-14 22:19:04
react-native ListView下拉刷新上拉加载实现代码
2023-07-02 06:35:34
MySQL6.0新增特性
2010-03-25 10:08:00
Pycharm Plugins加载失败问题解决方案
2023-12-28 22:28:49
基于Python实现原生的登录验证码详情
2021-12-23 02:26:02
微信小程序列表渲染功能之列表下拉刷新及上拉加载的实现方法分析
2024-05-11 09:34:44
Python Pandas list列表数据列拆分成多行的方法实现
2021-04-20 18:56:01
详解MySQL中的死锁情况以及对死锁的处理方法
2024-01-20 05:33:33
Python操作Word批量生成合同的实现示例
2023-06-17 09:59:14
Python如何避免文件同名产生覆盖
2021-02-05 21:16:34
Python opencv医学处理的实现过程
2021-11-19 01:51:47
Python 获取numpy.array索引值的实例
2021-01-19 21:36:57
js创建一个input数组并绑定click事件的方法
2023-08-15 02:35:09
Python中关于浮点数的冷知识
2023-11-13 14:01:57
python3爬虫中多线程进行解锁操作实例
2021-04-29 00:16:42
什么是Dynamic HTML(Dhtml)
2010-03-16 12:40:00
深度剖析使用python抓取网页正文的源码
2022-09-29 15:53:39
PHP PDOStatement::fetch讲解
2023-06-04 20:06:02
mysql load data infile 的用法(40w数据 用了3-5秒导进mysql)
2024-01-19 00:24:22
【JavaScript实用系列】-- 胖页面载入效果
2009-10-19 22:48:00