from tkinter import * from tkinter import ttk from tkinter import messagebox from tkinter import filedialog import pickle win=Tk() win.title('学生信息管理系统') win.geometry('520x590+400+200') ListTemp=[] f1 = LabelFrame(win,text='信息管理') f1.grid(row=0,column=0,padx=10,pady=10) Label(f1,text='学生姓名').grid(row=0,column=0,padx=10,pady=10) Label(f1,text='学生年龄').grid(row=0,column=2,padx=10,pady=10) Label(f1,text='数学成绩').grid(row=1,column=0,padx=10,pady=10) Label(f1,text='语文成绩').grid(row=1,column=2,padx=10,pady=10) Label(f1,text='现住城市').grid(row=2,column=2,padx=10,pady=10) name=StringVar() Entry(f1,textvariable=name,width=20).grid(row=0,column=1,padx=10,pady=10) age=StringVar() Entry(f1,textvariable=age,width=20).grid(row=0,column=3,padx=10,pady=10) math=StringVar() Entry(f1,textvariable=math,width=20).grid(row=1,column=1,padx=10,pady=10) chinese=StringVar() Entry(f1,textvariable=chinese,width=20).grid(row=1,column=3,padx=10,pady=10) v = IntVar() v.set(1) Radiobutton(f1,text='男生',variable=v,value=1).grid(row=2,column=0,padx=10,pady=10) Radiobutton(f1,text='女生',variable=v,value=2).grid(row=2,column=1,padx=10,pady=10) city=StringVar() c = ttk.Combobox(f1,textvariable=city,width=15) c.grid(row=2,column=3,padx=10,pady=10) c['values'] = ('北京市','上海市','深圳市') c['state'] = 'readonly' c.current(0) f2 = LabelFrame(win,text='信息查询') f2.grid(row=1,column=0,padx=10,pady=10,sticky=W+E) ch=StringVar() ch.set('输入学生姓名') Button(f2,text='查询',width=10).grid(row=0,column=4,padx=10,pady=10) Entry(f2,textvariable=ch,width=15).grid(row=0,column=5,padx=10,pady=10) f3 = LabelFrame(win,text='学生情况明细',font=(None ,10),padx=5,pady=5) f3.grid(row=3,column = 0,padx=10,pady=1) tree = ttk.Treeview(f3,show='headings',columns=('name','age','sex','city','math','chinese')) tree.column('name',width=80,anchor='center') tree.heading('name',text='姓名') tree.column('age',width=60,anchor='center') tree.heading('age',text='年龄') tree.column('sex',width=80,anchor='center') tree.heading('sex',text='性别') tree.column('city',width=80,anchor='center') tree.heading('city',text='现住城市') tree.column('math',width=80,anchor='center') tree.heading('math',text='数学成绩') tree.column('chinese',width=80,anchor='center') tree.heading('chinese',text='语文成绩') #加滚动条 vbar = Scrollbar(f3, orient=VERTICAL) vbar.config(command = tree.yview) tree.configure(yscrollcommand=vbar.set) tree.grid(row=3) vbar.grid(row=3, column=1,sticky=NS) tree.grid() f4 = LabelFrame(win,padx=5,pady=5) f4.grid(row=5,column = 0,padx=10,pady=1,sticky=W+E) #添加学生 def addDate(): if name.get()=='' or age.get()=='' or math.get()=='' or chinese.get()=='': messagebox.showinfo('提示','数据不完整') return sname = name.get() sage = age.get() smath = math.get() schinese = chinese.get() ssex = v.get() if ssex==1: ssex='男' else: ssex='女' scity = city.get() tree.insert('',END,values=(sname,sage,ssex,schinese,smath,scity)) messagebox.showinfo('提示','添加成功') #修改学生 def modDate(): try: i = tree.selection() t = tree.item(i,'values') name.set(t[0]) age.set(t[1]) math.set(t[2]) chinese.set(t[2]) except: messagebox.showinfo('提示','请先选中要修改的学生') return r = messagebox.askyesno('提示','确定要修改此学生吗?') if r==True: tree.delete(i) #删除学生 def delDate(): try: i = tree.selection() except: messagebox.showinfo('提示','请先选中要删除的学生') return r = messagebox.askyesno('提示','确定要删除此学生吗?') if r==True: tree.delete(i) messagebox.showinfo('提示','删除成功') #重置表单 def clearDate(): name.set('') age.set('') math.set('') chinese.set('') Button(f2,text='添加',width=5,command=addDate).grid(row=0,column=0,padx=10,pady=10) Button(f2,text='修改',width=5,command=modDate).grid(row=0,column=1,padx=10,pady=10) Button(f2,text='删除',width=5,command=delDate).grid(row=0,column=2,padx=10,pady=10) Button(f2,text='重置',width=5,command=clearDate).grid(row=0,column=3,padx=10,pady=10) stuList=[] #保存数据 def saveDate(): itemList=[] items = tree.get_children() for i in items: stuList.append(list(tree.item(i,'values'))) fileName = filedialog.asksaveasfilename(defaultextension='.txt') r = messagebox.askyesno('提示','确定更新数据吗') if r==True: with open(fileName,'wb') as file: pickle.dump(stuList,file) #tree.delete(i) messagebox.showinfo('提示','保存成功') #读取数据 def readDate(): fileName = filedialog.askopenfilename() with open(fileName,'rb') as file: s = pickle.load(file) for i in s: tree.insert('',END,values=(i[0],i[1],i[2],i[3],i[4],i[5])) #自定义项 def zhidingyi(): pass #退出程序 def exitDate(): win.destroy() Button(f4,text='读取数据',width=12,anchor='center',command=readDate).grid(row=0,column=0,padx=10,pady=10) Button(f4,text='保存数据',width=12,anchor='center',command=saveDate).grid(row=0,column=1,padx=10,pady=10) Button(f4,text='自定义项',width=12,anchor='center').grid(row=0,column=2,padx=10,pady=10) Button(f4,text='退出程序',width=12,anchor='center',command=exitDate).grid(row=0,column=3,padx=10,pady=10) win.mainloop()