阅读量:1
该项目主要功能,图书的增删查改,借阅证的增、删;借阅证用于结借书管理;借阅证分为老师和学生,numbers为借阅状态:
项目使用mysql数据库,表为3张:
先看运行主界面及功能:
主页面实现代码:
initwin(): #主窗口 def __init__(self,master): global im global image self.master=master self.master.config(bg='Magenta') self.initwin=tk.Frame(self.master,) self.initwin.pack() self.canvas = tk.Canvas(self.initwin, width = 1000, height = 700, bg = 'green') image=Image.open('bg.jpg') im=ImageTk.PhotoImage(image) self.canvas.create_image(0,0,anchor='nw',image = im) # 使用create_image将图片添加到Canvas组件中 self.canvas.pack() # 将Canvas添加到主窗口 btn_add=ttk.Button(self.initwin,text="图书入库或修改信息",command=self.add) btn_query=ttk.Button(self.initwin,text="查询图书信息",command=self.query) btn_card=ttk.Button(self.initwin,text="借书证管理",command=self.card) btn_borrow=ttk.Button(self.initwin,text="借书",command=self.borrow) btn_return=ttk.Button(self.initwin,text="还书",command=self.returnbook)#return是保留字。。。 self.canvas.create_window(100,50,width=200,height=40,window=btn_add) self.canvas.create_window(100,100,width=200,height=40,window=btn_query) self.canvas.create_window(100,250,width=200,height=40,window=btn_card) self.canvas.create_window(100,150,width=200,height=40,window=btn_borrow) self.canvas.create_window(100,200,width=200,height=40,window=btn_return) self.title=tk.Label(self.initwin,text="图书管理系统",font=("宋体",30)) self.author=tk.Label(self.initwin,text="源代码+QQ:3125841747",font=("宋体",24)) self.canvas.create_window(400,150,anchor="nw",width=300,height=70,window=self.title) self.canvas.create_window(400,350,anchor="nw",window=self.author) def add(self,): #add窗口,实现的功能:选择批量入库或者单本入库,添加或是修改 self.initwin.destroy() addwin(self.master) def query(self): self.initwin.destroy() querywin(self.master) def card(self): self.initwin.destroy() cardwin(self.master) def borrow(self): self.initwin.destroy() borrowwin(self.master) def returnbook(self): self.initwin.destroy() returnwin(self.master)
图书入库:分为单本入库和读取记事本批量入库:
单本入库:实现对图书的增、改:
批量入库成功后,会在文件夹中生成日志文件:
入库图书代码实现:
addwin(): def __init__(self,master): global im global image self.master=master self.master.config(bg='GhostWhite') self.str="此处输出状态信息" self.addwin=tk.Frame(self.master,) self.addwin.pack() self.canvas = tk.Canvas(self.addwin, width = 1000, height = 700, bg = 'white') image=Image.open('bg.jpg') im=ImageTk.PhotoImage(image) self.canvas.create_image(0,0,anchor='nw',image = im) # 使用create_image将图片添加到Canvas组件中 self.canvas.pack() # 将Canvas添加到窗口 self.btn_back=ttk.Button(self.addwin,text="返回主菜单",command=self.back) #btn_back.pack() self.canvas.create_window(100,50,width=200,height=40,window=self.btn_back) self.mode=tk.IntVar() #选择入库模式(单本或者批量) self.style_radio=ttk.Style() self.style_radio.configure("radio.TRadiobutton",font=("微软雅黑",12),background="Aqua") self.r1=ttk.Radiobutton(self.addwin,text="单本入库(或修改信息)",variable=self.mode,value=1,command=self.display1,style="radio.TRadiobutton") self.r2=ttk.Radiobutton(self.addwin,text="批量入库",variable=self.mode,value=2,command=self.display2,style="radio.TRadiobutton") self.canvas.create_window(400,50,window=self.r1) self.canvas.create_window(400,100,window=self.r2) self.style_label=ttk.Style() self.style_label.configure("label.TLabel",font=("微软雅黑",14)) self.s_title=ttk.Label(self.addwin,text="状态栏",style="label.TLabel", width=30,anchor="center") self.state=tk.Message(self.addwin,text=self.str,width=300,font=("微软雅黑",12)) self.canvas.create_window(800,100,height=50,window=self.s_title) self.canvas.create_window(650,150,anchor="nw",width=300,window=self.state) self.flag1=0#标记“批量入库”的控件是否存在的flag self.flag2=0#标记“单本入库”的控件是否存在的flag self.cflag=0#change flag,用来区分是添加一本书还是修改书的信息.为1时代表修改
查询图书可以分为:类别、书名、出版社、年份、作者、价格这几种进行查询:
按类别查询:
按书名查询:
按出版社查询:
其他功能不一一演示,
查询图书的代码:
ef show(self,result): #美观方面的问题还需要改进。。。现在先大体实现功能 #重构此部分,由label改为ttk的Treeview self.query_list=ttk.Treeview(self.querywin,columns=["书号","类别","书名","出版社","年份","作者","价格","库存数"],show="headings")#show设为headings可以隐藏首列 self.query_list.column("书号",width=100) self.query_list.column("类别",width=150) self.query_list.column("书名",width=200) self.query_list.column("出版社",width=150) self.query_list.column("年份",width=50) self.query_list.column("作者",width=100) self.query_list.column("价格",width=80) self.query_list.column("库存数",width=80) self.query_list.heading("书号",text="书号",) self.query_list.heading("类别",text="类别",) self.query_list.heading("书名",text="书名",) self.query_list.heading("出版社",text="出版社") self.query_list.heading("年份",text="年份") self.query_list.heading("作者",text="作者") self.query_list.heading("价格",text="价格") self.query_list.heading("库存数",text="库存数") self.canvas.create_window(50,250,height=300,anchor="nw",window=self.query_list) self.query_bar=ttk.Scrollbar(self.querywin,orient='vertical',command=self.query_list.yview) self.query_list.configure(yscrollcommand=self.query_bar.set) self.canvas.create_window(960,250,height=300,anchor="nw",window=self.query_bar) self.btn_list=ttk.Button(self.querywin,text="返回",command=self.destroy_search) self.canvas.create_window(480,580,height=30,width=50,window=self.btn_list) if type(result)!=type(()): return i=0#临时变量 for item in result: self.bnum_str=item[0] self.class_str=item[1] self.bname_str=item[2] self.pub_str=item[3] self.year_str=item[4] self.auth_str=item[5] self.pri_str=item[6] self.coll_str=item[7] self.query_list.insert("",i,values=(item[0],item[1],item[2],item[3],item[4],item[5],item[6],item[7])) i+=1
借书需要有借阅证,借阅证添加操作:
拥有借阅的证号,才可以借书:
还书 :
借书代码实现如下:
程序完整代码看主页联系
或访问:
腾讯文档腾讯文档-在线文档https://docs.qq.com/doc/p/71239d69a76f56cf1521717ae6b22c27cf876f10
import pymysql import logging import datetime def ShowList(conn,CardNum): if(CardNum==""): return "借书证号不能为空" try: cursor=conn.cursor() sql="select * from card where cnum=%s" cursor.execute(sql,CardNum) result=cursor.fetchone() #要看输入的卡号是不是有效的 if result is None: return "无效的借书证号" sql="select bnum,bname,btime,rtime,times from book natural join borrow where cnum=%s" cursor.execute(sql,CardNum) result=cursor.fetchall() return result except Exception as e: logging.exception(e) return e def Borrow(conn,CardNum,BookNum): if(CardNum=="" or BookNum==""): return "不能为空" try: cursor=conn.cursor() sql="select times,rtime from borrow where cnum=%s and bnum=%s" cursor.execute(sql,(CardNum,BookNum)) times=cursor.fetchone() if times is None: pass elif times[0]==1: r_time=times[1] r_time=r_time+datetime.timedelta(days=30) r_time=r_time.strftime("%Y-%m-%d %H:%M:%S") sql="update borrow set times=0,rtime=%s where cnum=%s and bnum=%s" cursor.execute(sql,(r_time,CardNum,BookNum)) conn.commit() return (1,r_time) elif times[0]==0: return "续借次数已达上限" #前面要判断是否是续借,允许续借一次 #判断完不是续借之后,还要判断是否借书数量已经达到上限 sql="select numbers from card where cnum=%s" cursor.execute(sql,CardNum) result=cursor.fetchone() if result[0]>=10: return "借书数量超出上限" else: numbers=result[0]+1 # sql="select bname,collection from book where bnum=%s" cursor.execute(sql,BookNum) result=cursor.fetchone() #此处的判断逻辑其实不是很好。。。先判断了是否超出上限,再判断的书是否存在。可能会导致用户为了借一本不存在的书,先去还书,回来才发现根本没有这本书 if result is None: return "借书失败,书号不存在" if result[1]!=0: #借书成功 bname=result[0] collection=result[1]-1 sql="update book set collection=%s where bnum=%s" cursor.execute(sql,(collection,BookNum)) b_time= datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") r_time=(datetime.datetime.now()+datetime.timedelta(days=30)).strftime("%Y-%m-%d %H:%M:%S") sql="insert into borrow values(%s,%s,%s,%s,1)" cursor.execute(sql,(CardNum,BookNum,b_time,r_time)) sql="update card set numbers=%s where cnum=%s" cursor.execute(sql,(numbers,CardNum)) conn.commit() return (0,r_time,bname) else: sql="select min(rtime) from borrow where bnum=%s" cursor.execute(sql,BookNum) result=cursor.fetchone() r_time=result[0] r_time=r_time.strftime('%Y-%m-%d %H:%M:%S') return (2,r_time) except Exception as e: logging.exception(e) conn.rollback() return e