from tkinter import * from tkinter import ttk import tkinter as tk import tkinter.messagebox as msgbox from tkinter import scrolledtext import os def Close(): reply=msgbox.askyesno('提示',"您确定退出系统吗?") if reply==True: root.destroy() def Help(): HelpGUI=Toplevel() HelpGUI.title('帮助') HelpGUI.geometry('350x350') HelpGUI.resizable(False,False) scr=scrolledtext.ScrolledText(HelpGUI,width=40,height=20,font=("Microsoft YaHei",12)) scr.insert(tk.INSERT,"""\ 用户须知: 1.本产品功能有限,仅支持这些操作: (1)录入书籍信息 (2)浏览书籍信息 (3)删除书籍信息 (4)修改书籍信息 (5)按书名统计书籍的数量 (6)按作者统计书籍的数量 (7)按出版社统计书籍的数量 (8)按类别统计书籍的数量 2.有关上述操作的说明: (1)1.1至1.4功能全部在基本功能菜单栏下,且每录入一条书籍信息或者修改书籍信息或者删除书籍信息,都会实时更新在文件中,用户无需进行点击保存的操作,如果用户想要浏览文件信息,需要选择菜单栏下的“浏览书籍信息”即可看到当前存入的所有书籍 (2)1.5至1.8功能在高级需求菜单栏下,这部分的功能会用一张表格输出 (3)有关排序的操作直接点击界面中的列名称即可,系统会自动根据选中的列信息进行排序 (4)退出的功能直接点击关闭即可 3.产品说明: ·开发者: 计算机20-2班 王思进 201002603 """) scr.configure(state='disabled') scr.pack() def CreateBookInfo(): CBIGUI=Toplevel() CBIGUI.title('录入书籍信息') CBIGUI.geometry('300x300') def IsEmpty(text): temp=0 for i in text: if not i.isspace(): temp=1 break if temp==1: return 0 else: return 1 def click(): num=addnuminput.get() name=addnameinput.get() author=addauthorinput.get() press=addpressinput.get() category=v.get() if IsEmpty(num) or IsEmpty(name) or IsEmpty(author) or IsEmpty(press) or IsEmpty(category): msgbox.showerror(title='提示',message="请填写所有信息!") else: f=open('图书.csv','r',encoding='utf-8-sig') for line in f.readlines(): info=line[:-1].split(",") if len(info)<5: break if info[0]==num: msgbox.showinfo(title='错误',message="序号已存在!") f.close() CBIGUI.destroy() return f.close() f=open('图书.csv','a',encoding='utf-8-sig') f.write('{},{},{},{},{}\n'.format(num,name,author,press,category)) f.close() reply=msgbox.showinfo(title='提示',message ="写入成功!") if reply: CBIGUI.destroy() addnum=Label(CBIGUI,text="序号",font=("Microsoft YaHei",10),anchor='w') addnum.place(x=30,y=30,height=20,width=80) addnuminput=Entry(CBIGUI,font=("Microsoft YaHei",10)) addnuminput.place(x=100,y=30,height=24,width=150) addname=Label(CBIGUI,text="书名",font=("Microsoft YaHei",10),anchor='w') addname.place(x=30,y=60,height=20,width=80) addnameinput=Entry(CBIGUI,font=("Microsoft YaHei",10)) addnameinput.place(x=100,y=60,height=24,width=150) addauthor=Label(CBIGUI,text="作者",font=("Microsoft YaHei",10),anchor='w') addauthor.place(x=30,y=90,height=20,width=80) addauthorinput=Entry(CBIGUI,font=("Microsoft YaHei",10)) addauthorinput.place(x=100,y=90,height=24,width=150) addpress=Label(CBIGUI,text="出版社",font=("Microsoft YaHei",10),anchor='w') addpress.place(x=30,y=120,height=20,width=80) addpressinput=Entry(CBIGUI,font=("Microsoft YaHei",10)) addpressinput.place(x=100,y=120,height=24,width=150) addcategory=Label(CBIGUI,text="类别",font=("Microsoft YaHei",10),anchor='w') addcategory.place(x=30,y=150,height=20,width=80) """addcategoryinput=Entry(CBIGUI,font=("Microsoft YaHei",10)) addcategoryinput.place(x=100,y=150,height=24,width=150)""" v=StringVar() v.set('其它') w1=Radiobutton(CBIGUI,text="专业书", value='专业书',variable=v) w2=Radiobutton(CBIGUI,text="工具书", value='工具书',variable=v) w3=Radiobutton(CBIGUI,text="报告", value='报告',variable=v) w4=Radiobutton(CBIGUI,text="小说", value='小说',variable=v) w5=Radiobutton(CBIGUI,text="其它", value='其它',variable=v) w1.place(x=100,y=150,height=20,width=60) w2.place(x=100,y=170,height=20,width=60) w3.place(x=95,y=190,height=20,width=60) w4.place(x=95,y=210,height=20,width=60) w5.place(x=95,y=230,height=20,width=60) Buttonadd=Button(CBIGUI,command=click,text="添加",font=("Microsoft YaHei",10)) Buttonadd.place(x=120,y=260,height=30,width=70) def DeleteBook(): DBGUI=Toplevel() DBGUI.title('删除书籍信息') DBGUI.geometry('400x400') def click(): num=addnuminput.get() fl=False def confirm(): reply=msgbox.askyesno('提示',"您确定要删除该图书信息吗?") if reply==True: with open("图书.csv","r",encoding="utf-8-sig") as f: lines=f.readlines() with open("图书.csv","w",encoding="utf-8-sig") as f_w: for line in lines: info=line[:-1].split(",") if info[0]==num: continue f_w.write(line) DBGUI.destroy() f=open('图书.csv','r',encoding='utf-8-sig') for line in f.readlines(): info=line[:-1].split(",") if len(info)<5: break if info[0]==num: fl=True name=info[1];author=info[2];press=info[3];category=info[4] f.close() if fl==False: msgbox.showinfo(title='错误',message="未找到该图书信息!") DBGUI.destroy() else: Info=Label(DBGUI,text="当前图书信息如下:") Info.place(x=50,y=60) addnum=Label(DBGUI,text="序号:"+num,font=("Microsoft YaHei",10),anchor='w') addnum.place(x=50,y=90,height=15,width=80) addname=Label(DBGUI,text="书名:"+name,font=("Microsoft YaHei",10),anchor='w') addname.place(x=50,y=120,height=15,width=80) addauthor=Label(DBGUI,text="作者:"+author,font=("Microsoft YaHei",10),anchor='w') addauthor.place(x=50,y=150,height=15,width=80) addpress=Label(DBGUI,text="出版社:"+press,font=("Microsoft YaHei",10),anchor='w') addpress.place(x=50,y=180,height=15,width=80) addcategory=Label(DBGUI,text="类别:"+category,font=("Microsoft YaHei",10),anchor='w') addcategory.place(x=50,y=210,height=15,width=80) adddeletebutton=Button(DBGUI,command=confirm,text="确认删除",font=("Microsoft YaHei",10)) adddeletebutton.place(x=170,y=270,height=30,width=60) addnum=Label(DBGUI,text="序号",font=("Microsoft YaHei",10),anchor='w') addnum.place(x=50,y=30,height=20,width=80) addnuminput=Entry(DBGUI,font=("Microsoft YaHei",10)) addnuminput.place(x=100,y=30,height=24,width=150) addsearchbutton=Button(DBGUI,command=click,text="查询",font=("Microsoft YaHei",10)) addsearchbutton.place(x=270,y=30,height=24,width=40) def ModifyBook(): MBGUI=Toplevel() MBGUI.title('修改书籍信息') MBGUI.geometry('400x400') def click(): num=addnuminput.get() def IsEmpty(text): temp=0 for i in text: if not i.isspace(): temp=1 break if temp==1: return 0 else: return 1 def confirm(): name=addnameinput.get() author=addauthorinput.get() press=addpressinput.get() category=v.get() if IsEmpty(name) or IsEmpty(author) or IsEmpty(press) or IsEmpty(category): msgbox.showerror(title='提示',message="请填写所有信息!") else: reply=msgbox.askyesno('提示',"您确定要修改该图书信息吗?") if reply==True: with open("图书.csv","r",encoding="utf-8-sig") as f: lines=f.readlines() with open("图书.csv","w",encoding="utf-8-sig") as f_w: for line in lines: info=line[:-1].split(",") if info[0]==num: f_w.write('{},{},{},{},{}\n'.format(num,name,author,press,category)) continue f_w.write(line) MBGUI.destroy() fl=False f=open('图书.csv','r',encoding='utf-8-sig') i=1; for line in f.readlines(): info=line[:-1].split(",") if len(info)<5: break if info[0]==num: fl=True name=info[1];author=info[2];press=info[3];category=info[4] i=i+1 f.close() if fl==False: msgbox.showinfo(title='错误',message="未找到该图书信息!") MBGUI.destroy() else: Info=Label(MBGUI,text="当前图书信息如下:") Info.place(x=50,y=60) sv=StringVar() sv.set(name) addname=Label(MBGUI,text="书名:",font=("Microsoft YaHei",10),anchor='w') addname.place(x=50,y=90,height=15,width=80) addnameinput=Entry(MBGUI,textvariable=sv,font=("Microsoft YaHei",10)) addnameinput.place(x=150,y=90,height=24,width=150) sv=StringVar() sv.set(author) addauthor=Label(MBGUI,text="作者:",font=("Microsoft YaHei",10),anchor='w') addauthor.place(x=50,y=120,height=15,width=80) addauthorinput=Entry(MBGUI,textvariable=sv,font=("Microsoft YaHei",10)) addauthorinput.place(x=150,y=120,height=24,width=150) sv=StringVar() sv.set(press) addpress=Label(MBGUI,text="出版社:",font=("Microsoft YaHei",10),anchor='w') addpress.place(x=50,y=150,height=15,width=80) addpressinput=Entry(MBGUI,textvariable=sv,font=("Microsoft YaHei",10)) addpressinput.place(x=150,y=150,height=24,width=150) addcategory=Label(MBGUI,text="类别",font=("Microsoft YaHei",10),anchor='w') addcategory.place(x=50,y=180,height=20,width=80) """addcategoryinput=Entry(CBIGUI,font=("Microsoft YaHei",10)) addcategoryinput.place(x=100,y=150,height=24,width=150)""" v=StringVar() v.set(category) w1=Radiobutton(MBGUI,text="专业书", value='专业书',variable=v) w2=Radiobutton(MBGUI,text="工具书", value='工具书',variable=v) w3=Radiobutton(MBGUI,text="报告", value='报告',variable=v) w4=Radiobutton(MBGUI,text="小说", value='小说',variable=v) w5=Radiobutton(MBGUI,text="其它", value='其它',variable=v) w1.place(x=100,y=180,height=20,width=60) w2.place(x=100,y=200,height=20,width=60) w3.place(x=95,y=220,height=20,width=60) w4.place(x=95,y=240,height=20,width=60) w5.place(x=95,y=260,height=20,width=60) adddeletebutton=Button(MBGUI,command=confirm,text="确认修改",font=("Microsoft YaHei",10)) adddeletebutton.place(x=170,y=310,height=30,width=60) addnum=Label(MBGUI,text="序号",font=("Microsoft YaHei",10),anchor='w') addnum.place(x=50,y=30,height=20,width=80) addnuminput=Entry(MBGUI,font=("Microsoft YaHei",10)) addnuminput.place(x=100,y=30,height=24,width=150) addsearchbutton=Button(MBGUI,command=click,text="查询",font=("Microsoft YaHei",10)) addsearchbutton.place(x=270,y=30,height=24,width=40) def CountName(): CNGUI=Toplevel() CNGUI.title('按书名统计书籍的数量') CNGUI.geometry('500x500') def click(): name=addnameinput.get() f=open('图书.csv','r',encoding='utf-8-sig') fl=False for line in f.readlines(): info=line[:-1].split(",") if len(info)<5: break if info[1]==name: fl=True break f.close() if fl==False: msgbox.showinfo(title='错误',message="未找到该图书信息!") CNGUI.destroy() else: addnameinput.configure(state='disabled') def treeview_sort_column(tv,col,reverse): l=[(tv.set(k,col),k) for k in tv.get_children('')] try: l.sort(key=lambda t:int(t[0]),reverse=reverse) except: l.sort(reverse=reverse) for index,(val,k) in enumerate(l): tv.move(k,'',index) tv.heading(col,command=lambda:treeview_sort_column(tv,col,not reverse)) columns=("序号","书名","作者", "出版社", "类别") tree=ttk.Treeview(CNGUI,columns=columns) tree['show']='headings' tree.place(x=20,y=50,width=460,height=400) tree.column("序号",width=30,anchor='c') tree.column("书名",width=80,anchor='c') tree.column("作者",width=80,anchor='c') tree.column("出版社",width=80,anchor='c') tree.column("类别",width=80,anchor='c') tree.heading("序号",text="序号") tree.heading("书名",text="书名") tree.heading("作者",text="作者") tree.heading("出版社",text="出版社") tree.heading("类别",text="类别") f=open('图书.csv','r',encoding='utf-8-sig') i=0 for line in f.readlines(): info=line[:-1].split(",") if info[1]==name: tree.insert("",'end',values=(info[0],info[1],info[2],info[3],info[4])) i=i+1 f.close() for col in columns: tree.heading(col,text=col,command=lambda _col=col:treeview_sort_column(tree,_col,False)) i=str(i) addcount=Label(CNGUI,text="共有"+i+"条记录",font=("Microsoft YaHei",10)) addcount.place(x=400,y=460) addname=Label(CNGUI,text="书名",font=("Microsoft YaHei",10),anchor='w') addname.place(x=20,y=20,height=20,width=80) addnameinput=Entry(CNGUI,font=("Microsoft YaHei",10)) addnameinput.place(x=100,y=20,height=24,width=150) addsearchbutton=Button(CNGUI,command=click,text="查询",font=("Microsoft YaHei",10)) addsearchbutton.place(x=270,y=20,height=24,width=40) def CountAuthor(): CAGUI=Toplevel() CAGUI.title('按作者统计书籍的数量') CAGUI.geometry('500x500') def click(): author=addauthorinput.get() f=open('图书.csv','r',encoding='utf-8-sig') fl=False for line in f.readlines(): info=line[:-1].split(",") if len(info)<5: break if info[2]==author: fl=True break f.close() if fl==False: msgbox.showinfo(title='错误',message="未找到该作者信息!") CAGUI.destroy() else: addauthorinput.configure(state='disabled') def treeview_sort_column(tv,col,reverse): l=[(tv.set(k,col),k) for k in tv.get_children('')] try: l.sort(key=lambda t:int(t[0]),reverse=reverse) except: l.sort(reverse=reverse) for index,(val,k) in enumerate(l): tv.move(k,'',index) tv.heading(col,command=lambda:treeview_sort_column(tv,col,not reverse)) columns=("序号","书名","作者", "出版社", "类别") tree=ttk.Treeview(CAGUI,columns=columns) tree['show']='headings' tree.place(x=20,y=50,width=460,height=400) tree.column("序号",width=30,anchor='c') tree.column("书名",width=80,anchor='c') tree.column("作者",width=80,anchor='c') tree.column("出版社",width=80,anchor='c') tree.column("类别",width=80,anchor='c') tree.heading("序号",text="序号") tree.heading("书名",text="书名") tree.heading("作者",text="作者") tree.heading("出版社",text="出版社") tree.heading("类别",text="类别") f=open('图书.csv','r',encoding='utf-8-sig') i=0 for line in f.readlines(): info=line[:-1].split(",") if info[2]==author: tree.insert("",'end',values=(info[0],info[1],info[2],info[3],info[4])) i=i+1 f.close() for col in columns: tree.heading(col,text=col,command=lambda _col=col:treeview_sort_column(tree,_col,False)) i=str(i) addcount=Label(CAGUI,text="共有"+i+"条记录",font=("Microsoft YaHei",10)) addcount.place(x=400,y=460) addauthor=Label(CAGUI,text="作者",font=("Microsoft YaHei",10),anchor='w') addauthor.place(x=20,y=20,height=20,width=80) addauthorinput=Entry(CAGUI,font=("Microsoft YaHei",10)) addauthorinput.place(x=100,y=20,height=24,width=150) addsearchbutton=Button(CAGUI,command=click,text="查询",font=("Microsoft YaHei",10)) addsearchbutton.place(x=270,y=20,height=24,width=40) def CountPress(): CPGUI=Toplevel() CPGUI.title('按出版社统计书籍的数量') CPGUI.geometry('500x500') def click(): press=addpressinput.get() f=open('图书.csv','r',encoding='utf-8-sig') fl=False for line in f.readlines(): info=line[:-1].split(",") if len(info)<5: break if info[3]==press: fl=True break f.close() if fl==False: msgbox.showinfo(title='错误',message="未找到该出版社信息!") CPGUI.destroy() else: addpressinput.configure(state='disabled') def treeview_sort_column(tv,col,reverse): l=[(tv.set(k,col),k) for k in tv.get_children('')] try: l.sort(key=lambda t:int(t[0]),reverse=reverse) except: l.sort(reverse=reverse) for index,(val,k) in enumerate(l): tv.move(k,'',index) tv.heading(col,command=lambda:treeview_sort_column(tv,col,not reverse)) columns=("序号","书名","作者", "出版社", "类别") tree=ttk.Treeview(CPGUI,columns=columns) tree['show']='headings' tree.place(x=20,y=50,width=460,height=400) tree.column("序号",width=30,anchor='c') tree.column("书名",width=80,anchor='c') tree.column("作者",width=80,anchor='c') tree.column("出版社",width=80,anchor='c') tree.column("类别",width=80,anchor='c') tree.heading("序号",text="序号") tree.heading("书名",text="书名") tree.heading("作者",text="作者") tree.heading("出版社",text="出版社") tree.heading("类别",text="类别") f=open('图书.csv','r',encoding='utf-8-sig') i=0 for line in f.readlines(): info=line[:-1].split(",") if info[3]==press: tree.insert("",'end',values=(info[0],info[1],info[2],info[3],info[4])) i=i+1 f.close() for col in columns: tree.heading(col,text=col,command=lambda _col=col:treeview_sort_column(tree,_col,False)) i=str(i) addcount=Label(CPGUI,text="共有"+i+"条记录",font=("Microsoft YaHei",10)) addcount.place(x=400,y=460) addpress=Label(CPGUI,text="出版社",font=("Microsoft YaHei",10),anchor='w') addpress.place(x=20,y=20,height=20,width=80) addpressinput=Entry(CPGUI,font=("Microsoft YaHei",10)) addpressinput.place(x=100,y=20,height=24,width=150) addsearchbutton=Button(CPGUI,command=click,text="查询",font=("Microsoft YaHei",10)) addsearchbutton.place(x=270,y=20,height=24,width=40) def CountCategory(): CCGUI=Toplevel() CCGUI.title('按类别统计书籍的数量') CCGUI.geometry('500x500') def click(): category=addcategoryinput.get() f=open('图书.csv','r',encoding='utf-8-sig') fl=False for line in f.readlines(): info=line[:-1].split(",") if len(info)<5: break if info[4]==category: fl=True break f.close() if fl==False: msgbox.showinfo(title='错误',message="未找到该类别信息!") CCGUI.destroy() else: addcategoryinput.configure(state='disabled') def treeview_sort_column(tv,col,reverse): l=[(tv.set(k,col),k) for k in tv.get_children('')] try: l.sort(key=lambda t:int(t[0]),reverse=reverse) except: l.sort(reverse=reverse) for index,(val,k) in enumerate(l): tv.move(k,'',index) tv.heading(col,command=lambda:treeview_sort_column(tv,col,not reverse)) columns=("序号","书名","作者", "出版社", "类别") tree=ttk.Treeview(CCGUI,columns=columns) tree['show']='headings' tree.place(x=20,y=50,width=460,height=400) tree.column("序号",width=30,anchor='c') tree.column("书名",width=80,anchor='c') tree.column("作者",width=80,anchor='c') tree.column("出版社",width=80,anchor='c') tree.column("类别",width=80,anchor='c') tree.heading("序号",text="序号") tree.heading("书名",text="书名") tree.heading("作者",text="作者") tree.heading("出版社",text="出版社") tree.heading("类别",text="类别") f=open('图书.csv','r',encoding='utf-8-sig') i=0 for line in f.readlines(): info=line[:-1].split(",") if info[4]==category: tree.insert("",'end',values=(info[0],info[1],info[2],info[3],info[4])) i=i+1 f.close() for col in columns: tree.heading(col,text=col,command=lambda _col=col:treeview_sort_column(tree,_col,False)) i=str(i) addcount=Label(CCGUI,text="共有"+i+"条记录",font=("Microsoft YaHei",10)) addcount.place(x=400,y=460) addcategory=Label(CCGUI,text="类别",font=("Microsoft YaHei",10),anchor='w') addcategory.place(x=20,y=20,height=20,width=80) addcategoryinput=Entry(CCGUI,font=("Microsoft YaHei",10)) addcategoryinput.place(x=100,y=20,height=24,width=150) addsearchbutton=Button(CCGUI,command=click,text="查询",font=("Microsoft YaHei",10)) addsearchbutton.place(x=270,y=20,height=24,width=40) def Root(): root.title('个人书籍管理系统') root['width']=800 root['height']=600 menu=Menu(root) submenu=Menu(menu,tearoff=0) submenu.add_command(label="录入书籍信息",command=CreateBookInfo) #submenu.add_command(label="保存书籍信息") submenu.add_command(label="浏览书籍信息",command=Table) #submenu.add_command(label="查询书籍信息") submenu.add_command(label="删除书籍信息",command=DeleteBook) submenu.add_command(label="修改书籍信息",command=ModifyBook) menu.add_cascade(label="基本功能",menu=submenu) submenu=Menu(menu,tearoff=0) submenu.add_command(label="按书名统计书籍的数量",command=CountName) submenu.add_command(label="按作者统计书籍的数量",command=CountAuthor) submenu.add_command(label="按出版社统计书籍的数量",command=CountPress) submenu.add_command(label="按类别统计书籍的数量",command=CountCategory) #submenu.add_command(label="按书名对书籍进行排序") menu.add_cascade(label="高级需求",menu=submenu) submenu=Menu(menu,tearoff=0) submenu.add_command(label="帮助",command=Help) menu.add_cascade(label="工具",menu=submenu) root.config(menu=menu) addLabel=Label(root,text="点击“工具”->“帮助”即可查看本系统的相关操作",font=("Microsoft YaHei",10)) addLabel.place(x=20,y=560) root.protocol("WM_DELETE_WINDOW",Close) def Table(): def treeview_sort_column(tv,col,reverse): l=[(tv.set(k,col),k) for k in tv.get_children('')] try: l.sort(key=lambda t:int(t[0]),reverse=reverse) except: l.sort(reverse=reverse) for index,(val,k) in enumerate(l): tv.move(k,'',index) tv.heading(col,command=lambda:treeview_sort_column(tv,col,not reverse)) columns=("序号","书名","作者", "出版社", "类别") tree=ttk.Treeview(root,columns=columns) tree['show']='headings' tree.place(x=10,y=10,width=780,height=550) tree.column("序号",width=80,anchor='c') tree.column("书名",width=150,anchor='c') tree.column("作者",width=150,anchor='c') tree.column("出版社",width=150,anchor='c') tree.column("类别",width=150,anchor='c') tree.heading("序号",text="序号") tree.heading("书名",text="书名") tree.heading("作者",text="作者") tree.heading("出版社",text="出版社") tree.heading("类别",text="类别") f=open('图书.csv','r',encoding='utf-8-sig') for line in f.readlines(): info=line[:-1].split(",") tree.insert("",'end',values=(info[0],info[1],info[2],info[3],info[4])) f.close() for col in columns: tree.heading(col,text=col,command=lambda _col=col:treeview_sort_column(tree,_col,False)) if __name__ == '__main__': root=Tk() Root() open('图书.csv','a',encoding='utf-8-sig') Table() root.mainloop()