python实现图书管理系统
本文实例为大家分享了python实现图书管理系统的具体代码,供大家参考,具体内容如下
importmysql.connector
importsys,os
importtime
importdatetime
fromtkinterimport*
fromtkinter.messageboximport*
classLibop:
user='root'
pwd=''
host='localhost'
db='library'
data_file='mysql-test.dat'
def__init__(self):
print("init")
try:
self.library=mysql.connector.connect(user=Libop.user,password=Libop.pwd,host=Libop.host,database=Libop.db)
self.cursor=self.library.cursor()
print("Connectsuccessfully")
exceptmysql.connector.Erroraserr:
print("WTF!initialwrong")
print("Error:{}".format(err.msg))
sys.exit()
defselect(self,str):
try:
self.cursor.execute(str)
returnself.cursor.fetchall()
exceptmysql.connector.Erroraserr:
print("WTF!selectwrong")
print("Error:{}".format(err.msg))
print(str)
showinfo("ERROR","Pleaseinputtheparametercorrectly")
defupdate(self,str):
try:
self.cursor.execute(str)
self.library.commit()
return1
exceptmysql.connector.Erroraserr:
print("WTF!updatewrong")
print("Error:{}".format(err.msg))
return0
defdelete(self,str):
try:
self.cursor.execute(str)
self.library.commit()
exceptmysql.connector.Erroraserr:
print("WTF!deletewrong")
print("Error:{}".format(err.msg))
definsert(self,str):
try:
self.cursor.execute(str)
self.library.commit()
return1
exceptmysql.connector.Erroraserr:
print("WTF!insertwrong")
print("Error:{}".format(err.msg))
return0
LIB=Libop()
root=Tk()
root.wm_title("LibrarySystem")
str1=str2=''
book_type=name=year=publisher=writer=price1=price2=order=''
cardid=''
defcall_rent(en1,en2,brok):
st1=en1.get()
st2=en2.get()
rent(st1,st2,brok)
defcall_return(en1,en2):
st1=en1.get()
st2=en2.get()
back(st1,st2)
deflogin():
str1=ide.get()
str2=pwde.get()
curs=LIB.select("selectmanage_id,pswdfrommanagerswheremanage_id='{}'andpswd='{}'".format(str1,str2))
if((str1,str2)incurs):
showinfo('message','loginsuccess')
label_book_id=Label(root,text='BookID:').grid(row=0,column=4,sticky=W)
label_card_id=Label(root,text='CardID:').grid(row=1,column=4,sticky=W)
entry_book_id=Entry(root)
entry_book_id.grid(row=0,column=5,sticky=W)
entry_card_id=Entry(root)
entry_card_id.grid(row=1,column=5,sticky=W)
entry_cardid=Entry(root)
entry_cardid.grid(row=5,column=0,sticky=W)
#buttons
button_insert=Button(root,text='insertbook',command=inbook)
button_insert.grid(row=0,column=2,sticky=W)
button_return=Button(root,text='rentbook')
button_return.bind("",lambdaz:call_rent(entry_book_id,entry_card_id,str1))
button_return.grid(row=0,column=3,sticky=W)
button_rent=Button(root,text='returnbook')
button_rent.bind("",lambdal:call_return(entry_book_id,entry_card_id))
button_rent.grid(row=1,column=2,sticky=W)
button_delete=Button(root,text='deletecard')
button_delete.bind("",lambdaj:deletecard(entry_cardid))
button_delete.grid(row=5,column=2,sticky=W)
button_add=Button(root,text='addcard',command=addwindow)
button_add.grid(row=5,column=3,sticky=W)
button_display=Button(root,text='displaybooks')
button_display.bind("",lambday:display_rent(entry_cardid))
button_display.grid(row=5,column=1,sticky=W)
else:
showinfo(title='loginfailure',message="WRONGIDORPASSWORD!")
#addacard
defaddwindow():
top=Toplevel(root)
label_card_id=Label(top,text='cardid:').grid(row=0,column=0,sticky=W)
label_name=Label(top,text='Name:').grid(row=1,column=0,sticky=W)
label_unit=Label(top,text='Units:').grid(row=2,column=0,sticky=W)
label_type=Label(top,text='type:').grid(row=3,column=0,sticky=W)
entry_card_id=Entry(top)
entry_card_id.grid(row=0,column=1,sticky=W)
entry_name=Entry(top)
entry_name.grid(row=1,column=1,sticky=W)
entry_unit=Entry(top)
entry_unit.grid(row=2,column=1,sticky=W)
entry_type=Entry(top)
entry_type.grid(row=3,column=1,sticky=W)
button_constructe=Button(top,text='Add')
button_constructe.bind("",lambdaq:addcard(entry_card_id,entry_name,entry_unit,entry_type))
button_constructe.grid(row=4)
defaddcard(en1,en2,en3,en4):
cardid=en1.get()
name=en2.get()
unit=en3.get()
c_type=en4.get()
select="selectcard_idfromlib_cardwherecard_id='{}'".format(cardid)
if(c_typenotin('T','S','O')):
showinfo('error',"NOSUCHTYPE")
return0
line=LIB.select(select)
if(len(line)!=0):
showinfo('additionfailure',"AlreadyhavethisID!")
else:
insert="insertintolib_cardvalues('{}','{}','{}','{}')".format(cardid,name,unit,c_type)
LIB.insert(insert)
showinfo('ok','additionsuccess')
return1
#insertbook
definbook():
insert_book=Toplevel(root)
insert_book.title('insertbook')
text_insert=Text(insert_book)
text_insert.pack()
button_confirm=Button(insert_book,text='confirm')
button_confirm.bind("",lambdax:booksin(text_insert))
button_confirm.pack()
defbooksin(text_insert):
string=text_insert.get('0.0',END)
string=string.split('\n')
print(string)
forlineinstring:
ifline=='':
break
lines=line.split(',')
bookid=lines[0][1:]
t=lines[1]
name=lines[2]
publisher=lines[3]
year=lines[4]
writer=lines[5]
price=lines[6]
total=lines[7]
stock=lines[8][:-1]
if(int(stock)>int(total)orint(price)<0):
showinfo('inserterror!','stockwrongorpriceisbelow0!')
continue
#print("insertintobookvalues('{}','{}','{}','{}',{},'{}',{},{},{})".format(bookid,t,name,publisher,year,writer,price,total,stock))
t=LIB.insert("insertintobookvalues('{}','{}','{}','{}',{},'{}',{},{},{})".format(bookid,t,name,publisher,year,writer,price,total,stock))
if(t==0):
showinfo('error','book{}hasbeeninserted'.format(bookid))
showinfo('success','inserthasbeendone')
#originalwindow
label_type=Label(root,text='TYPE').grid(row=3,sticky=W,column=0)
label_name=Label(root,text='NAME').grid(row=3,sticky=W,column=1)
label_year=Label(root,text='YEAR(XXXX-XXXX)').grid(row=3,sticky=W,column=2)
label_publisher=Label(root,text='PUBLISHER').grid(row=3,sticky=W,column=3)
label_writer=Label(root,text='WRITER').grid(row=3,sticky=W,column=4)
label_lowprice=Label(root,text='lowestprice').grid(row=3,sticky=W,column=5)
label_highprice=Label(root,text='highestprice').grid(row=3,sticky=W,column=6)
label_order=Label(root,text='orderby').grid(row=0,sticky=W,column=6)
entry_type=Entry(root)
entry_type.grid(row=4,column=0,sticky=W)
entry_name=Entry(root)
entry_name.grid(row=4,column=1,sticky=W)
entry_year=Entry(root)
entry_year.grid(row=4,column=2,sticky=W)
entry_publisher=Entry(root)
entry_publisher.grid(row=4,column=3,sticky=W)
entry_writer=Entry(root)
entry_writer.grid(row=4,column=4,sticky=W)
entry_price1=Entry(root)
entry_price1.grid(row=4,column=5,sticky=W)
entry_price2=Entry(root)
entry_price2.grid(row=4,column=6,sticky=W)
#ordertheresult
order_index=0
Lb_order=Listbox(root,selectmode=SINGLE,height=6)
order='name','book_type','T','writer','price','publisher'
Lb_order.insert(0,order[0])
Lb_order.insert(1,"type")
Lb_order.insert(2,"year")
Lb_order.insert(3,order[3])
Lb_order.insert(4,order[4])
Lb_order.insert(5,order[5])
Lb_order.grid(row=0,column=7,sticky=W)
Lb_order.selection_set(0)
defbooksearch():
select="selectBookID,book_type,name,publisher,T,writer,price,total,stockfrombook"
order_index=Lb_order.curselection()
if(len(order_index)!=0):
order_index=order_index[0]
else:
order_index=0;
orders=order[order_index]
name=entry_name.get()
book_type=entry_type.get()
publisher=entry_publisher.get()
year=entry_year.get()
writer=entry_writer.get()
price1=entry_price1.get()
price2=entry_price2.get()
year1=year[0:4]
year2=year[5:9]
count=0
ifname!='':
ifcount!=0:
select+="and"
else:select+="where"
count+=1
select+="name='{}'".format(name)
ifbook_type!='':
ifcount!=0:
select+="and"
else:select+="where"
count+=1
select+="book_type='{}'".format(book_type)
ifpublisher!='':
ifcount!=0:
select+="and"
else:select+="where"
count+=1
select+="publisher='{}'".format(publisher)
ifyear1!='':
ifyear2=='':
ifcount!=0:
select+="and"
else:select+="where"
count+=1
select+="t={}".format(year1)
else:
ifcount!=0:
select+="and"
else:select+="where"
count+=1
select+="Tbetween{}and{}".format(year1,year2)
ifwriter!='':
ifcount!=0:
select+="and"
else:select+="where"
count+=1
select+="writer='{}'".format(writer)
ifprice1!='':
ifprice2!='':
ifcount!=0:
select+="and"
else:select+="where"
count+=1
select+="pricebetween{}and{}".format(price1,price2)
else:
ifcount!=0:
selece+="and"
else:select+="where"
count+=1
select+="price={}".format(price1)
iforders=='':
select+="orderbyname"
else:
select+="orderby{}".format(orders)
print(select)
curs=LIB.select(select)
searchresult=Toplevel(root)
searchresult.title('result')
text=Text(searchresult)
text.insert(INSERT,"BookID\ttype\tname\tpublish\tyear\twriter\tprice\ttotal\tstock\n")
for(BookIDs,book_types,names,publishers,years,writers,prices,totals,stocks)incurs:
text.insert(INSERT,"{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\n".format(BookIDs,book_types,names,publishers,years,writers,prices,totals,stocks))
text.pack()
defdisplay_rent(en_cardid):
cardid=en_cardid.get()
select="selectcard_idfromlib_cardwherecard_id='{}'".format(cardid)
curs=LIB.select(select)
order_index=Lb_order.curselection()
iflen(order_index)!=0:
order_index=order_index[0]
else:
order_index=0;
orders=order[order_index]
if((cardid,)incurs):
select="selectcard_id,name,unit,c_typefromlib_cardwherecard_id='{}'".format(cardid)
curs=LIB.select(select)
top=Toplevel(root)
text=Text(top)
for(card_id,name,unit,c_type)incurs:
text.insert(INSERT,"card_id:{}\n".format(card_id))
text.insert(INSERT,"name:{}\n".format(name))
text.insert(INSERT,"unit:{}\n".format(unit))
text.insert(INSERT,"c_type:{}\n".format(c_type))
select="selectBookID,book_type,name,publisher,t,writer,price,total,stockfrombooknaturaljoinrecordwherecard_id='{}'andstill=1orderby{}".format(cardid,orders)
curs=LIB.select(select)
text.insert(INSERT,"BookID\ttype\tname\tpublish\tyear\twriter\tprice\ttotal\tstock\n")
for(BookID,book_type,name,publisher,t,writer,price,total,stock)incurs:
text.insert(INSERT,"{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\n".format(BookID,book_type,name,publisher,t,writer,price,total,stock))
text.pack()
else:
showinfo('Error',"NOSUCHCARD")
defcheckcard(cardid):
select="selectcard_idfromlib_cardwherecard_id='{}'".format(cardid)
curs=LIB.select(select)
if(cardid,)incurs:
return1
else:
return0
defrent(BookID,cardid,brokerage):
ifnot(checkcard(cardid)):
showinfo('error','nosuchcard')
return0
select="selectstockfrombookwhereBookID='{}'".format(BookID)
stocks=LIB.select(select)
#stocks=curs.fetchall()
stock=stocks[0]
if(stock>(0,)):
update="updatebooksetstock=stock-1whereBookID='{}'".format(BookID)
flag=LIB.update(update)
ifflag:
insert="insertintorecordvalues('','{}','{}',CURDATE(),DATE_ADD(CURDATE(),INTERVAL1MONTH),'{}',1)".format(cardid,BookID,brokerage)
LIB.insert(insert)
showinfo('rentsuccess','OK')
else:
showinfo('rentfailure',"stockwrong")
else:
select="selectreturn_timefromrecordwhereBookID='{}'andreturn_time>=all(selectreturn_timefromrecordwhereBookID='{}')".format(BookID,BookID)
date=LIB.select(select)
#date=curs.fetchall()
date=str(date)
showinfo('Outofstock',"Therearenomoresuchbook,Therecentreturningwillbeat:{}-{}-{}".format(date[16:20],date[22:23],date[26:27]))
return1
defback(BookID,cardid):
ifnot(checkcard(cardid)):return0
select="selectBookIDfromrecordwhereBookID='{}'andcard_id='{}'andstill=1".format(BookID,cardid)
curs=LIB.select(select)
if(BookID,)incurs:
select="selectrecord_idfromrecordwhereBookID='{}'andcard_id='{}'andstill=1orderbyrent_time".format(BookID,cardid)
cur=LIB.select(select)
#cur=curss.fetchall()
forrecordincur:
recordid=str(record)
recordid=recordid[1:]
recordid=recordid[:-2]
update="updaterecordsetstill=0whererecord_id='{}'".format(recordid)
LIB.update(update)
update="updatebooksetstock=stock+1whereBookID='{}'".format(BookID)
LIB.update(update)
break
showinfo('message',"ReturnSuccess!")
return1
else:
showinfo('error',"Youcan'treturnthebook!")
return1
defdeletecard(card):
cardid=card.get()
ifnot(checkcard(cardid)):
showinfo('error',"NOSUCHCARD")
return0
select="selectrecord_idfromrecordwherecard_id='{}'andstill=1".format(cardid)
curs=LIB.select(select)
forrecord_idincurs:
showinfo('error',"Somebooksthiscardrentedhaven'tbeenreturned")
return0
delete="deletefromlib_cardwherecard_id='{}'".format(cardid)
LIB.delete(delete)
showinfo('ok','success')
return1
label_id=Label(root,text='ID:')
label_id.grid(row=0,sticky=W)
ide=Entry(root)
ide.grid(row=0,column=1,sticky=E)
label_psw=Label(root,text='PassWord:').grid(row=1,sticky=W)
pwde=Entry(root,show='*')
pwde.grid(row=1,column=1,sticky=E)
log_button=Button(root,text='Login',command=login)
log_button.grid(row=2,column=1)
button_search=Button(root,text='SearchBook',command=booksearch)
button_search.grid(row=4,column=7,sticky=W)
print("alldone")
root.mainloop()
更多学习资料请关注专题《管理系统开发》。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。