import os import tkinter from tkinter import * import tkinter.messagebox as mb from tkinter import ttk import tkinter as tk import _sqlite3 # 数据库 # conn = _sqlite3.connect(":memory:") conn = _sqlite3.connect("data.db") c = conn.cursor() #创建表score # c.execute("create table score(itemName char(20),importPrice char(20)," # "sellPrice char(20),deductPrice char(20))") #添加成绩sql语句 def Insert(itemName, importPrice, sellPrice, deductPrice): c.execute('insert into score values(?,?,?,?)', (itemName, importPrice, sellPrice, deductPrice)) # 提交事务 conn.commit() #根据学号删除成绩 def Del(sellPrice): c.execute("delete from score where sellPrice = '%s'" % sellPrice) # 提交事务 conn.commit() #查询成绩sql语句 def Serch(): c.execute('select * from score') li = c.fetchall() # 提交事务 conn.commit() return li #显示所有的学生信息,先按照成绩从大-->小排序,当成绩相同时 按照学号从高-->矮排序: def Serchone2(): c.execute("select * from score order by deductPrice") # c.execute("select * from score order by deductPrice desc,sellPrice desc") # 提交事务 conn.commit() return c.fetchall() #根据学号查询成绩 def Serchone(sellPrice): c.execute("select * from score where score.sellPrice = '%s'" % sellPrice) # 提交事务 conn.commit() return c.fetchall() # 修改成绩 def Update(itemName, importPrice, sellPrice, deductPrice): c.execute('update score set itemName = (?),importPrice = (?),deductPrice = (?) where sellPrice = (?)', (itemName, importPrice, deductPrice, sellPrice)) # 提交事务 conn.commit() #根据id查询教师个人信息sql语句 def SerchMy(name): c.execute("select * from teachers where teachers.name = '%s'" % name) return c.fetchall() #修改密码sql语句 def UpdatePassWord(id, name): c.execute('update teachers set id = (?) where name = (?)', (id, name)) # 提交事务 conn.commit() #主窗体 class WelcomeFrame(Frame): # 继承Frame类 def __init__(self, name, master=None): Frame.__init__(self, master) self.root = master # 定义内部变量root self.usrname = name self.createPage() def createPage(self): Label(self, text='欢迎教师'+self.usrname+'登录', font=("黑体", 20)).pack() #我的信息 class MyFrame(Frame): def __init__(self, name, master=None): Frame.__init__(self, master) self.root = master # 定义内部变量root self.name = name self.createPage() def createPage(self): Button(self, text='个人信息', command=self.My_Page).pack() def My_Page(self): li = SerchMy(self.name) pth = li[0] print(pth) Label(self, text='工号: ').pack(side=LEFT,ipady=10) Label(self, text='%s' % pth[0]).pack(side=LEFT,ipady=10) Label(self, text='姓名: ').pack(side=LEFT,ipady=10) Label(self, text='%s' % pth[1]).pack(side=LEFT,ipady=10) Label(self, text='性别: ').pack(side=LEFT,ipady=10) Label(self, text='%s' % pth[2]).pack(side=LEFT,ipady=10) Label(self, text='专业: ').pack(side=LEFT,ipady=10) Label(self, text='%s' % pth[3]).pack(side=LEFT,ipady=10) Label(self, text='职称: ').pack(side=LEFT,ipady=10) Label(self, text='%s' % pth[4]).pack(side=LEFT,ipady=10) Label(self, text='入职年月: ').pack(side=LEFT,ipady=10) Label(self, text='%s' % pth[5]).pack(side=LEFT,ipady=10) #修改密码 class UpdatepwdFrame(Frame): def __init__(self, name, master=None): Frame.__init__(self, master) self.root = master # 定义内部变量root self.name = name self.id = StringVar() self.createPage() def createPage(self): Button(self, text='修改密码', command=self.My_Page).grid() def My_Page(self): li = SerchMy(self.name) pth = li[0] print(pth) self.id = tkinter.StringVar() self.ids = tkinter.StringVar() Label(self).grid(row=0, stick=W, pady=10) Label(self, text='请输入新密码: ').grid(row=1, stick=W, pady=10) Entry(self, textvariable=self.id).grid(row=1, column=1, stick=E) Label(self, text='确认密码: ').grid(row=2, stick=W, pady=10) Entry(self, textvariable=self.ids).grid(row=2, column=1, stick=E) Button(self, text='确认修改', width=8, height=2, command=self.UpdateDate).grid(row=3, column=1, stick=E) self.id.set('') self.ids.set('') def UpdateDate(self): UpdatePassWord(self.id.get(),self.name) tkinter.messagebox.showerror("成功", "修改成功") #成绩录入 class InputFrame(Frame): # 继承Frame类 def __init__(self, master=None): Frame.__init__(self, master) self.root = master # 定义内部变量root self.itemName = StringVar() self.importPrice = StringVar() self.sellPrice = StringVar() self.deductPrice = StringVar() self.createPage() def createPage(self): Label(self).grid(row=0, stick=W, pady=10) Label(self, text='课程名称: ').grid(row=1, stick=W, pady=10) Entry(self, textvariable=self.itemName).grid(row=1, column=1, stick=E) Label(self, text='班级: ').grid(row=2, stick=W, pady=10) Entry(self, textvariable=self.importPrice).grid(row=2, column=1, stick=E) Label(self, text='学号: ').grid(row=3, stick=W, pady=10) Entry(self, textvariable=self.sellPrice).grid(row=3, column=1, stick=E) Label(self, text='成绩: ').grid(row=4, stick=W, pady=10) Entry(self, textvariable=self.deductPrice).grid(row=4, column=1, stick=E) Button(self, text='成绩录入',command=self.AddScore).grid(row=6, column=1, stick=E, pady=10) #成绩录入 def AddScore(self): itemName = self.itemName.get() importPrice = self.importPrice.get() sellPrice = self.sellPrice.get() deductPrice = self.deductPrice.get() if sellPrice == '' or deductPrice == '': mb.showerror("错误", "信息不能为空!") else: Insert(itemName, importPrice, sellPrice, deductPrice) mb.showinfo("成功", "录入成绩成功") self.itemName.set('') self.importPrice.set('') self.sellPrice.set('') self.deductPrice.set('') #查询 class QueryFrame(Frame): # 继承Frame类 def __init__(self, master=None): Frame.__init__(self, master) self.root = master # 定义内部变量root self.itemName = StringVar() self.sellPrice = StringVar() self.createPage() def createPage(self): Label(self, text='查询界面', font=('华文行楷', 20), fg='purple').pack(pady=12) #查询所有成绩按钮 Button(self, text='查询所有成绩', command=self.show_score_frame).place(relx=0.18, rely=0.2, anchor='center') # 根据学号查询框 self.sellPrice = tkinter.StringVar() tkinter.Label(self, text='学号:').place(relx=0.4, rely=0.2, anchor='center') tkinter.Entry(self, textvariable=self.sellPrice).place(relx=0.46, rely=0.17, width=70) #学号查询按钮 Button(self, text='根据学号查询', command=self.show_score_frame2).place(relx=0.85, rely=0.2, anchor='center') Label(self, text='', font=('华文行楷', 20), fg='purple').pack(pady=2,anchor='w') columns = ("itemName", "importPrice", "sellPrice", "deductPrice") columns_values = ("课程名称", "班级", "学号", "成绩") self.tree_view = ttk.Treeview(self, show='headings', columns=columns) self.tree_view.column('itemName', width=80, anchor='center') self.tree_view.column('importPrice', width=80, anchor='center') self.tree_view.column('sellPrice', width=80, anchor='center') self.tree_view.column('deductPrice', width=80, anchor='center') self.tree_view.heading('itemName', text='课程名称') self.tree_view.heading('importPrice', text='班级') self.tree_view.heading('sellPrice', text='学号') self.tree_view.heading('deductPrice', text='成绩') self.tree_view.pack(fill=tk.BOTH, expand=True) self.show_score_frame() #查询全部学生成绩信息 def show_score_frame(self): # 删除旧的数据 for _ in map(self.tree_view.delete, self.tree_view.get_children('')): pass students = Serch() index = 0 for stu in students: print(stu) self.tree_view.insert('', index + 1, values=( stu[0], stu[1], stu[2], stu[3], )) #根据学号查询学生信息 def show_score_frame2(self): li = Serchone(self.sellPrice.get()) print(li) # 删除旧的数据 for _ in map(self.tree_view.delete, self.tree_view.get_children('')): pass index = 0 for stu in li: print(stu) self.tree_view.insert('', index + 1, values=( stu[0], stu[1], stu[2], stu[3], #设置输入框为空 self.sellPrice.set('') )) #统计 class CountFrame(Frame): # 继承Frame类 def __init__(self, master=None): Frame.__init__(self, master) self.root = master # 定义内部变量root self.createPage() def createPage(self): Label(self, text='统计界面', font=('华文行楷', 20), fg='purple').pack() Button(self, text='根据成绩排序', command=self.show_score).pack(anchor='center', pady=5) columns = ("itemName", "importPrice", "sellPrice", "deductPrice") columns_values = ("课程名称", "班级", "学号", "成绩") # # 创建滚动条 self.VScroll1 = Scrollbar(self, orient='vertical') # 给treeview添加配置 #,yscrollcommand=self.VScroll1.setm self.tree_view = ttk.Treeview(self, show='headings', columns=columns,yscrollcommand=self.VScroll1.set) self.tree_view.column('itemName', width=80, anchor='center') self.tree_view.column('importPrice', width=80, anchor='center') self.tree_view.column('sellPrice', width=80, anchor='center') self.tree_view.column('deductPrice', width=80, anchor='center') self.tree_view.heading('itemName', text='课程名称') self.tree_view.heading('importPrice', text='班级') self.tree_view.heading('sellPrice', text='学号') self.tree_view.heading('deductPrice', text='成绩') self.tree_view.pack(fill=tk.BOTH, expand=True) self.show_score() #根据成绩排序查询全部学生成绩信息 def show_score(self): # 删除旧的数据 for _ in map(self.tree_view.delete, self.tree_view.get_children('')): pass students = Serchone2() index = 0 for stu in students: print(stu) self.tree_view.insert('', index + 1, values=( stu[0], stu[1], stu[2], stu[3], )) #编辑 class EditFrame(Frame): # 继承Frame类 def __init__(self, master=None): Frame.__init__(self, master) self.root = master # 定义内部变量root self.itemName = StringVar() self.importPrice = StringVar() self.sellPrice = StringVar() self.deductPrice = StringVar() self.createPage() def createPage(self): Label(self, text='编辑界面', font=('华文行楷', 20), fg='purple').pack() Button(self, text='修改成绩', command=self.update_score_frame).pack(anchor='center', pady=5) Button(self, text='删除成绩', command=self.del_score_frame).pack(anchor='center', pady=5) # 修改成绩信息 def update_score_frame(self): def update_show(): li = Serchone(sellPrice.get()) if len(li) == 0: tkinter.messagebox.showerror("查询失败", "学号不存在") win4.destroy() pth = li[0] print(pth) tkinter.Label(win4, text='%s' % pth[1]).place(relx=0.67, rely=0.22, anchor='center') tkinter.Label(win4, text='%s' % pth[0]).place(relx=0.67, rely=0.32, anchor='center') tkinter.Label(win4, text='%s' % pth[3]).place(relx=0.67, rely=0.42, anchor='center') def UpdateDate(): Update(itemName.get(), importPrice.get(), sellPrice.get(), deductPrice.get()) tkinter.messagebox.showerror("成功", "修改成功") win4.destroy() # 设置窗口位置 # 不能使用两次Tk()去创建窗体,因为tkinter中只能有一个主线程, win4 = tkinter.Toplevel() win4.title('修改学生成绩') win4.geometry('500x400') sw = win4.winfo_screenwidth() sh = win4.winfo_screenheight() win4.geometry('+%d+%d' % ((sw - 500) / 2, (sh - 300) / 2)) # 欢迎语 l = tkinter.Label(win4, text='欢迎进入修改页面', font=('华文行楷', 20), fg='purple') l.place(relx=0.5, rely=0.05, anchor='center') itemName = tkinter.StringVar() importPrice = tkinter.StringVar() sellPrice = tkinter.StringVar() deductPrice = tkinter.StringVar() #学号输入框 tkinter.Label(win4, text='学号:').place(relx=0.4, rely=0.12, anchor='center') tkinter.Entry(win4, textvariable=sellPrice).place(relx=0.47, rely=0.1, width=70) # 班级输入框 tkinter.Label(win4, text='班级:').place(relx=0.4, rely=0.22, anchor='center') tkinter.Entry(win4, textvariable=importPrice).place(relx=0.47, rely=0.2, width=70) # 课程名称输入框 tkinter.Label(win4, text='课程名称:').place(relx=0.4, rely=0.32, anchor='center') tkinter.Entry(win4, textvariable=itemName).place(relx=0.47, rely=0.3, width=70) # 成绩输入框 tkinter.Label(win4, text='成绩:').place(relx=0.4, rely=0.42, anchor='center') tkinter.Entry(win4, textvariable=deductPrice).place(relx=0.47, rely=0.4, width=70) # 按钮 tkinter.Button(win4, text='查询', width=10, height=3, bg='gray', command=update_show).place(relx=0, rely=1,anchor='sw') tkinter.Button(win4, text='确认修改', width=10, height=3, bg='gray', command=UpdateDate).place(relx=0.15,rely=1,anchor='sw') tkinter.Button(win4, text='退出', width=10, height=3, bg='gray', command=win4.destroy).place(relx=1, rely=1,anchor='se') # 删除成绩信息 def del_score_frame(self): def show(): li = Serchone(sellPrice.get()) if len(li) == 0: tkinter.messagebox.showerror("错误", "学号不存在") win3.destroy() pth = li[0] print(pth) tkinter.Label(win3, text='%s' % pth[0]).place(relx=0.52, rely=0.2, anchor='center') tkinter.Label(win3, text='%s' % pth[1]).place(relx=0.52, rely=0.3, anchor='center') tkinter.Label(win3, text='%s' % pth[3]).place(relx=0.52, rely=0.4, anchor='center') def delDate(): Del(sellPrice.get()) tkinter.messagebox.showerror("成功", "删除成功") win3.destroy() # 设置窗口位置 # 不能使用两次Tk()去创建窗体,因为tkinter中只能有一个主线程, win3 = tkinter.Toplevel() win3.title('删除教师信息') win3.geometry('500x400') sw = win3.winfo_screenwidth() sh = win3.winfo_screenheight() win3.geometry('+%d+%d' % ((sw - 500) / 2, (sh - 300) / 2)) # 欢迎语 l = tkinter.Label(win3, text='欢迎进入删除页面', font=('华文行楷', 20), fg='purple') l.place(relx=0.5, rely=0.05, anchor='center') # 学号输入框 sellPrice = tkinter.StringVar() tkinter.Label(win3, text='学号:').place(relx=0.41, rely=0.12, anchor='center') tkinter.Entry(win3, textvariable=sellPrice).place(relx=0.47, rely=0.1, width=70) # 课程名称展示框 tkinter.Label(win3, text='课程名称:').place(relx=0.38, rely=0.2, anchor='center') # 班级展示框 tkinter.Label(win3, text='班级:').place(relx=0.41, rely=0.3, anchor='center') # 成绩展示框 tkinter.Label(win3, text='成绩:').place(relx=0.41, rely=0.4, anchor='center') # 按钮 tkinter.Button(win3, text='查询', width=10, height=3, bg='gray', command=show).place(relx=0, rely=1,anchor='sw') tkinter.Button(win3, text='确认删除', width=10, height=3, bg='gray', command=delDate).place(relx=0.15, rely=1,anchor='sw') tkinter.Button(win3, text='退出', width=10, height=3, bg='gray', command=win3.destroy).place(relx=1, rely=1,anchor='se') #意见反馈 class FeedbackFrame(Frame): def __init__(self, master=None): Frame.__init__(self, master) self.root = master # 定义内部变量root self.createPage() def createPage(self): Label(self, text='学生意见收集箱', font=('华文行楷', 20), fg='purple').pack() text = tk.Text(self, width=100, height=20) text.pack() text.insert("insert", "") text.insert('end', '\n') mytxtfile = "mytxtfile.txt" # 读取TXT文件,显示在tkinter的Text def in_f_txt(): if os.path.exists(mytxtfile): a = open(mytxtfile, 'r', encoding='utf-8') for id_names in a: text.insert('insert', id_names) a.close() b3 = tk.Button(text, text="查看", command=in_f_txt) text.window_create("insert", window=b3) text.insert('end', '\n')