用Python制作的学生积分管理系统(二)

# -*-coding:utf-8-*-

import threading
from tkinter import *
import shelve
from tkinter import messagebox
import xlrd,xlwt
from tkinter.ttk import *
from tkinter.filedialog import askopenfilename, asksaveasfilename
from tkinter.simpledialog import askstring
from datetime import date
import time
import math
import re
import random
import os


# s打开数据文件(“stu”:存学生信息;"JFinfo":存加减分信息;“CZinfo”:操作记录)
def openDataFile(xb):
    f = shelve.open("datafile")
    tempdata = f.get(xb, {})
    tempmima = f.get("mima", ["admin", "123456"])
    addFenJL = f.get("fjl", {})  #[日期,编号,姓名,班级,分值,加分理由]
    text1="学生编号:系统内的唯一身份号。\n姓名:学生本人的姓名。\n班级:学生在校班级。\n积分:完成学校所给的任务后所得积分累加值,可兑换奖励。\n星级:积分值越大,星级越高。\n称号:完成学校的某项特殊任务时获得,拥有称号的学生对应的成长值就高。\n成长值:学生每天积分会自动按成长值增加。"
    gongGao = f.get("gg", [text1,""])
    f.close()
    return [tempdata, tempmima,addFenJL,gongGao]
# 将数据保存到文件
def saveData(xb, datavar):
    f = shelve.open("datafile")
    f[xb] = datavar
    f.close()

#窗口居中
def win_center(win, w, h):
    s_w = win.winfo_screenwidth()
    s_h = win.winfo_screenheight()
    size = f"{w}x{h}+{int((s_w-w)/2)}+{int((s_h-h)/2)}"
    win.geometry(size)



# 成长值增加积分,每天一次
def add_fen(stuData):
    if len(stuData) > 0:
        today1 = str(date.today().isoformat())
        for i in stuData.keys():
            if today1 != stuData[i][8]:
                stuData[i][3] = int(stuData[i][3]) + int(stuData[i][6])
                import math
                stuData[i][4] = "★" * int(math.log(int(stuData[i][3]), 3))
                stuData[i][8] = today1
        saveData("stu", stuData)
# 主窗口
class mainWin():
    def __init__(self, title, stuData, mima, fenJL, gg):
        self.title = title
        self.w = 800
        self.h = 600
        self.flags = [0]
        self.stuData = stuData
        self.mima = mima
        self.mima_dl = False
        self.fenJL = fenJL
        self.gg = gg
        self.tempData = list(self.stuData.values())
        self.root = Tk()
        self.root.title(self.title)
        win_center(self.root, self.w, self.h)
        self.labelx = StringVar()  # 标签变量
        self.addCD()  # 添加菜单
        self.addZTL()  # 添加状态栏
        self.addDataFrame()
        self.root.mainloop()


    # 添加菜单
    def addCD(self):
        menubar = Menu(self.root)
        imdata = Menu(menubar)  # 添加数据导入菜单
        menubar.add_cascade(label="数据导入导出", menu=imdata)
        imdata.add_command(label="学生基本信息导入", command=self.dr_data)
        imdata.add_command(label="学生基本信息导出", command=self.dc_data)
        imdata.add_command(label="学生积分记录导出", command=self.JFJL_dc)
        find_data = Menu(menubar)  # 查询菜单
        menubar.add_cascade(label="数据查询", menu=find_data)
        find_data.add_command(label="编号查询", command=self.bh_find)
        find_data.add_command(label="姓名查询", command=self.xm_find)
        find_data.add_command(label="班级查询", command=self.bj_find)
        find_data.add_command(label="在校状态查询", command=self.zx_find)
        find_data.add_command(label="所有学生", command=self.all_stu)
        cz_data = Menu(menubar)  # 操作菜单
        menubar.add_cascade(label="数据操作", menu=cz_data)
        cz_data.add_command(label="查询结果加分", command=self.data_jf)
        cz_data.add_command(label="查询结果修改", command=self.data_xg)
        cz_data.add_command(label="查询结果删除", command=self.data_del)
        cz_data.add_command(label="添加数据", command=self.data_add)
        cz_data.add_command(label="发布公告一", command=self.rw_gg1)
        cz_data.add_command(label="发布公告二", command=self.rw_gg2)
        mima_data = Menu(menubar)  # 密码管理
        menubar.add_cascade(label="密码管理", menu=mima_data)
        mima_data.add_command(label="密码修改", command=self.mima_xg)
        mima_data.add_command(label="密码登录",command=self.mima_denglu)
        mima_data.add_command(label="密码退出",command=self.mima_tui)
        # 信息展示
        visibel_win = Menu(menubar)
        menubar.add_cascade(label="信息展示", menu=visibel_win)
        visibel_win.add_command(label="显示展台", command=self.visibel_ZT)
        self.root.config(menu=menubar)

    # 显示展台
    def visibel_ZT(self):
        if not self.flags[0]:
            self.thread1 = threading.Thread(target=zt_visible, args=(self.stuData, self.fenJL, self.gg,self.flags))
            self.thread1.setDaemon(True)
            self.thread1.start()
            self.flags[0] = 1



    # 积分记录导出
    def JFJL_dc(self):
        if not (askstring("请输入密码", "密码", show="*") == self.mima[1]):
            messagebox.showerror("错误", "密码不正确")
            return
        fname = asksaveasfilename(title="文件另存为", filetypes=[("Microsoft Excel 97-2003 文件", "*.xls")])

        if fname:
            while os.path.isfile(fname+".xls"):
                self.lab1["text"] = "文件名重复,请重新命名!"
                self.lab1["foreground"] = random.choice(["red", "green", "blue", "deeppink", "indigo"])
                fname = asksaveasfilename(title="文件另存为", filetypes=[("Microsoft Excel 97-2003 文件", "*.xls")])

            # [日期,编号,姓名,班级,分值,加分理由]
            workbook = xlwt.Workbook(encoding="utf-8")
            worksheet = workbook.add_sheet("Sheet1")
            worksheet.write(0, 0, "日期")
            worksheet.write(0, 1, "编号")
            worksheet.write(0, 2, "姓名")
            worksheet.write(0, 3, "增加积分")
            worksheet.write(0, 4, "加分理由")
            temdata = []
            for val in self.fenJL.values():
                temdata.extend(val)
            for i in range(len(temdata)):
                for j in range(5):
                    worksheet.write(i+1, j, temdata[i][j])

            workbook.save(fname + ".xls")
            self.lab1["text"] = "学生积分记录导出完成!"
            self.lab1["foreground"] = random.choice(["red", "green", "blue", "deeppink", "indigo"])




    # 添加数据
    def data_add(self):
        if self.mima_dl:
            pass
        elif not (askstring("请输入密码", "密码", show="*") == self.mima[1]):
            messagebox.showerror("错误", "密码不正确")
            return
        d = add_data_win(self.stuData, self.lab1)
        self.root.wait_window(d)
        if d.xsbh == "":
            self.lab1["text"] = "添加数据失败!"
            self.lab1["foreground"] = random.choice(["red", "green", "blue", "deeppink", "indigo"])
            return
        tempdata = [d.xsbh, d.xsxm, d.xsbj, d.xsjf, d.xsxj, d.xsch, d.xscz, d.xszx, date.today().isoformat()]
        self.stuData[d.xsbh] = tempdata
        saveData("stu", self.stuData)
        self.tempData = list(self.stuData.values())
        self.all_stu()
        self.lab1["text"] = "添加数据成功!"
        self.lab1["foreground"] = random.choice(["red", "green", "blue", "deeppink", "indigo"])



    # 密码登录
    def mima_tui(self):
        self.mima_dl = False
        messagebox.showinfo("成功", "密码退出成功。")
    #密码登录
    def mima_denglu(self):
        if not (askstring("请输入密码", "密码", show="*") == self.mima[1]):
            messagebox.showerror("错误", "密码不正确")
        else:
            self.mima_dl = True
            messagebox.showinfo("成功","密码登录成功,其他操作无需再输入密码。")
    # 密码修改
    def mima_xg(self):
        oldmi = askstring("密码", "请输入旧密码", show="*")
        if not oldmi:
            pass
        elif oldmi == self.mima[1]:
            newmi = askstring("密码", "请输入新密码", show="*")
            if not newmi:
                messagebox.showwarning("警告", "新密码不能为空!")
            elif len(newmi) < 6:
                messagebox.showwarning("警告", "密码长度不能小于6")
            else:
                self.mima[1] = newmi
                saveData("mima", self.mima)
                messagebox.showinfo("成功", "密码修改成功!")
    # 添加状态栏lab1(变量self.labelx)
    def addZTL(self):

        self.lab1 = Label(self.root, text="欢迎使用学生积分管理系统。 作者:周坤 联系电话:13991045332", relief="groove")
        self.lab1.pack(side=BOTTOM, padx=5, pady=1, fill=X)

    # 添加数据框架tree
    def addDataFrame(self):
        fm1 = Frame(self.root, relief=GROOVE, borderwidth=5)
        fm1.pack(side=BOTTOM, padx=5, pady=1, fill=BOTH, expand=True)

        # v建立TreeView
        col = ("bh", "xm", "bj", "jf", "xj", "ch", "cz", "zx", "rq")
        self.tree = Treeview(fm1, columns=col, show="headings")
        # 标题
        self.tree.heading("bh", text="学生编号")
        self.tree.heading("xm", text="姓名")
        self.tree.heading("bj", text="班级")
        self.tree.heading("jf", text="积分")
        self.tree.heading("xj", text="星级")
        self.tree.heading("ch", text="称号")
        self.tree.heading("cz", text="成长值")
        self.tree.heading("zx", text="在校状态")
        self.tree.heading("rq", text="日期")
        for var1 in col:
            self.tree.column(var1, width=87, anchor=CENTER)
        self.treeDate_add()
        yscroll = Scrollbar(fm1)
        yscroll.pack(side=RIGHT, fill=Y)
        self.tree.pack(side=RIGHT, fill=BOTH, expand=True)
        yscroll.config(command=self.tree.yview)
        self.tree.configure(yscrollcommand=yscroll.set)

    # 表格中重新添加数据,同时self.tempData重建数据
    def treeDate_add(self):
        for v in self.tempData:
           self.tree.insert("", index=END, text=v[0], values=v)
    # 清空表中数据
    def clear_treedata(self):
        treedata = self.tree.get_children()
        for item in treedata:
            self.tree.delete(item)
    # 删除文件中数据
    def del_filedata(self):
        if len(self.tempData):
            for temp in self.tempData:
                self.stuData.pop(temp[0])
        self.tempData = []
        saveData("stu", self.stuData)
    # 导入数据
    def dr_data(self):
        if not (askstring("谨慎操作", "将覆盖系统内数据,谨慎!\n请输入密码", show="*") == self.mima[1]):
            messagebox.showerror("错误", "密码不正确")
            return
        fname = askopenfilename(title="请选择要导入的文件", filetypes=[("Microsoft Excel 97-2003 文件", "*.xls")])
        if fname:
            try:
                workbook = xlrd.open_workbook(fname)
                datavalue = workbook.sheet_by_name(u"Sheet1")
                rows = datavalue.nrows
                for i in range(1,rows):
                    temp = datavalue.row_values(i)
                    temp[3] = int(temp[3])
                    temp[6] = int(temp[6])
                    temp.append(date.today().isoformat())
                    self.stuData[temp[0]] = temp
                saveData("stu", self.stuData)
                self.all_stu()
                self.lab1["text"] = f"成功更新{rows-1}条数据。"
                self.lab1["foreground"] = random.choice(["red", "green", "blue", "deeppink", "indigo"])
            except Exception as err:
                self.lab1["text"] = "数据格式有误。"
                self.lab1["foreground"] = random.choice(["red", "green", "blue", "deeppink", "indigo"])



    # 导出数据
    def dc_data(self):
        if not (askstring("请输入密码", "密码", show="*") == self.mima[1]):
            messagebox.showerror("错误", "密码不正确")
            return
        fname = asksaveasfilename(title="文件另存为", filetypes=[("Microsoft Excel 97-2003 文件", "*.xls")])
        if fname:
            while os.path.isfile(fname + ".xls"):
                self.lab1["text"] = "文件名重复,请重新命名!"
                self.lab1["foreground"] = random.choice(["red", "green", "blue", "deeppink", "indigo"])
                fname = asksaveasfilename(title="文件另存为", filetypes=[("Microsoft Excel 97-2003 文件", "*.xls")])
            workbook = xlwt.Workbook(encoding="utf-8")
            worksheet = workbook.add_sheet("Sheet1")
            worksheet.write(0, 0, "学生编号")
            worksheet.write(0, 1, "姓名")
            worksheet.write(0, 2, "班级")
            worksheet.write(0, 3, "积分")
            worksheet.write(0, 4, "星级")
            worksheet.write(0, 5, "称号")
            worksheet.write(0, 6, "成长值")
            worksheet.write(0, 7, "在校状态")
            worksheet.write(0, 8, "导出日期")
            for i in range(len(self.tempData)):
                for j in range(9):
                    worksheet.write(i + 1, j, str(self.tempData[i][j]))
            workbook.save(fname + ".xls")
            self.lab1["text"] = "数据导出成功!"
            self.lab1["foreground"] = random.choice(["red", "green", "blue", "deeppink", "indigo"])


    # 编号查询
    def bh_find(self):
        bh = askstring("编号查询","请输入学生编号")
        if bh:
            self.clear_treedata()
            self.tempData = []
            for tempdata in self.stuData.values():
                if bh in tempdata[0]:
                    self.tempData.append(tempdata)
            self.treeDate_add()
            self.lab1["text"] =f"查找到{len(self.tempData)}条数据!"
            self.lab1["foreground"] = random.choice(["red", "green", "blue", "deeppink", "indigo"])
                    
   # 姓名查询
    def xm_find(self):
        xm = askstring("姓名查询", "请输入学生姓名")
        if xm:
            self.clear_treedata()
            self.tempData = []
            for tempdata in self.stuData.values():
                if xm in tempdata[1]:
                    self.tempData.append(tempdata)
            self.treeDate_add()
            self.lab1["text"] = f"查找到{len(self.tempData)}条数据!"
            self.lab1["foreground"] = random.choice(["red", "green", "blue", "deeppink", "indigo"])


    # 班级查询
    def bj_find(self):
        bj = askstring("班级查询", "请输入学生班级")
        if bj:
            self.clear_treedata()
            self.tempData = []
            for tempdata in self.stuData.values():
                if bj in str(tempdata[2]):
                    self.tempData.append(tempdata)
            self.treeDate_add()
            self.lab1["text"] = f"查找到{len(self.tempData)}条数据!"
            self.lab1["foreground"] = random.choice(["red", "green", "blue", "deeppink", "indigo"])
    # 在校状态查询
    def zx_find(self):
        zx = askstring("在校状态查询", "请输入学生在校状态")
        if zx:
            self.clear_treedata()
            self.tempData = []
            for tempdata in self.stuData.values():
                if zx in tempdata[7]:
                    self.tempData.append(tempdata)
            self.treeDate_add()
            self.lab1["text"] = f"查找到{len(self.tempData)}条数据!"
            self.lab1["foreground"] = random.choice(["red", "green", "blue", "deeppink", "indigo"])
    # 显示所有学生
    def all_stu(self):
        self.clear_treedata()
        self.tempData = list(self.stuData.values())
        self.treeDate_add()
        self.lab1["text"] = f"查找到{len(self.tempData)}条数据!"
        self.lab1["foreground"] = random.choice(["red", "green", "blue", "deeppink", "indigo"])

    # 查询结果加分
    def data_jf(self):
        if self.mima_dl:
            pass
        elif not (askstring("请输入密码", "密码", show="*") == self.mima[1]):
            messagebox.showerror("错误", "密码不正确")
            return
        self.topwin = topwin(self.lab1)
        self.root.wait_window(self.topwin)
        if not self.topwin.fen:
            return
        for temps in self.tempData:
            temps[3] += self.topwin.fen
            temps[4] = "★" * int(math.log(temps[3],3))
            self.stuData[temps[0]] = temps
            if temps[0] in self.fenJL.keys():
                self.fenJL[temps[0]].append([temps[8], temps[0], temps[1], self.topwin.fen, self.topwin.cause])
            else:
                self.fenJL[temps[0]] = [[temps[8], temps[0], temps[1], self.topwin.fen, self.topwin.cause]]
        saveData("stu", self.stuData)
        saveData("fjl", self.fenJL)
        self.clear_treedata()
        self.treeDate_add()
        self.lab1["text"] = "加分成功!"
        self.lab1["foreground"] = random.choice(["red", "green", "blue", "deeppink", "indigo"])

    # 查询结果修改
    def data_xg(self):
        if self.mima_dl:
            pass
        elif not (askstring("请输入密码", "密码", show="*") == self.mima[1]):
            messagebox.showerror("错误", "密码不正确")
            return
        xgwin = dataXG_win(self.tempData, self.stuData, self.lab1)
        self.root.wait_window(xgwin)
        self.clear_treedata()
        self.treeDate_add()



    # 查询结果删除
    def data_del(self):
        if self.mima_dl:
            pass
        elif not (askstring("请输入密码", "密码", show="*") == self.mima[1]):
            messagebox.showerror("错误", "密码不正确")
            return
        if messagebox.askquestion("谨慎操作", "确实要删除吗?") == "yes":
            self.clear_treedata()
            self.del_filedata()
            self.lab1["text"] = "数据删除成功!"
            self.lab1["foreground"] = random.choice(["red", "green", "blue", "deeppink", "indigo"])

    # 发布任务公告
    def rw_gg1(self):
        if self.mima_dl:
            pass
        elif not (askstring("请输入密码", "密码", show="*") == self.mima[1]):
            messagebox.showerror("错误", "密码不正确")
            return
        gg_win(self.gg, 0, self.lab1)


    def rw_gg2(self):
        if self.mima_dl:
            pass
        elif not (askstring("请输入密码", "密码", show="*") == self.mima[1]):
            messagebox.showerror("错误", "密码不正确")
            return
        gg_win(self.gg, 1, self.lab1)

#发布公告窗口
class gg_win(Toplevel):
    def __init__(self, gg, i, labelx):
        super().__init__()
        self.gg = gg
        self.i = i
        self.lab1 = labelx
        self.title = "公告编辑"
        self.resizable(0, 0)
        self.wm_attributes("-topmost", -1)
        self.setUI()
        win_center(self, 350, 340)

    def setUI(self):
        self.frame1 = LabelFrame(self, text="公告编辑", labelanchor="n")
        self.frame1.pack(padx=3, pady=3)
        self.text1 = Text(self.frame1, padx=3, pady=3, height=20, width=45)
        self.text1.grid(row=0, column=0, rowspan=5, columnspan=2, padx=5, pady=5)
        self.bt1 = Button(self.frame1, text="发布", command=self.bt1_fabu)
        self.bt2 = Button(self.frame1, text="取消", command=self.bt2_fabu)
        self.bt1.grid(row=5, column=0)
        self.bt2.grid(row=5, column=1)
    def bt1_fabu(self):
        txt = self.text1.get("1.0", "end")
        self.gg[self.i] = txt
        saveData("gg", self.gg)
        self.lab1["text"] = f"公告{self.i+1}发布成功!"
        self.lab1["foreground"] = random.choice(["red", "green", "blue", "deeppink", "indigo"])

        self.destroy()
    def bt2_fabu(self):
        self.lab1["text"] = "已取消发布公告!"
        self.lab1["foreground"] = random.choice(["red", "green", "blue", "deeppink", "indigo"])
        self.destroy()



# 数据修改窗口
class dataXG_win(Toplevel):
    def __init__(self, tempdata, studata, labelx):
        super().__init__()
        self.tempdata = tempdata
        self.studata = studata
        self.lab1 = labelx
        self.data_len = len(tempdata)
        self.data_sum = 0
        self.title = "修改学生信息"
        self.resizable(0, 0)  # 设置窗口不可改变大小
        self.wm_attributes('-topmost', 1)  # 设置窗口置顶
        self.setUI()
        win_center(self, 250, 220)
        self.xg_data(self.tempdata[self.data_sum])

    def setUI(self):  # 界面设置
        self.frame1 = LabelFrame(self, text="学生信息", labelanchor="n")
        self.frame1.pack(padx=3, pady=3)
        self.bh_label = Label(self.frame1, text="学生编号:", background="gainsboro")
        self.bh_label.grid(row=0, column=0, padx=2, pady=2, sticky=W + E)
        self.bh_en = Entry(self.frame1)
        self.bh_en.grid(row=0, column=1, padx=2, pady=2, sticky=W + E)
        self.xm_label = Label(self.frame1, text="学生姓名:", background="gainsboro")
        self.xm_label.grid(row=1, column=0, padx=2, pady=2, sticky=W + E)
        self.xm_en = Entry(self.frame1)
        self.xm_en.grid(row=1, column=1, padx=2, pady=2, sticky=W + E)
        self.bj_label = Label(self.frame1, text="学生班级:", background="gainsboro")
        self.bj_label.grid(row=2, column=0, padx=2, pady=2, sticky=W + E)
        self.bj_en = Entry(self.frame1)
        self.bj_en.grid(row=2, column=1, padx=2, pady=2, sticky=W + E)
        self.jf_label = Label(self.frame1, text="学生积分:", background="gainsboro")
        self.jf_label.grid(row=3, column=0, padx=2, pady=2, sticky=W + E)
        self.jf_en = Entry(self.frame1)
        self.jf_en.grid(row=3, column=1, padx=2, pady=2, sticky=W + E)
        self.ch_label = Label(self.frame1, text="学生称号:", background="gainsboro")
        self.ch_label.grid(row=4, column=0, padx=2, pady=2, sticky=W + E)
        self.ch_en = Entry(self.frame1)
        self.ch_en.grid(row=4, column=1, padx=2, pady=2, sticky=W + E)
        self.cz_label = Label(self.frame1, text="成长值:", background="gainsboro")
        self.cz_label.grid(row=5, column=0, padx=2, pady=2, sticky=W + E)
        self.cz_en = Entry(self.frame1)
        self.cz_en.grid(row=5, column=1, padx=2, pady=2, sticky=W + E)
        self.ok_btn = Button(self.frame1, text="修改", command=self.bt1ok)
        self.ok_btn.grid(row=6, column=0, padx=2, pady=2)
        self.no_btn = Button(self.frame1, text="下一个", command=self.bt2no)
        self.no_btn.grid(row=6, column=1, padx=2, pady=2)


    def xg_data(self, data1):
        self.bh_en.config(state = NORMAL)
        self.bh_en.delete(0, END)
        self.bh_en.insert(0, data1[0])
        self.xm_en.delete(0, END)
        self.xm_en.insert(0, data1[1])
        self.bj_en.delete(0, END)
        self.bj_en.insert(0, data1[2])
        self.jf_en.delete(0, END)
        self.jf_en.insert(0, data1[3])
        self.ch_en.delete(0, END)
        self.ch_en.insert(0, data1[5])
        self.cz_en.delete(0, END)
        self.cz_en.insert(0, data1[6])
        self.bh_en.config(state = DISABLED)


    def bt1ok(self):
        xsbh = self.bh_en.get()
        xsxm = self.xm_en.get()
        xsbj = self.bj_en.get()
        xsjf = self.jf_en.get()
        xsxj = ""
        xsch = self.ch_en.get()
        xscz = self.cz_en.get()
        xszx = "在校"
        if xsbh.strip() == "" or xsxm.strip() == "" or xsbj.strip() == "":
            self.lab1["text"] = "警告:学生编号、班级、姓名均不能为空!"
            self.lab1["foreground"] = random.choice(["red", "green", "blue", "deeppink", "indigo"])
            return

        if re.search("^[1-9][0-9]*$", xsjf):
            xsjf = int(xsjf)
            xsxj = "★" * int(math.log(xsjf, 3))
        else:
            self.lab1["text"] = "警告:学生积分必须为一个整数!"
            self.lab1["foreground"] = random.choice(["red", "green", "blue", "deeppink", "indigo"])
            return
        if re.search("^[1-9][0-9]*$", xscz):
            xscz = int(xscz)
        else:
            self.lab1["text"] = "警告:学生成长值必须为一个整数!"
            self.lab1["foreground"] = random.choice(["red", "green", "blue", "deeppink", "indigo"])
            return
        tempvar = [xsbh, xsxm, xsbj, xsjf, xsxj, xsch, xscz, xszx, date.today().isoformat()]
        self.tempdata[self.data_sum] = tempvar
        self.studata[xsbh] = tempvar
        saveData("stu", self.studata)
        self.lab1["text"] = f"编号 {xsbh} 同学的数据修改成功!"
        self.lab1["foreground"] = random.choice(["red", "green", "blue", "deeppink", "indigo"])


    def bt2no(self):
        self.data_sum += 1
        if self.data_sum == self.data_len:
            self.data_sum = 0
        self.xg_data(self.tempdata[self.data_sum])




# 展台窗口
class ZT_win():
    def __init__(self, ztdata, fenJL, gg, flags):
        super().__init__()
        self.ztdata = ztdata
        self.fenJL = fenJL
        self.gg = gg
        self.flags = flags
        self.tempdata = list(self.ztdata.values())
        self.tempdata.sort(key=lambda x: x[3])
        self.root = Tk()
        self.root.title("学生积分管理系统展台")
        self.root.protocol("WM_DELETE_WINDOW", self.callback)
        self.setUI()
        win_center(self.root, 800, 600)
        self.thread1 = threading.Thread(target=self.left_top_zt)
        self.thread1.setDaemon(True)
        self.thread1.start()
        self.root.mainloop()




    def callback(self):
        self.flags[0] = 0
        self.root.destroy()


    def setUI(self):
        self.left_frame = Frame(self.root, relief=GROOVE)
        self.right_frame = Frame(self.root, relief=GROOVE)

        self.left_frame.rowconfigure(0, weight=1)
        self.left_frame.rowconfigure(1, weight=1)
        self.left_frame.columnconfigure(0, weight=1)

        self.right_frame.rowconfigure(0, weight=1)
        self.right_frame.rowconfigure(1, weight=1)
        self.right_frame.columnconfigure(0, weight=1)



        self.left_frame.pack(side=LEFT, fill=BOTH, padx=3, pady=3, expand=True)
        self.right_frame.pack(side=RIGHT, fill=BOTH, padx=3, pady=3, expand=True)
        self.right_top_frame = LabelFrame(self.right_frame, text="公告一", labelanchor="n")
        self.right_bottom_frame = LabelFrame(self.right_frame, text="公告二", labelanchor="n")
        self.right_top_frame.grid(row=0, column=0, padx=3, pady=3, sticky = N+S+W+E)
        self.right_bottom_frame.grid(row=1, column=0, padx=3, pady=3, sticky = N+S+W+E)
        self.left_top_frame = LabelFrame(self.left_frame, text="学生信息展示", labelanchor="n")
        self.left_bottom_frame = LabelFrame(self.left_frame, text="新加分消息", labelanchor="n")
        self.left_top_frame.grid(row=0, column=0, padx=5, pady=5, sticky=N+S+W+E)
        self.left_bottom_frame.grid(row=1, column=0, padx=5, pady=5, sticky=N+S+W+E)


        self.gg1_label = Text(self.right_top_frame, padx=3, pady=3, width=15, background="lightblue")
        self.gg2_label = Text(self.right_bottom_frame, padx=3, pady=3, width=15, background="lavender")
        self.gg1_label.pack(padx=2, pady=2, fill=BOTH, expand=True)
        self.gg2_label.pack(padx=2, pady=2, fill=BOTH, expand=True)


        self.left_top_frame.rowconfigure(0, weight=1)
        self.left_top_frame.columnconfigure(0, weight=1)
        self.left_top_frame.columnconfigure(1, weight=1)
        self.left_top_frame.columnconfigure(2, weight=1)
        self.left_top_frame.columnconfigure(3, weight=1)
        self.left_top_frame.columnconfigure(4, weight=1)
        self.left_top_frame.columnconfigure(5, weight=1)
        self.left_top_frame.columnconfigure(6, weight=1)


        self.jfinfo_text = Text(self.left_bottom_frame, padx=5, pady=5, font=("楷体",12), height=10)
        self.jfinfo_text.pack(padx=2, pady=2, fill=BOTH, expand=True)




        bh_label = Label(self.left_top_frame, text="学生编号", relief="solid", anchor=CENTER)
        xm_label = Label(self.left_top_frame, text="姓名", relief="solid", anchor=CENTER)
        bj_label = Label(self.left_top_frame, text="班级", relief="solid", anchor=CENTER)
        jf_label = Label(self.left_top_frame, text="积分", relief="solid", anchor=CENTER)
        xj_label = Label(self.left_top_frame, text="星级", width=10, relief="solid", anchor=CENTER)
        ch_label = Label(self.left_top_frame, text="称号", width=10, relief="solid", anchor=CENTER)
        cz_label = Label(self.left_top_frame, text="成长值", relief="solid", anchor=CENTER)
        bh_label.grid(row=0, column=0, sticky=N+S+W+E)
        xm_label.grid(row=0, column=1, sticky=N+S+W+E)
        bj_label.grid(row=0, column=2, sticky=N+S+W+E)
        jf_label.grid(row=0, column=3, sticky=N+S+W+E)
        xj_label.grid(row=0, column=4, sticky=N+S+W+E)
        ch_label.grid(row=0, column=5, sticky=N+S+W+E)
        cz_label.grid(row=0, column=6, sticky=N+S+W+E)

        self.arr_label = []

        for i in range(1, 11):
            self.left_top_frame.rowconfigure(i, weight=1)
            for j in range(7):
                if i % 2:
                    color1 = "skyblue"
                else:
                    color1 = "snow"
                var_label = Label(self.left_top_frame, text="", relief="solid", background=color1, anchor=CENTER)
                self.arr_label.append(var_label)
                var_label.grid(row=i, column=j, sticky=N+S+W+E)


    def left_top_zt(self):
        time.sleep(0.2)
        self.cf()



    def cf(self):
        zt_page = []
        page_data = []
        for var2 in self.arr_label:
            var2.configure(text="")
        for i in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:
            if len(self.tempdata):
                page_data.append(self.tempdata[-1][0])
                zt_page.extend(self.tempdata.pop()[:7])

            else:
                break
        for var1 in range(len(zt_page)):
            self.arr_label[var1].configure(text=zt_page[var1])
        self.gg1_label.configure(state=NORMAL)
        self.gg2_label.configure(state=NORMAL)
        self.gg1_label.delete("1.0", "end")
        self.gg2_label.delete("1.0", "end")
        self.gg1_label.insert("end", self.gg[0])
        self.gg2_label.insert("end", self.gg[1])
        self.gg1_label.configure(state=DISABLED)
        self.gg2_label.configure(state=DISABLED)

        self.jfinfo_text.delete("1.0", "end")
        arr_color =["red","blue","green"]
        ch_color = random.choice(arr_color)
        self.jfinfo_text.configure(foreground=ch_color,background="lightgray")

        for i in range(len(page_data)):
            if page_data[i] in self.fenJL.keys():
                temp3 = list(self.fenJL[page_data[i]][-1])
                self.jfinfo_text.insert("end",f"{temp3[0]}日,编号为 {temp3[1]} 的同学 {temp3[2]} 加{temp3[3]}分,理由{temp3[4]}\n")

        if not len(self.tempdata):
            self.tempdata = list(self.ztdata.values())
            self.tempdata.sort(key=lambda x: x[3])
        self.root.after(10000, self.cf)



#添加数据窗口
class add_data_win(Toplevel):
    def __init__(self, studata, labelx):
        super().__init__()
        self.studata = studata
        self.lab1 = labelx
        self.title = "添加学生信息"
        self.resizable(0, 0)  # 设置窗口不可改变大小
        self.wm_attributes('-topmost', 1)  # 设置窗口置顶
        self.protocol("WM_DELETE_WINDOW", self.callback)  # 设置窗口不可关闭
        self.xsbh=""
        self.xsxm=""
        self.xsbj=""
        self.xsjf=0
        self.xsxj=""
        self.xsch=""
        self.xscz=1
        self.xszx=""
        self.setUI()
        win_center(self, 250, 230)

    def setUI(self):  # 界面设置
        self.frame1=LabelFrame(self,text="学生信息",labelanchor="n")
        self.frame1.pack(padx=3, pady=3)
        self.bh_label = Label(self.frame1, text="学生编号:", background="gainsboro")
        self.bh_label.grid(row=0, column=0, padx=2, pady=2, sticky=W+E)
        self.bh_en = Entry(self.frame1)
        self.bh_en.grid(row=0,column=1, padx=2, pady=2, sticky=W+E)
        self.xm_label = Label(self.frame1, text="学生姓名:", background="gainsboro")
        self.xm_label.grid(row=1, column=0, padx=2, pady=2, sticky=W+E)
        self.xm_en = Entry(self.frame1)
        self.xm_en.grid(row=1, column=1, padx=2, pady=2, sticky=W+E)
        self.bj_label = Label(self.frame1, text="学生班级:", background="gainsboro")
        self.bj_label.grid(row=2, column=0, padx=2, pady=2, sticky=W + E)
        self.bj_en = Entry(self.frame1)
        self.bj_en.grid(row=2, column=1, padx=2, pady=2, sticky=W + E)
        self.jf_label = Label(self.frame1, text="学生积分:", background="gainsboro")
        self.jf_label.grid(row=3, column=0, padx=2, pady=2, sticky=W + E)
        self.jf_en = Entry(self.frame1)
        self.jf_en.grid(row=3, column=1, padx=2, pady=2, sticky=W + E)
        self.ch_label = Label(self.frame1, text="学生称号:", background="gainsboro")
        self.ch_label.grid(row=4, column=0, padx=2, pady=2, sticky=W + E)
        self.ch_en = Entry(self.frame1)
        self.ch_en.grid(row=4, column=1, padx=2, pady=2, sticky=W + E)
        self.cz_label = Label(self.frame1, text="成长值:", background="gainsboro")
        self.cz_label.grid(row=5, column=0, padx=2, pady=2, sticky=W + E)
        self.cz_en = Entry(self.frame1)
        self.cz_en.grid(row=5, column=1, padx=2, pady=2, sticky=W + E)
        self.ok_btn = Button(self.frame1, text="添加", command=self.bt1ok)
        self.ok_btn.grid(row=6, column=0, padx=2, pady=2)
        self.no_btn = Button(self.frame1, text="取消", command=self.bt2no)
        self.no_btn.grid(row=6, column=1, padx=2, pady=2)

    # 确定按钮功能
    def bt1ok(self):
        self.xsbh = self.bh_en.get()
        self.xsxm = self.xm_en.get()
        self.xsbj = self.bj_en.get()
        self.xsjf = self.jf_en.get()
        self.xsxj =""
        self.xsch = self.ch_en.get()
        self.xscz = self.cz_en.get()
        self.xszx = "在校"
        if self.xsbh.strip() == "" or self.xsxm.strip() == "" or self.xsbj.strip() == "":
            self.lab1["text"] = "警告:学生编号、班级、姓名均不能为空!"
            self.lab1["foreground"] = random.choice(["red", "green", "blue", "deeppink", "indigo"])
            return
        if self.xsbh in self.studata.keys():
            self.lab1["text"] = "警告:学生编号与系统内重复,请重新输入。"
            self.lab1["foreground"] = random.choice(["red", "green", "blue", "deeppink", "indigo"])
            return
        if re.search("^[1-9][0-9]*$",self.xsjf):
            self.xsjf = int(self.xsjf)
            self.xsxj = "★" * int(math.log(self.xsjf, 3))
        else:
            self.lab1["text"] = "警告:学生积分必须为一个整数!"
            self.lab1["foreground"] = random.choice(["red", "green", "blue", "deeppink", "indigo"])
            return
        if re.search("^[1-9][0-9]*$",self.xscz):
            self.xscz = int(self.xscz)
        else:
            self.lab1["text"] = "警告:学生成长值必须为一个整数!"
            self.lab1["foreground"] = random.choice(["red", "green", "blue", "deeppink", "indigo"])
            return
        self.destroy()


    def bt2no(self):  # 取消按钮功能
        self.destroy()

    def callback(self):  # 不可用X关闭
        pass


 # 加分窗口
class topwin(Toplevel):
    def __init__(self, labelx):
        super().__init__()
        self.lab1 = labelx
        self.fen = 0  # 增加的分值
        self.cause = ""  # 加分的原因
        self.resizable(0, 0)  # 设置窗口不可改变大小
        self.wm_attributes('-topmost', 1)  # 设置窗口置顶
        self.protocol("WM_DELETE_WINDOW", self.callback)  # 设置窗口不可关闭
        self.setUI()
        win_center(self, 290, 220)

    def setUI(self):  # 界面设置

        self.lab1 = Label(self, text="增加分值:")
        self.lab1.grid(row=0, column=0, padx=5, pady=5)

        self.en1 = Entry(self)
        self.en1.grid(row=0, column=1, pady=5)

        self.frame1 = LabelFrame(self, text="加分原因", labelanchor="n")
        self.frame1.grid(row=1, column=0, columnspan=2, padx=5)

        self.text1 = Text(self.frame1, width=38, height=10)
        self.text1.pack(padx=2, pady=2)

        self.bt1 = Button(self, text="确定", command=self.bt1ok)
        self.bt1.grid(row=2, column=0)

        self.bt2 = Button(self, text="取消", command=self.bt2no)
        self.bt2.grid(row=2, column=1)

    def bt1ok(self):  # 确定按钮功能
        tempfen = self.en1.get()

        if not re.search("^(-|[1-9])[0-9]*$",tempfen):
            self.lab1["text"] = "警告:请输入正确的数值!"
            self.lab1["foreground"] = random.choice(["red", "green", "blue", "deeppink", "indigo"])
            return
        if tempfen[0] == "-":
            if len(tempfen) > 1:
                self.fen = -int(tempfen[1:])
                self.cause = self.text1.get("1.0", END)[:-1]
                self.destroy()
            else:
                self.lab1["text"] = "警告:请输入正确的数值!"
                self.lab1["foreground"] = random.choice(["red", "green", "blue", "deeppink", "indigo"])
                return
        else:
            self.fen = int(tempfen)
            self.cause = self.text1.get("1.0", END)[:-1]
            self.destroy()



    def bt2no(self):  # 取消按钮功能
        self.destroy()

    def callback(self):  # 不可用X关闭
        pass

def zt_visible(a,b,c,d):
    ZT_win(a,b,c,d)


if __name__ == "__main__":
    stuData, mima, fenJL, gg = openDataFile("stu")
    add_fen(stuData)
    mainWin("学生积分管理系统", stuData, mima, fenJL, gg)

程序所需图:images文件夹下内容 

 所需导入excel模板:

系统密码:123456

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
学生宿舍管理系统一个用于管理学生在宿舍的信息和需求的系统。使用Python制作这样一个系统可以通过以下步骤进行: 1. 设计数据库结构:首先,需要设计一个数据库来存储学生的信息,如姓名、学号、性别、联系方式,以及宿舍的信息,如宿舍号、楼层、宿舍类型等。可以使用SQLite或者其他数据库来存储这些数据。 2. 编写程序框架:接下来,可以使用Python来编写程序框架。可以使用Tkinter或者其他GUI库来创建用户界面,让用户可以方便地输入和查看信息。 3. 实现功能模块:在程序中,可以实现一些功能模块,比如添学生信息、修改学生信息、删除学生信息、查询学生信息等。同时,还可以实现宿舍信息的管理功能,比如添宿舍信息、修改宿舍信息、查看空闲宿舍等。 4. 数据操作和交互:在程序中,通过使用数据库操作语句,可以实现对数据库的增删改查操作。同时,还可以实现用户和程序之间的交互,让用户能够方便地输入自己的信息并查看宿舍的相关情况。 5. 完善和测试:最后,可以对程序进行完善和测试,确保程序的功能能够正常运行并且没有bug,让用户能够方便地使用这个宿舍管理系统。 通过以上步骤,就可以使用Python制作一个学生宿舍管理系统的流程。这样的系统能够有效地管理学生宿舍的信息,让学生和管理人员能够更便捷地了解宿舍相关的情况。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值