python数据可视化学生成绩管理系统

 

 大一python作业


import operator
import matplotlib.pyplot as plt
import easygui as g
import numpy as np

plt.rcParams['font.sans-serif'] = ['SimHei']  # 显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 这两行需要手动设置
stu_list = []  # 学生信息列表


def menu():
    choice_list = ['录入信息', '显示信息', '查询成绩', '修改成绩', '删除成绩', '排序成绩', '直方图', '画图2']
    opt = g.choicebox(msg='选择菜单', title="主菜单", choices=choice_list)
    if opt == "录入信息":
        stu_list.clear()  # 清 空 列 表
        input_score()  # 录 入 信 息  返 回 一 个 信 息 []   每 条 信 息 为 一 个{} == [{},{},{}]
        write_score()  # 不 覆 盖 写 入 文 件
        menu()
    if opt == "显示信息":
        read_file()  # 文 件 读 取
        show_inf()  # 显 示 信 息 成 绩 表
        menu()
    if opt == "查询成绩":
        querry_student()
        menu()
    if opt == "修改成绩":
        read_file()
        modify()
        write_score_2()
        menu()
    if opt == "删除成绩":
        read_file()
        del_l()
        write_score_2()
        menu()
    if opt == "排序成绩":
        sort_score()
        menu()
    if opt == "直方图":
        read_file()  # 文 件 读 取
        drow()
        menu()
    if opt == "画图2":
        drow_2()
        menu()


# 输入信息函数
def input_score():
    c = True
    while c:
        fields_list = ["姓名:", "学号", "性别", "语文", "数学", "英语"]
        stu = g.multenterbox(title="录入成绩", msg="录入成绩", fields=fields_list)
        stu_dict = {"name": stu[0], "id": stu[1], "sex": stu[2], "yw": stu[3], "sx": stu[4], "yy": stu[5]}
        if len(stu[0]) * len(stu[1]) * len(stu[2]) * len(stu[3]) * len(stu[4]) * len(stu[5]) == 0:
            g.msgbox(msg="信息不完整", title="信息错误")
            input_score()
        else:
            try:
                stu_dict = {"name": stu[0], "id": stu[1], "sex": stu[2], "yw": stu[3], "sx": stu[4], "yy": stu[5]}
                print(stu[0])
            except:
                g.msgbox(msg="数字错误", title="错误")
                input_score()
        # 选择是否继续录入信息
        c = g.ccbox("是否继续", "通知", default_choice="是", cancel_choice="否")
        # 存入每一条信息
        stu_list.append(stu_dict)
        # 这 里 注 意 缩 进
    return stu_list


# 成绩单
def show_inf():
    info_file = open("info.txt", "r", encoding="utf-8")
    #  使用 textbox 直接显示已保存文件
    g.textbox(msg='姓名    语文    数学    英语', title='成绩单', text=info_file.read())


# 选择读取信息文件
def read_file():
    stu_list.clear()
    # file_name = g.fileopenbox(msg="选择读取文件", title="打开文件")
    # a = open(file_name, "r", encoding="utf-8")
    # global stu_dict  # 全局作用域
    a = open("info.txt", "r", encoding='utf-8')
    stu_data = a.readlines()
    for stu in stu_data:
        stu = stu.strip('\n').split('        ')
        stu_dict = {"name": stu[0], "id": stu[1], "sex": stu[2], "yw": stu[3], "sx": stu[4], "yy": stu[5]}
        stu_list.append(stu_dict)


# 将字典数组[{},{}]存入文件
def write_score():
    # global f  # 全局作用域
    # if i == 1:
    f = open("info.txt", "a", encoding="utf-8")  # a 只能写 文件不存在时创建 不覆盖追加写
    # f.write("\n") # 解 决 插 入 数 据 不 换 行 问 题
    # elif i == 2:
    #     print(stu_list)
    #     print("2")
    #     f = open("info.txt", "w", encoding="utf-8")  # w 只能写  文件不存在时创建  覆盖
    for stu in stu_list:
        stu_save = list(stu.values())
        f.write("        ".join(stu_save))
        f.write("\n")


def write_score_2():
    f = open("info.txt", "w", encoding="utf-8")  # w 只能写  文件不存在时创建  覆盖
    for stu in stu_list:
        stusave = list(stu.values())
        f.write("        ".join(stusave))
        f.write("\n")


def drow():
    list_1 = []  # 语 文 成 绩
    list_2 = []  # 数 学 成 绩
    list_3 = []  # 英 语 成 绩
    global stusave, width, position  # 全局作用域
    # 遍 历 文 件 中 每 一 条 信 息   遍 历 出 的 stu 为 字 典
    for stu in stu_list:
        stusave = list(stu.values())
        # 将 成 绩 转 化 为 int 存 入 列 表
        list_1.append(int(stusave[3]))
        list_2.append(int(stusave[4]))
        list_3.append(int(stusave[5]))

        position = np.arange(1, len(stu_list) + 1)  # 生 成 一 个 数 组 长 度 为 1 到 len(stu_list)+1 --->就 是 人 数
        print(position)

        total_width = 0.4
        n = 2
        width = total_width / n
        # position = position - (total_width - width) / n

    plt.xlabel('这个是行属性字符串')
    plt.ylabel('score')
    plt.title('This is a headline')
    first_bar = plt.bar(position, list_1, width=width, color='b')
    second_bar = plt.bar(position + width, list_2, width=width, color='r')
    third_bar = plt.bar(position + width * 2, list_3, width=width, color='g')

    for data in first_bar:
        y = data.get_height()
        x = data.get_x()
        plt.text(x + 0.06, y, str(y), va='bottom')  # 0.06为偏移值,可以自己调整,正好在柱形图顶部正中
    for data in second_bar:
        y = data.get_height()
        x = data.get_x()
        plt.text(x + 0.06, y, str(y), va='bottom')
    for data in third_bar:
        y = data.get_height()
        x = data.get_x()
        plt.text(x + 0.06, y, str(y), va='bottom')

    plt.show()


# 查 询 学 生 信 息
def querry_student():
    global temp  # 全局作用域
    name = g.enterbox("输入想查询的学生姓名:")
    i = 0
    for temp in stu_list:
        if temp['name'] == name:
            i = 1
            break
        else:
            pass
    if i == 0:
        g.msgbox("未查询到此学生,请重新输入")
    else:
        g.textbox(msg='姓名\t学号\t语文\t数学\t英语', title='成绩单', text=(temp['name'] + '\t', temp['id'] + '\t'
                                                               , temp['yw'] + '\t', temp['sx'] + '\t', temp['yy']))


def del_l():
    name = g.enterbox("请输入想删除的姓名或学号")
    i = 0
    l = 0
    for temp in stu_list:
        if temp['name'] == name:
            i = 1
            break
        elif temp['id'] == name:
            i = 1
            break
        else:
            l = l + 1
    if i == 0:
        print("未查询到此学生")
    else:
        del stu_list[l]
        g.msgbox(msg="删除成功", title="信息删除", ok_button="确认")
    write_score_2()


def modify():
    id = g.enterbox("请输入想修改的学号")
    i = 0
    l = 0
    for temp in stu_list:
        if temp['id'] == id:
            i = 1
            break
        else:
            l = l + 1
    if i == 0:
        print("未找到此学生")
    else:
        while True:
            a_list = ['姓名', '语文', '数学', '英语', '退出修改']
            mod = g.choicebox(msg='选择修改项', title="修改项", choices=a_list)

            if mod == "姓名":
                newname = g.enterbox("请输入修改后的姓名")
                temp['name'] = newname
                print(g.msgbox(msg="修改成功", title="姓名修改", ok_button="确认"))

            if mod == "语文":
                newyuwen = g.enterbox("请输入修改后的语文成绩")
                temp['yw'] = newyuwen
                print(g.msgbox(msg="修改成功", title="语文修改", ok_button="确认"))

            if mod == "数学":
                newshuxue = g.enterbox("请输入修改后的数学成绩")
                temp['sx'] = newshuxue
                print(g.msgbox(msg="修改成功", title="数学修改", ok_button="确认"))
            if mod == "英语":
                newyingyu = g.enterbox("请输入修改后的英语成绩")
                temp['yy'] = newyingyu
                print(g.msgbox(msg="修改成功", title="英语修改", ok_button="确认"))

            if mod == "退出修改":
                break


#  排序
def sort_score():
    a_list = ['语文成绩排序', '数学成绩排序', '英语成绩排序', '退出排序']
    mod = g.choicebox(msg='选择排序项', title="排序项", choices=a_list)
    if mod == "语文成绩排序":
        sort_list = demo("yw")
        sort_yw = sorted(sort_list, key=operator.itemgetter('语文'))
        print(sort_yw)
        g.choicebox(msg="语文排序", title="1", choices=sort_yw)
    if mod == "数学成绩排序":
        sort_list = demo("sx")
        sort_sx = sorted(sort_list, key=operator.itemgetter('数学'))
        print(sort_sx)
        g.choicebox(msg="数学排序", title="2", choices=sort_sx)
    if mod == "英语成绩排序":
        sort_list = demo("yy")
        sort_yy = sorted(sort_list, key=operator.itemgetter('英语'))
        print(sort_yy)
        g.choicebox(msg="英语排序", title="3", choices=sort_yy)
    if mod == "退出修改":
        return


#  排序demo
def demo(ob):
    global ob2
    if ob == "yw":
        ob2 = "语文"
    elif ob == "sx":
        ob2 = "数学"
    elif ob == "yy":
        ob2 = "英语"
    sort_list = []
    sort_dick = {}
    for stu_data in stu_list:
        for k, v in stu_data.items():  # 提取键值对(k v)
            if k == ob:
                sort_dick = {"姓名": stu_data["name"], ob2: v}
                sort_list.append(sort_dick)
    return sort_list


# 图表2
def drow_2():
    yuwen_score = []
    shuxue_score = []
    yingyu_score = []
    text = []
    all_text = ["语文", "数学", "英语"]
    all_score = []
    for stu_data in stu_list:
        yuwen_score.append(int(stu_data["yw"]))
        shuxue_score.append(int(stu_data["sx"]))
        yingyu_score.append(int(stu_data["yy"]))
        text.append(int(stu_data["id"]))

    all_score.append(sum(yuwen_score))
    all_score.append(sum(shuxue_score))
    all_score.append(sum(yingyu_score))
    print(all_score)

    plt.subplot(2, 2, 1)
    plt.pie(yuwen_score, labels=text, autopct="%.2f%%")
    plt.title("语文")
    plt.legend(text, loc=2)

    plt.subplot(2, 2, 2)
    plt.pie(shuxue_score, labels=text, autopct="%.2f%%")
    plt.title("数学")
    plt.legend(text, loc=2)

    plt.subplot(2, 2, 3)
    plt.pie(yingyu_score, labels=text, autopct="%.2f%%")
    plt.title("英语")
    plt.legend(text, loc=2)

    plt.subplot(2, 2, 4)
    plt.pie(all_score, labels=all_text, autopct="%.2f%%")
    plt.title("各科成绩占比")
    plt.legend(text, loc=2)

    plt.suptitle("成绩统计表")
    plt.show()


if __name__ == '__main__':
    menu()

  • 5
    点赞
  • 63
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jcen_Leung

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值