基于python+tkinter的学生成绩信息管理系统

基于python+tkinter的学生成绩信息管理系统

  1. 系统设计
    基于python+tkinter的学生成绩信息管理系统_第1张图片

2.开发工具
开发语言:python3.6.8
开发工具:JetBrains PyCharm 2019.1.2 x64
使用三方模块:os、tkinter、ttkbootstrap、messagebox、datetime、tkinter.ttk

3.系统展示
(1)登录界面
基于python+tkinter的学生成绩信息管理系统_第2张图片
(2)管理界面
基于python+tkinter的学生成绩信息管理系统_第3张图片
4.程序设计
Stu_login.py实现系统登录
Maingui.py 实现系统管理
studetailgui.py实现学生添加、修改、查看功能
changegui.py 实现用户权限,用户信息添加、密码修改
img存储程序需要的背景文件
data存储系统数据
基于python+tkinter的学生成绩信息管理系统_第4张图片
5.Stu_login.py系统登录的实现

import os
from tkinter import *
from ttkbootstrap import *
from tkinter.messagebox import *
from datetime import *
import maingui


class login_windows(Tk):
    def __init__(self):
        super().__init__()  # 先执行tk这个类的初始化
        self.title("学生成绩信息管理系统登录界面V2.0")
        self.geometry("544x344+400+200")
        self.iconbitmap("./img/student.ico")  # 给窗体增加一个图标
        self.resizable(0, 0)  # 不允许改变窗体大小

        # 定义全局变量
        self.img_file = "./img/stu_login_png.png"  # 文件路径
        self.user_file = "./data/user.txt"
        self.user_info_list = []  # 存储用户信息
        self.user = ""
        self.password = ""
        self.current_user_list = []  # 存储当前登录的用户名信息
        self.password_error_times = 0  # 记录密码错误次数

        # 运行函数
        self.setup_gui()  # 加载界面显示函数
        self.load_user_info()  # 加载用户信息存储文件

    def setup_gui(self):
        self.style01 = Style(theme="flatly")  # 为窗体设置一个显示主题
        # 创建一个Label展示登录界面
        self.login_img = PhotoImage(file=self.img_file)
        self.label_img = Label(self, image=self.login_img)
        self.label_img.place(x=0, y=0)
        # 创建一个用户名label+entry标签
        self.label_user = Label(self, text="用户名:", font="微软雅黑 14 bold").place(x=20, y=304)
        self.var_user = StringVar()
        self.entry_user = Entry(self, width=10, textvariable=self.var_user, font="微软雅黑 14 bold").place(x=100, y=304)
        # 创建一个密码Label+entry标签
        self.label_password = Label(self, text="密码:", font="微软雅黑 14 bold").place(x=240, y=304)
        self.var_password = StringVar()
        self.entry_password = Entry(self, width=10, show="*", textvariable=self.var_password, font="微软雅黑 14 bold")
        self.entry_password.place(x=300, y=304)
        # 创建一个登录按钮
        self.login_button = Button(self, text="登录", width=8, font="微软雅黑 12 bold", command=self.login).place(x=450,
                                                                                                            y=302)

    def load_user_info(self):
        if not os.path.exists(self.user_file):
            showinfo("消息提示", "数据存储data文件中不存在用户存储信息文件!")
        else:
            try:
                with open(file=self.user_file, mode="r", encoding="utf-8") as refile:
                    current = refile.readlines()
                    for list in current:
                        current_dic = dict(eval(list))
                        self.user_info_list.append(current_dic)
            except:
                showinfo("消息提示", "文件读取异常")

    def login(self):
        # 获取输入的用户名和密码
        self.user = self.var_user.get()
        self.password = self.var_password.get()
        # 身份验证
        for index in self.user_info_list:
            # 判断输入的用户名是否正确
            if self.user.strip().lower() == index["user"]:
                # 判断账号是否被禁用
                if index["times"] != "0":
                    showinfo("消息提示", "账号已被禁用,无法登录,请联系管理员!")
                    break
                # 判断密码是否正确
                if self.password != index["password"]:
                    self.password_error_times += 1  # 记录登录次数
                    # print(self.password_error_times)
                    if self.password_error_times >= 3:
                        showinfo("消息提示", "密码错误已达三次!账号已锁定!")
                        # 改变状态
                        index["times"] = "2"
                        # 将状态改变后的信息写入文件
                        self.write_user_info(self.user_info_list)
                    else:
                        showinfo("消息提示", "输入密码错误!")
                else:
                    self.times = 0
                    self.current_user_list.append(index)
                    self.login_main_windows()
                    break
                    # showinfo("消息提示", "系统登录成功!")
            else:
                showinfo("消息提示", "该用户不存在!")
                break

    def write_user_info(self, user: list):
        try:
            with open(file=self.user_file, mode="w", encoding="utf-8") as wfile:
                for index in user:
                    wfile.write(str(index) + "\n")
        except:
            showinfo("消息提示", "写入文件异常")

    def login_main_windows(self):
        self.destroy()  # 关闭当前窗口
        # 打开新窗口
        maingui.Main_Windows(self.current_user_list, self.get_login_time())

    def get_login_time(self):
        # 获取当前时间
        today = datetime.today()
        return ("%04d-%02d-%02d %02d:%02d:%02d" % (
            today.year, today.month, today.day, today.hour, today.minute, today.second))


if __name__ == "__main__":
    this_windows = login_windows()
    this_windows.mainloop()

6.maingui.py系统管理界面的实现

import os
from tkinter import *
from tkinter.ttk import Treeview

from ttkbootstrap import *
from tkinter.messagebox import *
import studetailgui
import changegui


class Main_Windows(Tk):
    def __init__(self, current_user_list, current_login_time):
        super().__init__()
        self.title("欢迎使用学生成绩信息管理系统V2.0")
        self.geometry("1280x720+120+20")
        self.iconbitmap("./img/student.ico")  # 位窗体添加一个图标
        self["bg"] = "RoyalBlue"

        # 文件路径
        self.student_file = "./data/student.txt"

        # 定义全局变量
        self.current_login_time = current_login_time
        self.current_login_user_list = current_user_list
        self.all_student_info = []  # 存储学生信息
        self.query_result_student = []  # 存储查询信息
        self.sort_result_student = []  # 存储排序后的学生信息
        self.action_flag = 0  # 标记变量
        self.current_student_info = []  # 存储当前的学生信息
        self.change_flag = 0  # 用户信息标记变量

        # 运行函数
        self.setup_gui()  # 加载显示界面
        self.load_student_file()  # 加载学生信息
        self.load_student_info_treeview(self.all_student_info)

    def setup_gui(self):
        # 添加一个显示主题
        self.style01 = Style(theme="flatly")
        # 添加一个显示标签
        self.label_title = Label(self, text="学生成绩信息管理系统", font="微软雅黑 40 bold", relief="groove")
        self.label_title.place(relwidth=1, relheight=0.18, relx=0, rely=0)
        # 添加登录用户和时间显示标签
        self.label_login_time = Label(self, text=self.current_login_time, font="微软雅黑 12")
        self.label_login_time.place(relwidth=0.15, relheight=0.05, relx=0.84, rely=0.12)
        self.label_login_user = Label(self, text="当前登录用户:" + self.get_login_user(), font="微软雅黑 12")
        self.label_login_user.place(relwidth=0.15, relheight=0.05, relx=0.84, rely=0.08)
        # 添加左侧显示区域
        self.Pane_left = PanedWindow(self, relief="groove")
        self.Pane_left.place(relwidth=0.15, relheight=0.82, relx=0, rely=0.18)
        # 添加信息录入按钮
        self.add_button = Button(self.Pane_left, text="录入学生信息", font="微软雅黑 12 bold", command=self.add_student_info)
        self.add_button.place(relwidth=1, relheight=0.08, relx=0, rely=0.05)
        # 添加删除信息按钮
        self.del_button = Button(self.Pane_left, text="删除学生信息", font="微软雅黑 12 bold", command=self.del_student_info)
        self.del_button.place(relwidth=1, relheight=0.08, relx=0, rely=0.18)
        # 添加删除信息按钮
        self.mod_button = Button(self.Pane_left, text="修改学生信息", font="微软雅黑 12 bold", command=self.upadte_student_info)
        self.mod_button.place(relwidth=1, relheight=0.08, relx=0, rely=0.31)
        # 添加密码修改按钮
        self.user_mod_button = Button(self.Pane_left, text="用户权限更改", font="微软雅黑 12 bold",
                                      command=self.permission_upadte)
        self.user_mod_button.place(relwidth=1, relheight=0.08, relx=0, rely=0.44)
        # 成绩单打印
        self.user_mod_button = Button(self.Pane_left, text="用户密码修改", font="微软雅黑 12 bold", command=self.update_user_info)
        self.user_mod_button.place(relwidth=1, relheight=0.08, relx=0, rely=0.57)
        # 添加用户添加按钮
        self.user_add_button = Button(self.Pane_left, text="用户添加", font="微软雅黑 12 bold", command=self.add_user_info)
        self.user_add_button.place(relwidth=1, relheight=0.08, relx=0, rely=0.70)
        # 添加退出系统按钮
        self.exit_button = Button(self.Pane_left, text="退出系统", font="微软雅黑 12 bold", command=self.exit_windows)
        self.exit_button.place(relwidth=1, relheight=0.08, relx=0, rely=0.83)
        # 添加右侧显示区域
        self.Pane_right = PanedWindow(self, relief="groove")
        self.Pane_right.place(relwidth=0.849, relheight=0.82, relx=0.15, rely=0.18)
        # 添加查询选择区域
        self.labelFrame = LabelFrame(self.Pane_right, text="请输入要查询的学生信息", foreground="red")
        self.labelFrame.place(relwidth=0.99, relheight=0.1, relx=0.01, rely=0)
        # 添加学号
        self.id_label = Label(self.labelFrame, text="学号:", font="微软雅黑 12")
        self.id_label.place(relwidth=0.1, relheight=0.8, relx=0, rely=0.1)
        # 添加学号输入区域
        self.var_id = StringVar()
        self.id_entry = Entry(self.labelFrame, textvariable=self.var_id, font="微软雅黑 12")
        self.id_entry.place(relwidth=0.15, relheight=0.8, relx=0.08, rely=0.1)
        # 添加姓名
        self.name_label = Label(self.labelFrame, text="姓名:", font="微软雅黑 12")
        self.name_label.place(relwidth=0.1, relheight=0.8, relx=0.25, rely=0.1)
        # 添加姓名输入区域
        self.var_name = StringVar()
        self.name_entry = Entry(self.labelFrame, textvariable=self.var_name, font="微软雅黑 12")
        self.name_entry.place(relwidth=0.15, relheight=0.8, relx=0.33, rely=0.1)
        # 添加班级
        self.classes_label = Label(self.labelFrame, text="班级:", font="微软雅黑 12")
        self.classes_label.place(relwidth=0.1, relheight=0.8, relx=0.5, rely=0.1)
        # 添加姓名输入区域
        self.var_classes = StringVar()
        self.classes_entry = Entry(self.labelFrame, textvariable=self.var_classes, font="微软雅黑 12")
        self.classes_entry.place(relwidth=0.15, relheight=0.8, relx=0.58, rely=0.1)
        # 添加查询按钮
        self.query_button = Button(self.labelFrame, text="查询", font="微软雅黑 12", command=self.query_student)
        self.query_button.place(relwidth=0.08, relheight=0.8, relx=0.8, rely=0.1)
        # 添加显示全部按钮
        self.show_button = Button(self.labelFrame, text="显示全部", font="微软雅黑 12", command=self.show_all_student)
        self.show_button.place(relwidth=0.08, relheight=0.8, relx=0.91, rely=0.1)
        # 添加学生信息排序区域
        self.labelSort = LabelFrame(self.Pane_right, text="请选择成绩信息排序条件", foreground="red")
        self.labelSort.place(relwidth=0.99, relheight=0.1, relx=0.01, rely=0.1)
        # 添加排序条件
        self.var_sort = IntVar()
        self.total = Radiobutton(self.labelSort, text="总分", variable=self.var_sort, value=1, font="微软雅黑 12")
        self.total.place(relwidth=0.06, relheight=0.8, relx=0.02, rely=0.1)
        # 语文
        self.chinese = Radiobutton(self.labelSort, text="语文", variable=self.var_sort, value=2, font="微软雅黑 12")
        self.chinese.place(relwidth=0.06, relheight=0.8, relx=0.1, rely=0.1)
        # 数学
        self.maths = Radiobutton(self.labelSort, text="数学", variable=self.var_sort, value=3, font="微软雅黑 12")
        self.maths.place(relwidth=0.06, relheight=0.8, relx=0.18, rely=0.1)
        # 英语
        self.english = Radiobutton(self.labelSort, text="英语", variable=self.var_sort, value=4, font="微软雅黑 12")
        self.english.place(relwidth=0.06, relheight=0.8, relx=0.26, rely=0.1)
        # 物理
        self.physics = Radiobutton(self.labelSort, text="物理", variable=self.var_sort, value=5, font="微软雅黑 12")
        self.physics.place(relwidth=0.06, relheight=0.8, relx=0.34, rely=0.1)
        # 化学
        self.chemistry = Radiobutton(self.labelSort, text="化学", variable=self.var_sort, value=6, font="微软雅黑 12")
        self.chemistry.place(relwidth=0.06, relheight=0.8, relx=0.42, rely=0.1)
        # 生物
        self.biology = Radiobutton(self.labelSort, text="生物", variable=self.var_sort, value=7, font="微软雅黑 12")
        self.biology.place(relwidth=0.06, relheight=0.8, relx=0.50, rely=0.1)
        # 历史
        self.history = Radiobutton(self.labelSort, text="历史", variable=self.var_sort, value=8, font="微软雅黑 12")
        self.history.place(relwidth=0.06, relheight=0.8, relx=0.58, rely=0.1)
        # 地理
        self.geography = Radiobutton(self.labelSort, text="地理", variable=self.var_sort, value=9, font="微软雅黑 12")
        self.geography.place(relwidth=0.06, relheight=0.8, relx=0.66, rely=0.1)
        # 政治
        self.plitics = Radiobutton(self.labelSort, text="政治", variable=self.var_sort, value=10, font="微软雅黑 12")
        self.plitics.place(relwidth=0.06, relheight=0.8, relx=0.74, rely=0.1)
        # 添加排序按钮
        self.sort_button = Button(self.labelSort, text="排序", font="微软雅黑 12", command=self.sort_tree_student)
        self.sort_button.place(relwidth=0.08, relheight=0.8, relx=0.91, rely=0.1)
        # 添加treeview显示控件
        self.Tree = Treeview(self.Pane_right,
                             columns=(
                                 "id", "name", "classes", "chinese", "maths", "english", "physics", "chemistry",
                                 "biology",
                                 "history", "geography", "plitics", "total"),
                             show="headings", height=24)
        # 设置每一个列的宽度和对齐方式
        self.Tree.column("id", width=100, anchor="center")
        self.Tree.column("name", width=80, anchor="center")
        self.Tree.column("classes", width=70, anchor="center")
        self.Tree.column("chinese", width=70, anchor="center")
        self.Tree.column("maths", width=70, anchor="center")
        self.Tree.column("english", width=70, anchor="center")
        self.Tree.column("physics", width=70, anchor="center")
        self.Tree.column("chemistry", width=70, anchor="center")
        self.Tree.column("biology", width=70, anchor="center")
        self.Tree.column("history", width=70, anchor="center")
        self.Tree.column("geography", width=70, anchor="center")
        self.Tree.column("plitics", width=70, anchor="center")
        self.Tree.column("total", width=70, anchor="center")
        # 设置每个列的标题
        self.Tree.heading("id", text="学号")
        self.Tree.heading("name", text="姓名")
        self.Tree.heading("classes", text="班级")
        self.Tree.heading("chinese", text="语文")
        self.Tree.heading("maths", text="数学")
        self.Tree.heading("english", text="英语")
        self.Tree.heading("physics", text="物理")
        self.Tree.heading("chemistry", text="化学")
        self.Tree.heading("biology", text="生物")
        self.Tree.heading("history", text="历史")
        self.Tree.heading("geography", text="地理")
        self.Tree.heading("plitics", text="政治")
        self.Tree.heading("total", text="总分")
        # 设置Tree摆放位置
        self.Tree.place(relwidth=0.99, relheight=0.8, relx=0.01, rely=0.20)
        # 添加触发双击某一行的事件
        self.Tree.bind("", self.view_student_info)

    def get_login_user(self):
        for index in self.current_login_user_list:
            return str(index["user"])

    def load_student_file(self):
        if not os.path.exists(self.student_file):
            showinfo("消息提示", "暂未在存储文件夹DATA中找到学生信息存储文件!")
        else:
            try:
                with open(file=self.student_file, mode="r", encoding="utf-8") as rfile:
                    current_line = rfile.readlines()  # 读取学生信息
                    for list in current_line:
                        current_dic = dict(eval(list))  # 将字符串转换成字典
                        self.all_student_info.append(current_dic)  # 将数据存储学生存储列表中
                    # print(self.all_student_info)
            except:
                showinfo("消息提示", "学生信息存储文件读取异常")

    def load_student_info_treeview(self, student_info: list):
        # 清空treevie显示信息
        for i in self.Tree.get_children():
            self.Tree.delete(i)
        # 判断是否存在数据
        if not student_info:
            showinfo("消息提示", "暂时没有要显示的数据!")
        else:
            try:
                for index in student_info:
                    # 将数据插入到tree中显示
                    self.Tree.insert("", END, values=(
                        index["id"], index["name"], index["classes"], index["chinese"], index["maths"],
                        index["english"], index["physics"], index["chemistry"], index["biology"], index["history"],
                        index["geography"], index["plitics"], index["total"]))
            except:
                showinfo("消息提示", "数据显示错误")

    def query_student(self):
        """
        查询学生信息
        :return:
        """
        # 清空查询列表
        self.query_result_student.clear()
        # 准备条件获取查询查询参数
        query_dic = dict()  # 定义一个空字典用来存储查询参数
        query_dic["id"] = self.var_id.get().strip()  # 获取学号
        query_dic["name"] = self.var_name.get().strip()  # 获取姓名
        query_dic["classes"] = self.var_classes.get().strip()  # 获取班级信息
        # 遍历所有符合条件的学生
        for index in self.all_student_info:
            if query_dic["id"] in index["id"] and query_dic["name"] in index["name"] and query_dic["classes"] in index[
                "classes"]:
                # 将满足条件的学生插入查询列表
                self.query_result_student.append(index)
        # 将查询结果显示在treeview中
        self.load_student_info_treeview(self.query_result_student)

    def show_all_student(self):
        """
        显示全部学生信息
        :return:
        """
        # 清空查询参数
        self.var_id.set("")
        self.var_name.set("")
        self.var_classes.set("")
        # 将所有学生信息显示至treeview中
        self.load_student_info_treeview(self.all_student_info)

    def sort_tree_student(self):
        """
        对总学生或查询学生成绩信息进行排序
        :return:
        """
        # 清空排序结果存储列表
        self.sort_result_student.clear()
        # 遍历所有的学生信息
        for index in self.all_student_info:
            self.sort_result_student.append(index)
        # 对所有的学生成绩按总分进行降序排序
        # print(self.var_sort.get())
        if self.var_sort.get() == 1:
            # 按总分降序排列
            self.sort_result_student.sort(key=lambda x: x["total"], reverse=True)
        elif self.var_sort.get() == 2:
            # 按语文成绩降序排列
            self.sort_result_student.sort(key=lambda x: x["chinese"], reverse=True)
        elif self.var_sort.get() == 3:
            # 按数学成绩降序排列
            self.sort_result_student.sort(key=lambda x: x["maths"], reverse=True)
        elif self.var_sort.get() == 4:
            # 按英语成绩降序排列
            self.sort_result_student.sort(key=lambda x: x["english"], reverse=True)
        elif self.var_sort.get() == 5:
            # 按物理成绩降序排列
            self.sort_result_student.sort(key=lambda x: x["physics"], reverse=True)
        elif self.var_sort.get() == 6:
            # 按化学成绩降序排列
            self.sort_result_student.sort(key=lambda x: x["chemistry"], reverse=True)
        elif self.var_sort.get() == 7:
            # 按生物成绩降序排列
            self.sort_result_student.sort(key=lambda x: x["biology"], reverse=True)
        elif self.var_sort.get() == 8:
            # 按历史成绩降序排列
            self.sort_result_student.sort(key=lambda x: x["history"], reverse=True)
        elif self.var_sort.get() == 9:
            # 按地理成绩降序排列
            self.sort_result_student.sort(key=lambda x: x["geography"], reverse=True)
        elif self.var_sort.get() == 10:
            # 按政治成绩降序排列
            self.sort_result_student.sort(key=lambda x: x["plitics"], reverse=True)
        else:
            # 显示左右学生的成绩信息,不做排列
            self.load_student_info_treeview(self.all_student_info)
        # 将排序结果显示在treeview中
        self.load_student_info_treeview(self.sort_result_student)

    def view_student_info(self, event):
        """
        查看学生成绩信息
        :return:
        """
        self.action_flag = 1
        # 获取双击行的数据
        item = self.Tree.selection()[0]
        temp_student_list = self.Tree.item(item, "values")
        for index in self.all_student_info:
            if index["id"] == temp_student_list[0]:
                self.current_student_info = index
        # 加载明细窗体
        self.load_detail_student_gui()

    def del_student_info(self):
        """
        删除学生信息
        :return:
        """
        try:
            # 获取双击行的数据
            item = self.Tree.selection()
            temp_student_list = self.Tree.item(item, "values")
            # 询问是否删除
            choose = askyesno("删除确认", "确认要删除学生信息【学号:" + temp_student_list[0] + "   姓名:" + temp_student_list[1]
                              + "】信息吗?")
            if choose:
                for index in range(len(self.all_student_info)):
                    if temp_student_list[0] == self.all_student_info[index]["id"]:
                        self.all_student_info.pop(index)
                        break
                # 更新数据表格显示
                self.load_student_info_treeview(self.all_student_info)
                # 将删除后的数据写入文件
                self.write_to_file()
                # 成功提示
                showinfo("消息提示", "消息删除成功!")
            else:
                return
        except:
            showinfo("消息提示", "请先选择要删除的学生信息!")

    def load_detail_student_gui(self):
        """
        加载学生信息明细窗口
        :return:
        """
        detail_windows = studetailgui.detail_windows(self.action_flag, self.current_student_info, self.all_student_info)
        self.wait_window(detail_windows)
        return detail_windows.userinfo

    def add_student_info(self):
        """
        录入学生信息
        :return:
        """
        self.action_flag = 2
        if self.load_detail_student_gui() == 1:
            # 将所有的信息显示在treeview中
            self.load_student_info_treeview(self.all_student_info)
            # 将学生信息写入文件
            self.write_to_file()
        else:
            return

    def upadte_student_info(self):
        """
        修改学生信息
        :return:
        """
        self.action_flag = 3
        # 获取双击行的数据
        try:
            item = self.Tree.selection()[0]
            temp_student_list = self.Tree.item(item, "values")
            for index in self.all_student_info:
                if index["id"] == temp_student_list[0]:
                    self.current_student_info = index
            # 加载窗体
            if self.load_detail_student_gui() == 1:
                self.load_student_info_treeview(self.all_student_info)
                self.write_to_file()
            else:
                return
        except:
            showinfo("消息提示", "请先选择要修改的学生!")

    def write_to_file(self):
        """
        将学生信息写入文件
        :param all_student_info:
        :return:
        """
        if os.path.exists(self.student_file):
            try:
                with open(file=self.student_file, mode="w", encoding="utf-8") as wfile:
                    for index in self.all_student_info:
                        wfile.write(str(index) + "\n")
            except:
                showinfo("消息提示", "文件写入错误!")

    def exit_windows(self):
        # 关闭住显示窗口
        self.destroy()

    def load_change_gui(self):
        """
        加载用户信息修改窗口
        :return:
        """
        change_windows = changegui.change_windows(self.change_flag, self.current_login_user_list)
        self.wait_window(change_windows)
        return change_windows.user_info

    def add_user_info(self):
        """
        添加用户信息
        :return:
        """
        self.change_flag = 2
        self.load_change_gui()

    def update_user_info(self):
        """
        修改用户信息
        :return:
        """
        self.change_flag = 1
        if self.load_change_gui() == 1:
            showinfo("消息提示", "用户密码已经修改,请重新登录!")
            self.destroy()

    def permission_upadte(self):
        if self.current_login_user_list[0]["user"] == "admin":
            self.change_flag = 3
            self.load_change_gui()
        else:
            showinfo("消息提示", "你还不是管理员,无权对用户权限进行更改!")
            return


if __name__ == "__main__":
    windows = Main_Windows()
    windows.mainloop()

7.studetail.py明细信息窗口功能的实现

from tkinter import *
from ttkbootstrap import *
from tkinter.messagebox import *


class detail_windows(Toplevel):
    def __init__(self, action_flag: int, current_student_info: dict, all_student_info: list):
        super().__init__()
        self.title("学生信息明细界面")
        self.geometry("400x400+400+300")
        self.resizable(0, 0)  # 不允许改变窗体大小
        self.iconbitmap("./img/student.ico")

        # 定义全局变量
        self.action_flag = action_flag
        self.current_student_info = current_student_info
        self.all_student_info = all_student_info

        # 加载显示界面
        self.setup_gui()
        self.load_maingui_flag()
        # 把窗体的行为捕获转为方法
        self.protocol("WM_DELETE_WINDOW", self.close_window)

    def setup_gui(self):
        # 给界面添加一个主题
        self.style01 = Style(theme="flatly")
        # 添加一个title
        self.var_title = StringVar()
        self.title_label = Label(self, textvariable=self.var_title, font="微软雅黑 16 bold", foreground="darkred",
                                 relief="groove")
        self.title_label.place(relwidth=0.99, relheight=0.15, relx=0.005, rely=0)
        # 添加一个区域显示窗体
        self.Pane = PanedWindow(self, relief="groove")
        self.Pane.place(relwidth=0.99, relheight=0.85, relx=0.005, rely=0.15)
        # 添加学号标签
        self.id_label = Label(self.Pane, text="学号:", font="微软雅黑 10 bold")
        self.id_label.place(relwidth=0.2, relheight=0.1, relx=0, rely=0.01)
        self.var_id = StringVar()
        self.id_Entry = Entry(self.Pane, textvariable=self.var_id, font="微软雅黑 10 bold")
        self.id_Entry.place(relwidth=0.3, relheight=0.1, relx=0.15, rely=0.01)
        # 添加一个姓名标签
        self.name_label = Label(self.Pane, text="姓名:", font="微软雅黑 10 bold")
        self.name_label.place(relwidth=0.2, relheight=0.1, relx=0.5, rely=0.01)
        self.var_name = StringVar()
        self.name_Entry = Entry(self.Pane, textvariable=self.var_name, font="微软雅黑 10 bold")
        self.name_Entry.place(relwidth=0.3, relheight=0.1, relx=0.65, rely=0.01)
        # 添加班级标签
        self.classes_label = Label(self.Pane, text="班级:", font="微软雅黑 10 bold")
        self.classes_label.place(relwidth=0.2, relheight=0.1, relx=0, rely=0.15)
        self.var_classes = StringVar()
        self.classes_Entry = Entry(self.Pane, textvariable=self.var_classes, font="微软雅黑 10 bold")
        self.classes_Entry.place(relwidth=0.3, relheight=0.1, relx=0.15, rely=0.15)
        # 添加一个语文标签
        self.chinese_label = Label(self.Pane, text="语文:", font="微软雅黑 10 bold")
        self.chinese_label.place(relwidth=0.2, relheight=0.1, relx=0.5, rely=0.15)
        self.var_chinese = StringVar()
        self.chinese_Entry = Entry(self.Pane, textvariable=self.var_chinese, font="微软雅黑 10 bold")
        self.chinese_Entry.place(relwidth=0.3, relheight=0.1, relx=0.65, rely=0.15)
        # 添加数学标签
        self.maths_label = Label(self.Pane, text="数学:", font="微软雅黑 10 bold")
        self.maths_label.place(relwidth=0.2, relheight=0.1, relx=0, rely=0.29)
        self.var_maths = StringVar()
        self.maths_Entry = Entry(self.Pane, textvariable=self.var_maths, font="微软雅黑 10 bold")
        self.maths_Entry.place(relwidth=0.3, relheight=0.1, relx=0.15, rely=0.29)
        # 添加一个英语标签
        self.english_label = Label(self.Pane, text="英语:", font="微软雅黑 10 bold")
        self.english_label.place(relwidth=0.2, relheight=0.1, relx=0.5, rely=0.29)
        self.var_english = StringVar()
        self.english_Entry = Entry(self.Pane, textvariable=self.var_english, font="微软雅黑 10 bold")
        self.english_Entry.place(relwidth=0.3, relheight=0.1, relx=0.65, rely=0.29)
        # 添加物理标签
        self.physics_label = Label(self.Pane, text="物理:", font="微软雅黑 10 bold")
        self.physics_label.place(relwidth=0.2, relheight=0.1, relx=0, rely=0.43)
        self.var_physics = StringVar()
        self.physics_Entry = Entry(self.Pane, textvariable=self.var_physics, font="微软雅黑 10 bold")
        self.physics_Entry.place(relwidth=0.3, relheight=0.1, relx=0.15, rely=0.43)
        # 添加一个化学标签
        self.chemistry_label = Label(self.Pane, text="化学:", font="微软雅黑 10 bold")
        self.chemistry_label.place(relwidth=0.2, relheight=0.1, relx=0.5, rely=0.43)
        self.var_chemistry = StringVar()
        self.chemistry_Entry = Entry(self.Pane, textvariable=self.var_chemistry, font="微软雅黑 10 bold")
        self.chemistry_Entry.place(relwidth=0.3, relheight=0.1, relx=0.65, rely=0.43)
        # 添加生物标签
        self.biology_label = Label(self.Pane, text="生物:", font="微软雅黑 10 bold")
        self.biology_label.place(relwidth=0.2, relheight=0.1, relx=0, rely=0.57)
        self.var_biology = StringVar()
        self.biology_Entry = Entry(self.Pane, textvariable=self.var_biology, font="微软雅黑 10 bold")
        self.biology_Entry.place(relwidth=0.3, relheight=0.1, relx=0.15, rely=0.57)
        # 添加一个历史标签
        self.history_label = Label(self.Pane, text="历史:", font="微软雅黑 10 bold")
        self.history_label.place(relwidth=0.2, relheight=0.1, relx=0.5, rely=0.57)
        self.var_history = StringVar()
        self.history_Entry = Entry(self.Pane, textvariable=self.var_history, font="微软雅黑 10 bold")
        self.history_Entry.place(relwidth=0.3, relheight=0.1, relx=0.65, rely=0.57)
        # 添加地理标签
        self.geography_label = Label(self.Pane, text="地理:", font="微软雅黑 10 bold")
        self.geography_label.place(relwidth=0.2, relheight=0.1, relx=0, rely=0.71)
        self.var_geography = StringVar()
        self.geography_Entry = Entry(self.Pane, textvariable=self.var_geography, font="微软雅黑 10 bold")
        self.geography_Entry.place(relwidth=0.3, relheight=0.1, relx=0.15, rely=0.71)
        # 添加一个政治标签
        self.plitics_label = Label(self.Pane, text="政治:", font="微软雅黑 10 bold")
        self.plitics_label.place(relwidth=0.2, relheight=0.1, relx=0.5, rely=0.71)
        self.var_plitics = StringVar()
        self.plitics_Entry = Entry(self.Pane, textvariable=self.var_plitics, font="微软雅黑 10 bold")
        self.plitics_Entry.place(relwidth=0.3, relheight=0.1, relx=0.65, rely=0.71)
        # 添加保存按钮
        self.save_button = Button(self, text="保存", font="微软雅黑 10 bold", command=self.save_commit)
        self.save_button.place(relwidth=0.2, relheight=0.1, relx=0.4, rely=0.87)
        # 添加关闭按钮
        self.close_button = Button(self, text="关闭", font="微软雅黑 10 bold", command=self.close_window)
        self.close_button.place(relwidth=0.2, relheight=0.1, relx=0.74, rely=0.87)

    def load_maingui_flag(self):
        if self.action_flag == 1:
            self.var_title.set("==查看学生明细信息==")
            # 加载数据
            self.load_student_detail()
            # 设置控件状态为不可改变
            self.save_button.forget()
            self.id_Entry["state"] = DISABLED
            self.name_Entry["state"] = DISABLED
            self.classes_Entry["state"] = DISABLED
            self.chinese_Entry["state"] = DISABLED
            self.maths_Entry["state"] = DISABLED
            self.english_Entry["state"] = DISABLED
            self.physics_Entry["state"] = DISABLED
            self.chemistry_Entry["state"] = DISABLED
            self.biology_Entry["state"] = DISABLED
            self.history_Entry["state"] = DISABLED
            self.geography_Entry["state"] = DISABLED
            self.plitics_Entry["state"] = DISABLED
        elif self.action_flag == 2:
            self.var_title.set("==添加学生明细信息==")
        elif self.action_flag == 3:
            self.var_title.set("==修改学生明细信息==")
            # 填充数据
            self.load_student_detail()
            # 学号不允许修改
            self.id_Entry["state"] = DISABLED

    def load_student_detail(self):
        if not self.current_student_info:
            showinfo("消息提示", "没有任何信息可以展示!")
        else:
            self.var_id.set(str(self.current_student_info["id"]))
            self.var_name.set(str(self.current_student_info["name"]))
            self.var_classes.set(str(self.current_student_info["classes"]))
            self.var_chinese.set(str(self.current_student_info["chinese"]))
            self.var_maths.set(str(self.current_student_info["maths"]))
            self.var_english.set(str(self.current_student_info["english"]))
            self.var_physics.set(str(self.current_student_info["physics"]))
            self.var_chemistry.set(str(self.current_student_info["chemistry"]))
            self.var_biology.set(str(self.current_student_info["biology"]))
            self.var_history.set(str(self.current_student_info["history"]))
            self.var_geography.set(str(self.current_student_info["geography"]))
            self.var_plitics.set(str(self.current_student_info["plitics"]))

    def save_commit(self):
        if self.action_flag == 1:
            pass
        elif self.action_flag == 2:
            # 准备数据
            temp_dic = dict()
            if len(str(self.var_id.get().strip())) == 0:
                showinfo("消息提示", "学号不能为空!")
            else:
                try:
                    temp_dic["id"] = str(self.var_id.get().strip())  # 获取学号
                    temp_dic["name"] = str(self.var_name.get().strip())  # 获取姓名
                    temp_dic["classes"] = str(self.var_classes.get().strip())  # 获取班级
                    temp_dic["chinese"] = float(self.var_chinese.get().strip())  # 获取语文
                    temp_dic["maths"] = float(self.var_maths.get().strip())  # 获取数学
                    temp_dic["english"] = float(self.var_english.get().strip())  # 获取英语成绩
                    temp_dic["physics"] = float(self.var_physics.get().strip())  # 获取物理成绩
                    temp_dic["chemistry"] = float(self.var_chemistry.get().strip())  # 获取化学
                    temp_dic["biology"] = float(self.var_biology.get().strip())  # 获取生物
                    temp_dic["history"] = float(self.var_history.get().strip())  # 获取历史成绩
                    temp_dic["geography"] = float(self.var_geography.get().strip())  # 获取地理成绩
                    temp_dic["plitics"] = float(self.var_plitics.get().strip())  # 获取政治成绩
                    temp_dic["total"] = temp_dic["chinese"] + temp_dic["maths"] + temp_dic["english"] + temp_dic[
                        "physics"] + temp_dic["chemistry"] + temp_dic["biology"] + temp_dic["history"] + temp_dic[
                                            "geography"] + temp_dic["plitics"]
                    for index in range(len(self.all_student_info)):
                        if temp_dic["id"] == self.all_student_info[index]["id"]:
                            showinfo("消息提示", "输入学生信息已经存在,请重新输入!")
                            # 将所有消息恢复为空
                            self.var_id.set("")
                            self.var_name.set("")
                            self.var_classes.set("")
                            self.var_chinese.set("")
                            self.var_maths.set("")
                            self.var_english.set("")
                            self.var_physics.set("")
                            self.var_chemistry.set("")
                            self.var_biology.set("")
                            self.var_history.set("")
                            self.var_geography.set("")
                            self.var_plitics.set("")
                            break
                        else:
                            self.all_student_info.append(temp_dic)
                            showinfo("消息提示", "学生信息添加成功!")
                            # 反馈信号给主窗体
                            self.userinfo = 1
                            # 关闭窗口
                            self.destroy()
                            break
                except:
                    showinfo("消息提示", "输入数据类型错误")
        elif self.action_flag == 3:
            # 准备数据
            temp_dic = dict()
            if len(str(self.var_id.get().strip())) == 0:
                showinfo("消息提示", "学号不能为空!")
            else:
                try:
                    temp_dic["id"] = str(self.var_id.get().strip())  # 获取学号
                    temp_dic["name"] = str(self.var_name.get().strip())  # 获取姓名
                    temp_dic["classes"] = str(self.var_classes.get().strip())  # 获取班级
                    temp_dic["chinese"] = float(self.var_chinese.get().strip())  # 获取语文
                    temp_dic["maths"] = float(self.var_maths.get().strip())  # 获取数学
                    temp_dic["english"] = float(self.var_english.get().strip())  # 获取英语成绩
                    temp_dic["physics"] = float(self.var_physics.get().strip())  # 获取物理成绩
                    temp_dic["chemistry"] = float(self.var_chemistry.get().strip())  # 获取化学
                    temp_dic["biology"] = float(self.var_biology.get().strip())  # 获取生物
                    temp_dic["history"] = float(self.var_history.get().strip())  # 获取历史成绩
                    temp_dic["geography"] = float(self.var_geography.get().strip())  # 获取地理成绩
                    temp_dic["plitics"] = float(self.var_plitics.get().strip())  # 获取政治成绩
                    temp_dic["total"] = temp_dic["chinese"] + temp_dic["maths"] + temp_dic["english"] + temp_dic[
                        "physics"] + temp_dic["chemistry"] + temp_dic["biology"] + temp_dic["history"] + temp_dic[
                                            "geography"] + temp_dic["plitics"]
                    # print(temp_dic)
                    # 遍历存储列表
                    for index in range(len(self.all_student_info)):
                        if temp_dic["id"] == self.all_student_info[index]["id"]:
                            # 删除原来的数据
                            self.all_student_info[index] = temp_dic
                            # 将新数据修改后的数据添加到列表
                            # self.all_student_info.append(temp_dic)
                    showinfo("消息提示", "学生信息修改成功!")
                    # 反馈信号给主窗体
                    self.userinfo = 1
                    # 关闭窗体
                    self.destroy()
                except:
                    showinfo("消息提示", "输入数据类型错误!")

    def close_window(self):
        self.userinfo = 0
        self.destroy()

8.changegui.py用户信息边界功能的实现

import os
from tkinter import *
from ttkbootstrap import *
from tkinter.messagebox import *


class change_windows(Toplevel):
    def __init__(self, change_flag: int, current_login_list: dict):
        super().__init__()
        self.title("用户信息窗口")
        self.geometry("300x300+400+300")
        self.resizable(0, 0)
        self.iconbitmap("./img/student.ico")

        # 定义全局变量
        self.change_flag = change_flag
        self.current_login_list = current_login_list  # 存储当前登录用户信息
        self.user_file = "./data/user.txt"  # 用户文件
        self.all_user_list = []  # 存储所有用户的信息

        # 加载函数
        self.setup_gui()
        self.load_maingui_flag()
        self.read_user_file()
        # 把窗体的行为捕获转为方法
        self.protocol("WM_DELETE_WINDOW", self.close_windows)

    def setup_gui(self):
        self.style01 = Style(theme="flatly")
        # 添加一个title
        self.var_title = StringVar()
        self.title_label = Label(self, textvariable=self.var_title, font="微软雅黑 16 bold", foreground="darkred",
                                 relief="groove")
        self.title_label.place(relwidth=0.99, relheight=0.15, relx=0.005, rely=0)
        # 添加一个区域显示窗体
        self.Pane = PanedWindow(self, relief="groove")
        self.Pane.place(relwidth=0.99, relheight=0.85, relx=0.005, rely=0.15)
        # 添加用户名标签
        self.user_label = Label(self.Pane, text="用户名:", font="微软雅黑 10 bold")
        self.user_label.place(relwidth=0.2, relheight=0.15, relx=0.03, rely=0.1)
        self.var_user = StringVar()
        self.user_Entry = Entry(self.Pane, textvariable=self.var_user, font="微软雅黑 10 bold")
        self.user_Entry.place(relwidth=0.7, relheight=0.15, relx=0.20, rely=0.1)
        # 添加密码标签
        self.password_label = Label(self.Pane, text="密码:", font="微软雅黑 10 bold")
        self.password_label.place(relwidth=0.2, relheight=0.15, relx=0.05, rely=0.4)
        self.var_password = StringVar()
        self.password_Entry = Entry(self.Pane, textvariable=self.var_password, font="微软雅黑 10 bold")
        self.password_Entry.place(relwidth=0.7, relheight=0.15, relx=0.20, rely=0.4)
        # 添加保存按钮
        self.save_button = Button(self, text="保存", font="微软雅黑 10 bold", command=self.save_commit)
        self.save_button.place(relwidth=0.2, relheight=0.1, relx=0.4, rely=0.87)

    def load_maingui_flag(self):
        if self.change_flag == 1:
            self.var_title.set("==用户密码修改==")
            self.user_Entry["state"] = DISABLED
            self.var_user.set(self.current_login_list[0]["user"])
        elif self.change_flag == 2:
            self.var_title.set("==添加用户信息==")
        elif self.change_flag == 3:
            self.var_title.set("==用户权限更改==")

    def save_commit(self):
        """
        保存用户信息修改
        :return:
        """
        if self.change_flag == 1:
            temp_dic = dict()
            try:
                temp_dic["user"] = str(self.var_user.get().strip())
                temp_dic["password"] = str(self.var_password.get().strip())
                temp_dic["times"] = "0"
                for index in range(len(self.all_user_list)):
                    if temp_dic["user"] == self.all_user_list[index]["user"]:
                        self.all_user_list[index] = temp_dic
                showinfo("消息提示", "密码修改成功!")
                # 将修改后的密码保存至文件中
                self.write_user_to_file()
                # 反馈信号给主窗体
                self.user_info = 1
                self.destroy()
            except:
                showinfo("消息提示", "输入数据类型错误")
        elif self.change_flag == 2:
            temp_dic = dict()
            if len(str(self.var_user.get().strip())) == 0:
                showinfo("消息提示", "用户名不能为空!")
            else:
                try:
                    temp_dic["user"] = str(self.var_user.get().strip())  # 获取用户名
                    temp_dic["password"] = str(self.var_password.get().strip())  # 获取密码
                    temp_dic["times"] = "0"
                    for index in range(len(self.all_user_list)):
                        if temp_dic["user"] == self.all_user_list[index]["user"]:
                            showinfo("消息提示", "用户名已经存在,请重新输入!")
                            self.var_user.set("")
                            self.var_password.set("")
                            break
                        else:
                            self.all_user_list.append(temp_dic)
                            showinfo("消息提示", "用户信息添加成功!")
                            self.user_info = 0
                            self.destroy()
                            break
                    self.write_user_to_file()
                except:
                    showinfo("消息提示", "输入数据类型错误!")
        elif self.change_flag == 3:
            temp_dic = dict()
            if len(str(self.var_user.get().strip())) == 0:
                showinfo("消息提示", "用户名不能为空!")
            else:
                try:
                    temp_dic["user"] = str(self.var_user.get().strip())  # 获取用户名
                    temp_dic["password"] = str(self.var_password.get().strip())  # 获取密码
                    temp_dic["times"] = "0"
                    for index in range(len(self.all_user_list)):
                        if temp_dic["user"] == self.all_user_list[index]["user"]:
                            self.all_user_list[index] = temp_dic
                    showinfo("消息提示", "用户权限修改成功!")
                    # 关闭窗口
                    self.destroy()
                    # 反馈信号给主窗体
                    self.user_info = 1
                    self.write_user_to_file()
                except:
                    showinfo("消息提示", "输入数据类型错误!")

    def close_windows(self):
        self.destroy()
        self.user_info = 0

    def read_user_file(self):
        if os.path.exists(self.user_file):
            try:
                with open(self.user_file, mode="r", encoding="utf-8") as rfile:
                    temp_user = rfile.readlines()
                    for list in temp_user:
                        temp_dic = dict(eval(list))
                        self.all_user_list.append(temp_dic)
            except:
                showinfo("消息提示", "用户信息文件读取错误!")

        else:
            showinfo("消息提示", "data中暂不存在保存的用户信息文件!")

    def write_user_to_file(self):
        if os.path.exists(self.user_file):
            try:
                with open(self.user_file, mode="w", encoding="utf-8") as wfile:
                    for index in self.all_user_list:
                        wfile.write(str(index) + "\n")
            except:
                showinfo("消息提示", "用户信息写入错误!")
        else:
            showinfo("消息提示", "用户信息写入错误!")

需要程序源码的请至https://download.csdn.net/download/qq_25327943/87862925进行下载。

你可能感兴趣的:(python,gui开发,python,pycharm,开发语言)