You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
system/书籍管理系统.py

878 lines
37 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import tkinter
import xlrd
import xlwt
import os
from tkinter import *
from xlutils import copy
from tkinter import messagebox
import datedays
#创建窗口
def createwin():
window=Tk()
window.title('书籍管理系统')
window.geometry('350x800')
button1 = tkinter.Button(window, text='退出', command=lambda: window.destroy(), width=7, height=2)
button1.place(relx=1, rely=1, anchor='se')
button2 = tkinter.Button(window, width=22, height=1, command=lambda: createentry(), text='添加书籍');
button2.place(x=100, y=200)
button3 = tkinter.Button(window, width=22, height=1, command=lambda: addUser(), text='添加用户');
button3.place(x=100, y=250)
button4 = tkinter.Button(window, width=22, height=1, command=lambda: modifybook(), text='修改书籍');
button4.place(x=100, y=300)
button5 = tkinter.Button(window, width=22, height=1, command=lambda: modifyuser(), text='修改用户');
button5.place(x=100, y=350)
button6 = tkinter.Button(window, width=22, height=1, command=lambda: lookbook(), text='查看书籍信息');
button6.place(x=100, y=100)
button7 = tkinter.Button(window, width=22, height=1, command=lambda: rookbook(), text='查看用户信息');
button7.place(x=100, y=150)
button8 = tkinter.Button(window, width=22, height=1, command=lambda: allbook(), text='超时未还书借书者信息');
button8.place(x=100, y=400)
button9 = tkinter.Button(window, width=22, height=1, command=lambda: searchbook(), text='查询书籍信息');
button9.place(x=100, y=450)
button10 = tkinter.Button(window, width=22, height=1, command=lambda: checkhistory(), text='查询用户信息');
button10.place(x=100, y=500)
button11 = tkinter.Button(window, width=22, height=1, command=lambda: borrowbook(), text='借书');
button11.place(x=100, y=550)
button12 = tkinter.Button(window, width=22, height=1, command=lambda: returnbook(), text='还书');
button12.place(x=100, y=600)
button13 = tkinter.Button(window, width=22, height=1, command=lambda: deletebook(), text='删除书籍');
button13.place(x=100, y=650)
button14 = tkinter.Button(window, width=22, height=1, command=lambda: deleteuser(), text='删除用户');
button14.place(x=100, y=700)
return window
#查看书籍信息
def lookbook():
lookbook=Tk()
lookbook.title('书籍信息')
lookbook.geometry('1600x500')
roll=tkinter.Scrollbar(lookbook)
roll.pack(side=tkinter.RIGHT,fill=tkinter.Y)
text_input=tkinter.Text(lookbook,width=800,height=500)
text_input.insert('insert',getvar1())
text_input.pack(expand=tkinter.YES,fill=tkinter.BOTH)
text_input.config(yscrollcommand=roll.set)
roll.config(command=text_input.yview)
#获取书籍信息表格中的内容
def getvar1():
xls=xlrd.open_workbook('书籍信息.xls')
sheet=xls.sheet_by_index(0)
table = xls.sheets()[0]
n=table.nrows
listindes=""
for i in range(0,n):
for j in range(0,7):
listindes=listindes+str(sheet.cell_value(i,j))+"\t\t\t\t"
listindes=listindes+"\n"
return listindes
#查看借书者信息
def rookbook():
rookbook=Tk()
rookbook.title('用户信息')
rookbook.geometry('700x500')
roll=tkinter.Scrollbar(rookbook)
roll.pack(side=tkinter.RIGHT,fill=tkinter.Y)
text_input=tkinter.Text(rookbook,width=800,height=500)
text_input.insert('insert',showUsers())
text_input.pack(expand=tkinter.YES,fill=tkinter.BOTH)
text_input.config(yscrollcommand=roll.set)
roll.config(command=text_input.yview)
# 显示所有借书者信息
def showUsers():
# 获取所有用户信息Excel文件的文件名
path = '用户信息\\'
files = os.listdir(path)
users = []
for file in files:
if file.endswith('.xls'):
users.append(file)
st = "姓名\t\t\t性别\t\t\t年龄\t\t\t借书账号\t\t\t"
for user in users:
s = path + user
book = xlrd.open_workbook(s)
sheet = book.sheet_by_index(0)
st=st+"\n"
for j in range(sheet.ncols-5):
st = st+str(sheet.cell_value(1, j)) + "\t\t\t"
return st
# 统计超过期限未还书的借书者并输出借书者信息
def overdueUsers():
date = datedays.getnow()
# 获取所有用户信息Excel文件的文件名
path = '用户信息\\'
files = os.listdir(path)
users = []
for file in files:
if file.endswith('.xls'):
users.append(file)
st = "姓名\t\t\t性别\t\t\t年龄\t\t\t借书账号\t\t\t借书日期\t\t\t书籍编号\t\t\t借书书名\t\t\t应还书日期\t\t\t是否已还书"
st = st + "\n"
for user in users:
s = path + user
book = xlrd.open_workbook(s)
sheet = book.sheet_by_index(0)
t=0
for i in range(1, sheet.nrows):
return_date = str(sheet.cell_value(i, 7))
status = sheet.cell_value(i, 8)
if status == '' and return_date < date:
if t==0:
t=1
for k in range(0,4):
st=st+str(sheet.cell_value(1,k))+"\t\t\t"
for k in range(4,9):
st=st+str(sheet.cell_value(i,k))+"\t\t\t"
else:
for j in range(sheet.ncols):
st = st + str(sheet.cell_value(i, j)) + "\t\t\t"
st = st + "\n"
return st
#输出超过期限未还书的借书者信息
def allbook():
allbook=Tk()
allbook.title('超时未还书借书者信息')
allbook.geometry('1600x500')
roll=tkinter.Scrollbar(allbook)
roll.pack(side=tkinter.RIGHT,fill=tkinter.Y)
text_input=tkinter.Text(allbook,width=1000,height=500)
text_input.insert('insert',overdueUsers())
text_input.pack(expand=tkinter.YES,fill=tkinter.BOTH)
text_input.config(yscrollcommand=roll.set)
roll.config(command=text_input.yview)
#创建新用户表格,表格以借书账号命名,添加用户信息
def addUser():
#创建用于输入添加的用户的信息的窗口
addusers = Tk()
addusers.title('添加用户')
addusers.geometry('275x250')
button1 = tkinter.Button(addusers, text='退出', command=lambda: addusers.destroy(), width=4, height=1)
button1.place(relx=1, rely=1, anchor='se')
button2 = tkinter.Button(addusers, width=8, height=1,command=lambda: getentry2(addusers, entry1, entry2, entry3, entry4), text='确认');
button2.place(x=100, y=175)
text1 = tkinter.Label(addusers, bd=4, fg='black', text='姓名:')
text1.place(x=20, y=45)
text2 = tkinter.Label(addusers, bd=4, fg='black', text='性别:')
text2.place(x=20, y=70)
text3 = tkinter.Label(addusers, bd=4, fg='black', text='年龄:')
text3.place(x=20, y=95)
text4 = tkinter.Label(addusers, bd=4, fg='black', text='借书账号:')
text4.place(x=20, y=120)
entry1 = tkinter.Entry(addusers, width=20, show='')
entry1.place(x=100, y=50)
entry2 = tkinter.Entry(addusers, width=20, show='')
entry2.place(x=100, y=75)
entry3 = tkinter.Entry(addusers, width=20, show='')
entry3.place(x=100, y=100)
entry4 = tkinter.Entry(addusers, width=20, show='')
entry4.place(x=100, y=125)
def getentry2(addusers, entry1, entry2, entry3, entry4):
#判断输入是否完整,避免保存错误信息
if entry1.get()=="" or entry2.get()=="" or entry3.get()=="" or entry4.get()=="":
tkinter.messagebox.showerror('错误', '请输入完整信息!')
return
data = []
data.append(str(entry1.get()))
data.append(str(entry2.get()))
data.append(str(entry3.get()))
data.append(str(entry4.get()))
for i in range(0,5):
data.append("")
s = '用户信息' + '\\' + data[3] + '.xls'
#判断是否已有用户,避免重复添加用户造成报错
if not os.path.exists(s):
book = xlwt.Workbook(encoding='utf-8', style_compression=0)
sheet = book.add_sheet('用户管理系统', cell_overwrite_ok=True)
col = ('姓名', '性别', '年龄', '借书账号', '借书日期', '书籍编号', '借书书名', '应还书日期', '是否已还书')
for i in range(0, 9):
sheet.write(0, i, col[i])
book.save(s)
xls = xlrd.open_workbook(s)
wbook = copy.copy(xls)
wsheet = wbook.get_sheet(0)
table = xls.sheets()[0]
n = table.nrows
for i in range(len(data)):
wsheet.write(n, i, data[i])
wbook.save(s)
tkinter.messagebox.showinfo('提示', '添加成功!')
else:
tkinter.messagebox.showerror('错误', '添加失败,用户已存在!')
addusers.destroy()
#添加书籍
def getentry(addwin,entry1,entry2,entry3,entry4,entry5,entry6,entry7):
#判断输入框是否输入完整,防止存到错误信息
if entry1.get()=="" or entry2.get()=="" or entry3.get()=="" or entry4.get()=="" or entry5.get()=="" or entry6.get()=="" or entry7.get()=="":
tkinter.messagebox.showerror('错误', '请输入完整信息!')
return
xls = xlrd.open_workbook('书籍信息.xls')
wbook = copy.copy(xls)
wsheet = wbook.get_sheet(0)
sheet = xls.sheet_by_index(0)
table = xls.sheets()[0]
n = table.nrows
t=0
#避免书籍信息重复添加
for i in range(0, n):
if entry5.get()==sheet.cell_value(i,4):
t=1
tkinter.messagebox.showerror('错误', '添加失败,已有书籍信息!')
break
#添加书籍信息,写入表格
if t==0:
data=[]
data.append(str(entry1.get()))
data.append(str(entry2.get()))
data.append(str(entry3.get()))
data.append(str(entry4.get()))
data.append(str(entry5.get()))
data.append(str(entry6.get()))
data.append(str(entry7.get()))
n = table.nrows
for i in range(len(data)):
wsheet.write(n, i, data[i])
wbook.save('书籍信息.xls')
tkinter.messagebox.showinfo('提示', '添加成功!')
#关闭窗口
addwin.destroy()
def createentry():
#创建输入需要添加的书籍的信息的窗口
addwin=Tk()
addwin.title('添加书籍')
addwin.geometry('275x300')
button1 = tkinter.Button(addwin, text='退出', command=lambda: addwin.destroy(), width=4, height=1)
button1.place(relx=1, rely=1, anchor='se')
button2 = tkinter.Button(addwin, width=8, height=1, command=lambda:getentry(addwin,entry1,entry2,entry3,entry4,entry5,entry6,entry7) , text='确认');
button2.place(x=100, y=250)
text1 = tkinter.Label(addwin, bd=4, fg='black', text='书名:')
text1.place(x=20,y=45)
text2 = tkinter.Label(addwin, bd=4, fg='black', text='作者:')
text2.place(x=20, y=70)
text3 = tkinter.Label(addwin, bd=4, fg='black', text='出版社:')
text3.place(x=20, y=95)
text4 = tkinter.Label(addwin, bd=4, fg='black', text='出版日期:')
text4.place(x=20, y=120)
text5 = tkinter.Label(addwin, bd=4, fg='black', text='书籍编号:')
text5.place(x=20, y=145)
text6 = tkinter.Label(addwin, bd=4, fg='black', text='书籍位置:')
text6.place(x=20, y=170)
text7 = tkinter.Label(addwin, bd=4, fg='black', text='库存量:')
text7.place(x=20, y=195)
entry1 = tkinter.Entry(addwin, width=20, show='')
entry1.place(x=100, y=50)
entry2 = tkinter.Entry(addwin, width=20, show='')
entry2.place(x=100, y=75)
entry3 = tkinter.Entry(addwin, width=20, show='')
entry3.place(x=100, y=100)
entry4 = tkinter.Entry(addwin, width=20, show='')
entry4.place(x=100, y=125)
entry5 = tkinter.Entry(addwin, width=20, show='')
entry5.place(x=100, y=150)
entry6 = tkinter.Entry(addwin, width=20, show='')
entry6.place(x=100, y=175)
entry7 = tkinter.Entry(addwin, width=20, show='')
entry7.place(x=100, y=200)
#修改书籍信息
def modifybook():
modifywin = Tk()
modifywin.title('修改书籍信息')
modifywin.geometry('275x200')
button1 = tkinter.Button(modifywin, text='退出', command=lambda: modifywin.destroy(), width=4, height=1)
button1.place(relx=1, rely=1, anchor='se')
text1 = tkinter.Label(modifywin, bd=4, fg='black', text='书籍编号:')
text1.place(x=20, y=45)
entry1 = tkinter.Entry(modifywin, width=20, show='')
entry1.place(x=100, y=50)
button2 = tkinter.Button(modifywin, width=8, height=1, command=lambda: getentry_mod(modifywin,entry1), text='确认');
button2.place(x=100, y=150)
def getentry_mod(modify,entry1):
if entry1.get()=="":
tkinter.messagebox.showerror('错误', '请输入书籍编号!')
return
bookid=entry1.get()
modify.destroy()
# 打开Excel文件
xls = xlrd.open_workbook('书籍信息.xls')
# 复制一份Excel文件以便修改
wbook = copy.copy(xls)
# 获取工作表
wsheet = wbook.get_sheet(0)
table = xls.sheets()[0]
n = table.nrows
# 查找对应书籍编号
for i in range(1, n):
if table.cell(i, 4).value == bookid:
# 创建修改书籍信息窗口
modifywin = Tk()
modifywin.title('修改书籍信息')
modifywin.geometry('275x250')
# 添加标签和输入框
tkinter.Label(modifywin, bd=4, fg='black', text='书名:').place(x=20, y=25)
entry1 = tkinter.Entry(modifywin, width=20, justify='right')
entry1.insert(0, table.cell(i, 0).value)
entry1.place(x=100, y=25)
tkinter.Label(modifywin, bd=4, fg='black', text='作者:').place(x=20, y=50)
entry2 = tkinter.Entry(modifywin, width=20, justify='right')
entry2.insert(0, table.cell(i, 1).value)
entry2.place(x=100, y=50)
tkinter.Label(modifywin, bd=4, fg='black', text='出版社:').place(x=20, y=75)
entry3 = tkinter.Entry(modifywin, width=20, justify='right')
entry3.insert(0, table.cell(i, 2).value)
entry3.place(x=100, y=75)
tkinter.Label(modifywin, bd=4, fg='black', text='出版日期:').place(x=20, y=100)
entry4 = tkinter.Entry(modifywin, width=20, justify='right')
entry4.insert(0, table.cell(i, 3).value)
entry4.place(x=100, y=100)
tkinter.Label(modifywin, bd=4, fg='black', text='书籍位置:').place(x=20, y=125)
entry5 = tkinter.Entry(modifywin, width=20, justify='right')
entry5.insert(0, table.cell(i, 5).value)
entry5.place(x=100, y=125)
tkinter.Label(modifywin, bd=4, fg='black', text='库存量:').place(x=20, y=150)
entry6 = tkinter.Entry(modifywin, width=20, justify='right')
entry6.insert(0, table.cell(i, 6).value)
entry6.place(x=100, y=150)
# 添加确认修改按钮和退出按钮
tkinter.Button(modifywin, width=8, height=1, text='确认',command=lambda: getentry_mod_finish(bookid, entry1.get(), entry2.get(), entry3.get(),entry4.get(), entry5.get(), entry6.get(),modifywin)).place(x=100, y=200)
tkinter.Button(modifywin, text='退出', command=modifywin.destroy, width=4, height=1).place(relx=1, rely=1,anchor='se')
return
# 如果书籍编号不存在,弹出提示框
tkinter.messagebox.showerror('错误', '该书籍编号不存在!')
#获取修改书籍信息的窗口的内容并对相应的行进行修改
def getentry_mod_finish(bookid,entry1,entry2,entry3,entry4,entry5,entry6,modifywin):
if entry1=="" or entry2=="" or entry3=="" or entry4=="" or entry5=="" or entry6=="":
tkinter.messagebox.showerror('错误', '请输入完整信息!')
return
xls = xlrd.open_workbook('书籍信息.xls')
wbook = copy.copy(xls)
wsheet = wbook.get_sheet(0)
table = xls.sheets()[0]
n = table.nrows
for i in range(1, n):
if table.cell(i, 4).value == bookid:
wsheet.write(i, 0, str(entry1))
wsheet.write(i, 1, str(entry2))
wsheet.write(i, 2, str(entry3))
wsheet.write(i, 3, str(entry4))
wsheet.write(i, 5, str(entry5))
wsheet.write(i, 6, str(entry6))
wbook.save('书籍信息.xls')
tkinter.messagebox.showinfo('提示', '修改成功!')
modifywin.destroy()
return
#修改用户信息
def modifyuser():
modify = Tk()
modify.title('修改用户信息')
modify.geometry('275x200')
text1 = tkinter.Label(modify, bd=4, fg='black', text='借书账号:')
text1.place(x=20, y=45)
entry = tkinter.Entry(modify, width=20, show='')
entry.place(x=100, y=50)
button1 = tkinter.Button(modify, text='退出', command=lambda: modify.destroy(), width=4, height=1)
button1.place(relx=1, rely=1, anchor='se')
button2 = tkinter.Button(modify, width=8, height=1, command=lambda: getentry3(modify, entry), text='确认');
button2.place(x=100, y=100)
#创建窗口,显示用户信息并进行修改
def getentry3(modify,entry):
if entry.get()=="":
tkinter.messagebox.showerror('错误', '请输入用户账号!')
return
account=entry.get()
path = '用户信息' + '\\' + account + '.xls'
try:
xls = xlrd.open_workbook(path)
except:
text = tkinter.Label(modify, bd=4, fg='red', text='用户不存在!')
text.place(x=90, y=20)
return
sheet = xls.sheet_by_index(0)
data = []
for j in range(0, 4):
data.append(str(sheet.cell_value(1, j)))
modify2 = Tk()
modify2.title('修改用户信息')
modify2.geometry('275x200')
button1 = tkinter.Button(modify2, text='退出', command=lambda: modify2.destroy(), width=4, height=1)
button1.place(relx=1, rely=1, anchor='se')
button2 = tkinter.Button(modify2, width=8, height=1,
command=lambda: updateuser(modify2, account, entry1, entry2, entry3), text='确认修改');
button2.place(x=100, y=150)
text1 = tkinter.Label(modify2, bd=4, fg='black', text='姓名:')
text1.place(x=20, y=45)
text2 = tkinter.Label(modify2, bd=4, fg='black', text='性别:')
text2.place(x=20, y=70)
text3 = tkinter.Label(modify2, bd=4, fg='black', text='年龄:')
text3.place(x=20, y=95)
entry1 = tkinter.Entry(modify2, width=20, show='')
entry1.insert(0, data[0])
entry1.place(x=100, y=50)
entry2 = tkinter.Entry(modify2, width=20, show='')
entry2.insert(0, data[1])
entry2.place(x=100, y=75)
entry3 = tkinter.Entry(modify2, width=20, show='')
entry3.insert(0, data[2])
entry3.place(x=100, y=100)
modify.destroy()
#更新用户信息表格中的指定用户的信息
def updateuser(modify2, account, entry1,entry2,entry3):
if entry1.get()=="" or entry2.get()=="" or entry3.get()=="":
tkinter.messagebox.showerror('错误', '请输入完整信息!')
return
data = []
data.append(entry1.get())
data.append(entry2.get())
data.append(entry3.get())
s = '用户信息' + '\\' + account + '.xls'
xls = xlrd.open_workbook(s)
wbook = copy.copy(xls)
wsheet = wbook.get_sheet(0)
table = xls.sheets()[0]
for i in range(len(data)):
wsheet.write(1,i,data[i])
wbook.save(s)
tkinter.messagebox.showinfo('提示', '修改成功!')
modify2.destroy()
#查询书籍信息
def searchbook():
search = Tk()
search.title('查询书籍信息')
search.geometry('275x200')
button1 = tkinter.Button(search, text='退出', command=lambda: search.destroy(), width=4, height=1)
button1.place(relx=1, rely=1, anchor='se')
text1 = tkinter.Label(search, bd=4, fg='black', text='书籍编号:')
text1.place(x=20, y=45)
entry1 = tkinter.Entry(search, width=20, show='')
entry1.place(x=100, y=50)
button2 = tkinter.Button(search, width=8, height=1, command=lambda: getentry_search(entry1.get()), text='确认');
button2.place(x=100, y=150)
def getentry_search(bookid):
if bookid=="":
tkinter.messagebox.showerror('错误', '请输入要查询的书籍编号!')
return
# 打开Excel文件
xls = xlrd.open_workbook('书籍信息.xls')
table = xls.sheets()[0]
n = table.nrows
# 查找对应书籍编号
for i in range(1, n):
if table.cell(i, 4).value == bookid:
# 创建查询书籍信息窗口
seaech = Tk()
seaech.title('查询书籍信息')
seaech.geometry('275x300')
# 添加标签和输入框
tkinter.Label(seaech, bd=4, fg='black', text='书名:').place(x=20, y=25)
entry1 = tkinter.Entry(seaech, width=20, justify='right')
entry1.insert(0, table.cell(i, 0).value)
entry1.place(x=100, y=25)
tkinter.Label(seaech, bd=4, fg='black', text='作者:').place(x=20, y=50)
entry2 = tkinter.Entry(seaech, width=20, justify='right')
entry2.insert(0, table.cell(i, 1).value)
entry2.place(x=100, y=50)
tkinter.Label(seaech, bd=4, fg='black', text='出版社:').place(x=20, y=75)
entry3 = tkinter.Entry(seaech, width=20, justify='right')
entry3.insert(0, table.cell(i, 2).value)
entry3.place(x=100, y=75)
tkinter.Label(seaech, bd=4, fg='black', text='出版日期:').place(x=20, y=100)
entry4 = tkinter.Entry(seaech, width=20, justify='right')
entry4.insert(0, table.cell(i, 3).value)
entry4.place(x=100, y=100)
tkinter.Label(seaech, bd=4, fg='black', text='书籍位置:').place(x=20, y=125)
entry5 = tkinter.Entry(seaech, width=20, justify='right')
entry5.insert(0, table.cell(i, 5).value)
entry5.place(x=100, y=125)
tkinter.Label(seaech, bd=4, fg='black', text='库存量:').place(x=20, y=150)
entry6 = tkinter.Entry(seaech, width=20, justify='right')
entry6.insert(0, table.cell(i, 6).value)
entry6.place(x=100, y=150)
# 添加退出按钮
tkinter.Button(seaech, text='退出', command=seaech.destroy, width=4, height=1).place(relx=1, rely=1,
anchor='se')
return
# 如果书籍编号不存在,弹出提示框
tkinter.messagebox.showerror('错误', '该书籍编号不存在!')
#查阅借阅历史
def checkhistory():
check = Tk()
check.title('查询用户信息')
check.geometry('275x200')
text1 = tkinter.Label(check, bd=4, fg='black', text='借书账号:')
text1.place(x=20, y=45)
entry1 = tkinter.Entry(check, width=20, show='')
entry1.place(x=100, y=50)
button1 = tkinter.Button(check, text='退出', command=lambda: check.destroy(), width=4, height=1)
button1.place(relx=1, rely=1, anchor='se')
button2 = tkinter.Button(check, width=8, height=1, command=lambda: getentry_check(entry1.get()), text='确认');
button2.place(x=100, y=100)
def getentry_check(account):
if account=="":
tkinter.messagebox.showerror('错误', '请输入要查询的用户账号!')
return
path = '用户信息' + '\\' + account + '.xls'
try:
xls = xlrd.open_workbook(path)
except:
tkinter.messagebox.showerror('错误', '该用户不存在!')
return
sheet = xls.sheet_by_index(0)
n = sheet.nrows
data=[]
ss=""
for j in range(0, 9):
ss = ss + str(sheet.cell_value(0, j)) + "\t\t\t"
ss=ss+"\n"
for i in range(1, n):
data1=[]
for j in range(sheet.ncols):
data1.append(str(sheet.cell_value(i,j)))
data.append(data1)
for i in range(0,4):
ss=ss+sheet.cell_value(1,i)+"\t\t\t"
data.sort(key=lambda x:x[8])
t=0
for value in data:
if t==1:
for i in range(0,4):
ss=ss+""+"\t\t\t"
for i in range(4,len(value)):
ss=ss+str(value[i])+"\t\t\t"
if t==0:
for i in range(4,len(value)):
ss=ss+str(value[i])+"\t\t\t"
t=1
ss=ss+"\n"
check = Tk()
check.title('查询用户信息')
check.geometry('1600x500')
roll = tkinter.Scrollbar(check)
roll.pack(side=tkinter.RIGHT, fill=tkinter.Y)
text_input = tkinter.Text(check, width=800, height=500)
text_input.insert('insert', ss)
text_input.pack(expand=tkinter.YES, fill=tkinter.BOTH)
text_input.config(yscrollcommand=roll.set)
roll.config(command=text_input.yview)
button1 = tkinter.Button(check, text='退出', command=lambda: check.destroy(), width=4, height=1)
button1.place(relx=1, rely=1, anchor='se')
#删除书籍信息
def deletebook():
delete = Tk()
delete .title('删除书籍信息')
delete .geometry('275x200')
button1 = tkinter.Button(delete , text='退出', command=lambda: delete.destroy(), width=4, height=1)
button1.place(relx=1, rely=1, anchor='se')
text1 = tkinter.Label(delete, bd=4, fg='black', text='书籍编号:')
text1.place(x=20, y=45)
entry1 = tkinter.Entry(delete, width=20, show='')
entry1.place(x=100, y=50)
button2 = tkinter.Button(delete, width=8, height=1, command=lambda: getentry_delete(entry1), text='确认');
button2.place(x=100, y=150)
def getentry_delete(entry1):
bookid=entry1.get()
if bookid=="":
tkinter.messagebox.showerror('错误', '请输入要删除的书籍编号!')
return
# 打开Excel文件
xls = xlrd.open_workbook('书籍信息.xls')
sheet = xls.sheet_by_index(0)
table = xls.sheets()[0]
n = table.nrows
data=[]
for i in range(1,n):
if sheet.cell_value(i,4) == bookid:
for j in range(i+1,n):
for u in range(0,7):
data.append(str(sheet.cell_value(j,u)))
os.remove('书籍信息.xls')
if not os.path.exists('书籍信息.xls'):
book = xlwt.Workbook(encoding='utf-8', style_compression=0)
sheet = book.add_sheet('书籍管理系统', cell_overwrite_ok=True)
col = ('书名', '作者', '出版社', '出版日期', '书籍编号', '书籍位置', '库存量')
for i in range(0, 7):
sheet.write(0, i, col[i])
savepath = '书籍信息.xls'
book.save(savepath)
xls = xlrd.open_workbook('书籍信息.xls')
wbook = copy.copy(xls)
wsheet = wbook.get_sheet(0)
k=0
for i in range(1,n-1):
for j in range(0,7):
wsheet.write(i,j,data[k])
k=k+1
wbook.save('书籍信息.xls')
tkinter.messagebox.showinfo('提示', '删除成功!')
entry1.delete("0","end")
return
for k in range(0,7):
data.append(str(sheet.cell_value(i,k)))
tkinter.messagebox.showerror('错误', '未找到该图书编号的书籍信息!')
entry1.delete("0", "end")
#删除用户
def deleteuser():
delete = Tk()
delete .title('删除用户信息')
delete .geometry('275x200')
text1 = tkinter.Label(delete, bd=4, fg='black', text='删除账号:')
text1.place(x=20, y=45)
entry = tkinter.Entry(delete, width=20, show='')
entry.place(x=100, y=50)
button1 = tkinter.Button(delete, text='退出', command=lambda: delete.destroy(), width=4, height=1)
button1.place(relx=1, rely=1, anchor='se')
button2 = tkinter.Button(delete, width=8, height=1, command=lambda: getentry5(delete, entry), text='确认');
button2.place(x=100, y=100)
def getentry5(modify,entry):
if entry.get()=="":
tkinter.messagebox.showerror('错误', '请输入用户账号!')
return
account=entry.get()
path = '用户信息' + '\\' + account + '.xls'
if os.path.exists(path):
os.remove(path)
tkinter.messagebox.showinfo('提示', '删除成功!')
else:
tkinter.messagebox.showerror('错误', '未找到该图书编号的书籍信息!')
#借书
def borrowbook():
#创建用于输入借书相关信息的窗口
modify = Tk()
modify.title('借书')
modify.geometry('275x200')
text1 = tkinter.Label(modify, bd=4, fg='black', text='借书账号:')
text1.place(x=20, y=45)
entry1 = tkinter.Entry(modify, width=20, show='')
entry1.place(x=100, y=50)
text2 = tkinter.Label(modify, bd=4, fg='black', text='书籍编号:')
text2.place(x=20, y=95)
entry2 = tkinter.Entry(modify, width=20, show='')
entry2.place(x=100, y=100)
button1 = tkinter.Button(modify, text='退出', command=lambda: modify.destroy(), width=4, height=1)
button1.place(relx=1, rely=1, anchor='se')
button2 = tkinter.Button(modify, width=8, height=1, command=lambda: getbook(entry1,entry2), text='确认');
button2.place(x=100, y=150)
def getbook(entry1,entry2):
#判断是否输入借书账号
if entry1.get()=="":
tkinter.messagebox.showerror('错误', '请输入借书账号!')
entry2.delete("0", "end")
return
#判断是否输入书籍编号
if entry2.get()=="":
tkinter.messagebox.showerror('错误', '请输入书籍编号!')
entry2.delete("0", "end")
return
account = entry1.get()
path = '用户信息' + '\\' + account + '.xls'
#判断用户是否存在
try:
xls1 = xlrd.open_workbook(path)
wbook1 = copy.copy(xls1)
wsheet1 = wbook1.get_sheet(0)
sheet1 = xls1.sheet_by_index(0)
table1 = xls1.sheets()[0]
#判断用户未还书籍数量上限为5本达到上限则不允许借书
y=0
for p in range (1,table1.nrows):
if sheet1.cell_value(p,8)=="":
y=y+1
if y>=5:
tkinter.messagebox.showerror('错误', '该用户现已借满5本书请先还书')
entry1.delete("0", "end")
entry2.delete("0", "end")
return
except:
tkinter.messagebox.showerror('错误', '用户不存在!')
#清空错误的输入信息
entry1.delete("0", "end")
entry2.delete("0", "end")
return
bookid=entry2.get()
xls = xlrd.open_workbook('书籍信息.xls')
wbook=copy.copy(xls)
sheet = xls.sheet_by_index(0)
wsheet=wbook.get_sheet(0)
table = xls.sheets()[0]
n = table.nrows
# 查找对应书籍编号
t=0
for i in range(1, n):
#判断图书馆是否收录此书
if table.cell(i, 4).value == bookid:
t=1
#判断书籍是否有库存
if(int(sheet.cell_value(i,6))>0):
for j in range(1, table1.nrows):
#判断是否已借过此书,防止重复借同本书
if sheet1.cell_value(j, 5) == bookid and sheet1.cell_value(j, 8) == "":
tkinter.messagebox.showerror('错误', '已借过此书且未归还,请先归还再借!')
entry2.delete("0", "end")
return
#获取该书籍编号的书名
bookname=table.cell(i,0).value
#成功借书库存减1
x=int(sheet.cell_value(i,6))-1
wsheet.write(i,6,str(x))
wbook.save('书籍信息.xls')
else:
tkinter.messagebox.showerror('错误', '此书已借出,暂无剩余库存!')
entry2.delete("0", "end")
return
break
if t==0:
tkinter.messagebox.showerror('错误','本馆暂无收录此书!')
entry2.delete("0", "end")
return
data=[]
now=datedays.gettomorrow(days=0)
data.append(str(now))
data.append(str(bookid))
data.append(str(bookname))
end=datedays.gettomorrow(days=30)
data.append(str(end))
data.append("")
n=4
#从未借过书的情况
if sheet1.cell_value(1,4) == "":
for i in range(len(data)):
wsheet1.write(1, n, data[i])
n=n+1
#借过书的情况
else:
for i in range(len(data)):
wsheet1.write(table1.nrows,n,data[i])
n=n+1
wbook1.save(path)
tkinter.messagebox.showinfo('提示', '借书成功!')
entry2.delete("0", "end")
#还书
def returnbook():
#创建用于输入还书相关信息的窗口
modify = Tk()
modify.title('还书')
modify.geometry('275x200')
text1 = tkinter.Label(modify, bd=4, fg='black', text='还书账号:')
text1.place(x=20, y=45)
entry1 = tkinter.Entry(modify, width=20, show='')
entry1.place(x=100, y=50)
text2 = tkinter.Label(modify, bd=4, fg='black', text='书籍编号:')
text2.place(x=20, y=95)
entry2 = tkinter.Entry(modify, width=20, show='')
entry2.place(x=100, y=100)
button1 = tkinter.Button(modify, text='退出', command=lambda: modify.destroy(), width=4, height=1)
button1.place(relx=1, rely=1, anchor='se')
button2 = tkinter.Button(modify, width=8, height=1, command=lambda: returnbook2(entry1, entry2), text='确认');
button2.place(x=100, y=150)
def returnbook2(entry1,entry2):
#判断还书账号是否输入
if entry1.get()=="":
tkinter.messagebox.showerror('错误', '请输入还书账号!')
entry2.delete("0", "end")
return
#判断归还书籍的书籍编号是否输入
if entry2.get()=="":
tkinter.messagebox.showerror('错误', '请输入归还书籍的书籍编号!')
entry2.delete("0", "end")
return
account = entry1.get()
path = '用户信息' + '\\' + account + '.xls'
#判断用户是否存在
try:
xls1 = xlrd.open_workbook(path)
wbook1 = copy.copy(xls1)
wsheet1 = wbook1.get_sheet(0)
sheet1 = xls1.sheet_by_index(0)
table1 = xls1.sheets()[0]
except:
tkinter.messagebox.showerror('错误', '用户不存在!')
#清空错误信息
entry1.delete("0", "end")
entry2.delete("0", "end")
return
bookid=entry2.get()
xls = xlrd.open_workbook('书籍信息.xls')
wbook=copy.copy(xls)
sheet = xls.sheet_by_index(0)
wsheet=wbook.get_sheet(0)
table = xls.sheets()[0]
n = table.nrows
t=0
d=0
#查找是否借过并且尚未归还此书
for i in range(1,table1.nrows):
if sheet1.cell_value(i,5)==bookid:
#成功查找到未还此书的借书信息
if sheet1.cell_value(i, 8) == "":
# 查找对应书籍编号,判断图书馆是否收录有此书
for j in range(1, n):
if table.cell(j, 4).value == bookid:
d = 1
#成功还书库存加1用户借书信息改为已还书
x = int(sheet.cell_value(j, 6)) + 1
wsheet.write(j, 6, str(x))
wbook.save('书籍信息.xls')
wsheet1.write(i, 8, "")
wbook1.save(path)
break
#未在书籍信息表格中找到此书
if d == 0:
tkinter.messagebox.showerror('错误', '本馆暂无收录此书!')
entry2.delete("0", "end")
return
t=1
break
#查无符合条件的借书信息
if t==0:
tkinter.messagebox.showerror('错误', '该用户无此借书信息!')
return
tkinter.messagebox.showinfo('提示', '还书成功!')
entry2.delete("0", "end")
#若不存在名为书籍信息的表格,则创建一个
if not os.path.exists('书籍信息.xls'):
book = xlwt.Workbook(encoding='utf-8', style_compression=0)
sheet = book.add_sheet('书籍管理系统', cell_overwrite_ok=True)
col = ('书名', '作者', '出版社', '出版日期','书籍编号','书籍位置','库存量')
for i in range(0, 7):
sheet.write(0, i, col[i])
savepath='书籍信息.xls'
book.save(savepath)
#若不存在名为用户信息的文件夹,则创建一个
path='用户信息'
if not os.path.exists(path):
os.mkdir('用户信息')
window=createwin()
window.mainloop() # 运行主窗口,保持窗口打开状态,直到用户关闭窗口。