|
楼主 |
发表于 2020-5-5 21:13:57
|
显示全部楼层
我自己用Tkinter加工了一下,最后加入存盘的操作时出现了问题,大佬有时间帮我看看嘛,谢谢啦!- import tkinter as tk
- import tkinter.messagebox
- import pickle
- def saveup(cdic,ydic):
- a = [cdic,ydic]
- file = open('数据.pkl','wb')
- pickle.dump(a,file)
- file.close()
-
- def savedown():
- file = open('数据.pkl','rb')
- a = pickle.load(file)
- return a[0],a[1]
- def addstudent():
- def save():
- while True:
- if str(v1.get()) in cdic:
- if tk.messagebox.askokcancel('提示', '该学生已存在!是否重新编辑?') == True:
- del cdic[str(v1.get())]
- else:
- tk.messagebox.showinfo('提示','取消成功')
- break
- valu = int(v2.get())
- cdic[str(v1.get())] = valu
- ydic[str(v1.get())] = '暂无记录!'
- break
- tk.messagebox.showinfo('提示','完成操作')
- saveup(cdic,ydic)
-
- cdic,ydic = savedown()
- root1 = tk.Frame(root)
- root1.place(relwidth = 0.5,relheight = 0.5)
- v1 = tk.StringVar()
- v2 = tk.IntVar()
-
- tk.Label(root1,text = '请输入学生姓名:').grid(row = 1,column = 1)
- tk.Entry(root1,textvariable = v1).grid(row = 1,column = 2,padx = 10,pady = 5)
-
- tk.Label(root1,text = '请输入学生分数:').grid(row = 2,column = 1)
- tk.Entry(root1,textvariable = v2).grid(row = 2,column = 2,padx = 10,pady = 5)
-
- tk.Button(root1,text = '保存',command = save).grid(row = 3,column = 2)
-
- def lookstudent():
- def save():
- root3 = tk.Frame(root)
- root3.place(relwidth = 0.5,relheight = 0.5)
- try:
- tk.Label(root3,text = '学生' + v1.get() + '的分数:' + str(cdic[v1.get()])).grid(row = 1,column = 1)
- tk.Label(root3,text = '分数记录:').grid(row = 2,column = 1)
- tk.Label(root3,text = ydic[v1.get()]).grid(row = 3,column = 1)
- except:
- tk.messagebox.showerror('错误','该学生不存在')
-
- root2 = tk.Frame(root)
- root2.place(relwidth = 0.5,relheight = 0.5)
-
- v1 = tk.StringVar()
-
- tk.Label(root2,text = '请输入学生姓名:').grid(row = 1,column = 1)
- tk.Entry(root2,textvariable = v1).grid(row = 1,column = 2,padx = 10,pady = 5)
- tk.Button(root2,text = '查询',command = save).grid(row = 3,column = 2)
- def delstudent():
- def dell():
- try:
- del cdic[v1.get()]
- del ydic[v1.get()]
- tk.messagebox.showinfo('提示','删除成功')
- except KeyError:
- tk.messagebox.showinfo('提示','该学生不存在!')
- saveup(cdic,ydic)
- cdic,ydic = savedown()
- root4 = tk.Frame(root)
- root4.place(relwidth = 0.5,relheight = 0.5)
-
- v1 = tk.StringVar()
-
- tk.Label(root4,text = '请输入学生姓名:').grid(row = 1,column = 1)
- tk.Entry(root4,textvariable = v1).grid(row = 1,column = 2,padx = 10,pady = 5)
- tk.Button(root4,text = '删除',command = dell).grid(row = 3,column = 2)
- def modifystudent():
- def save():
- try:
- do = v3.get()
- if do[0] == '+':
- cdic[v1.get()] += int(do[0:])
- ydic[v1.get()] = '变动分数:' + do + ', '
- elif do[0] == '-':
- cdic[v1.get()] -= int(do[0:])
- ydic[v1.get()] = '变动分数:' + do + ', '
- else:
- tk.messagebox.showerror('错误','输入不合法!')
- tish = '变动原因:' + str(v2.get())
- if ydic[v1.get()] != '暂无记录!':
- ydic[v1.get()] += tish
- tk.messagebox.showinfo('提示','更新成功')
- else:
- ydic[v1.get()] += tish,'\n'
- tk.messagebox.showinfo('提示','更新成功')
- except KeyError as a:
- tk.messagebox.showinfo('提示','该学生不存在!')
- saveup(cdic,ydic)
- cdic,ydic = savedown()
- root5 = tk.Frame(root)
- root5.place(relwidth = 0.5,relheight = 0.5)
-
- v1 = tk.StringVar()
- v2 = tk.StringVar()
- v3 = tk.StringVar()
-
- tk.Label(root5,text = '学生姓名:').grid(row = 1,column = 1)
- tk.Entry(root5,textvariable = v1).grid(row = 1,column = 2,padx = 10,pady = 5)
- tk.Label(root5,text = '原因:').grid(row = 2,column = 1)
- tk.Entry(root5,textvariable = v2).grid(row = 2,column = 2,padx = 10,pady = 5)
- tk.Label(root5,text = '加减分数:').grid(row = 3,column = 1)
- tk.Entry(root5,textvariable = v3).grid(row = 3,column = 2,padx = 10,pady = 5)
- tk.Button(root5,text = '完成',command = save).grid(row = 4,column = 2)
- cdic,ydic = savedown()
- root = tk.Tk()
- root.title('学生分数管理系统')
- root.geometry("400x200+500+500")
- menubar = tk.Menu(root)
- menubar.add_command(label = '学生新增',command = addstudent,)
- menubar.add_command(label = '查看学生',command = lookstudent)
- menubar.add_command(label = '学生删除',command = delstudent)
- menubar.add_command(label = '更新分数',command = modifystudent)
- menubar.add_command(label = '退出程序',command = root.destroy)
- root.config(menu = menubar)
- root.mainloop()
复制代码 |
|