完整代码以及打包好的exe文件地址:
链接:https://pan.baidu.com/s/1FbVLMp74gLmCC69O20jMlg
提取码:ljyx
注:main_desktop.py中QQ和QQ授权码需要换成自己的!!!!!!
一、效果图
主页面
管理员界面
读者界面
功能介绍:
1、登录
会判断账号是否存在和输入是否正确,密码错误可以进行忘记密码操作。
2、注册
会判断账号是否存在,账号、密码格式是否正确,会向所填邮箱发送验证码,并将注册后的账号密码等信息记录起来。
代码
QQ和QQ授权码需要换成自己的
#main_desktop.py
#用于第一个页面的功能
import pandas as pd
# smtplib 用于邮件的发信动作
import smtplib
# email 用于构建邮件内容
from email.mime.text import MIMEText
#构建邮件头
from email.header import Header
from email.utils import formataddr
from email.mime.multipart import MIMEMultipart
import time
import random
import string
import desktop
import os
import sys
#生成资源文件目录访问路径
def resource_path(relative_path):
if getattr(sys, 'frozen', False): #是否Bundle Resource
base_path = sys._MEIPASS
else:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
#访问res文件夹下数据
#各种表格
administrators = pd.read_csv(resource_path(os.path.join("res","administrators.csv"))) #管理员账号表
user = pd.read_csv(resource_path(os.path.join("res","user.csv"))) #读者账号表
#选择编码
def Pick(number):
while True:
try:
pick = int(input('请输入需要的编号:'))
if pick not in [i for i in range(1,number+1)]:
print('编号无效!请重新输入!')
continue
else:
break
except:
print('请输入数字!')
return pick
#发验证码
def to_email(email,txt):
# 发信方的信息:发信邮箱,QQ 邮箱授权码
from_addr = '*******@qq.com'
password = '自己的授权码'
# 发信服务器
smtp_server = 'smtp.qq.com'
msg = MIMEMultipart()
msg['From'] = formataddr(["图书馆管理系统",from_addr])
msg['To'] = formataddr(['',email])
msg['Subject'] = Header('图书馆管理系统-邮箱验证')
# 邮箱正文内容,第一个参数为内容,第二个参数为格式(plain 为纯文本),第三个参数为编码
#生成一个随机验证码
if txt == 0:
code = ''.join(random.choice(string.ascii_letters + string.digits) for _ in range(5))
txt='【图书馆管理系统】本次验证码为'+code+',您正在申请通过图书馆管理系统验证,如非本人操作,请忽略。'
msg.attach(MIMEText(txt, 'plain', 'utf-8'))
server = smtplib.SMTP_SSL(smtp_server)
server.connect(smtp_server,465)
server.login(from_addr, password)
server.sendmail(from_addr, email, msg.as_string())
# 关闭服务器
server.quit()
print(f'验证码已发送至{email},请注意查收!')
return code
# 密码格式判断
def count(s):
num, char, space, d ,count= 0, 0, 0, 0 ,0 # 分别统计数字、字母、空格、其他字符个数
for i in s:
if i.isdigit():
num += 1
elif i.isalpha():
char += 1
elif i == ' ':
space += 1
print('密码存在空格!')
return 0
else:
d += 1
for i in [num, char, d]:
if i >0:
count += 1
if count>=2:
return 1
else:
print('密码应该包含数字、字母、标点符号中的两种或两种以上!')
return 0
#忘记密码
def forget(email,x): #0管理员,1读者
global administrators
global user
print('正在发送验证码....')
try:
code_x = to_email(email,0)
except:
print('邮箱错误!')
return desktop.home_page()
code_y =input('请输入验证码:')
count_ = 0 #计算次数
while code_x!=code_y:
print('验证码输入错误!')
count_+=1
if count_==5:
print('连续错误5次,需要重新发送验证码!')
code_x = to_email(email,0)
print(f'验证码已发送至{email},请注意查收!')
count_=0
else:
code_y = input('请输入验证码:')
print('验证码输入正确!')
while True:
newpassword = input('请输入新的密码:')
if (len(newpassword)<6) or (len(newpassword)>10):
print('密码应该长度大于6且小于10')
continue
else:
if count(newpassword): #正确就退出
break
else:
continue
print('正在修改........')
if x==0:
administrators.loc[administrators['邮箱']==email,'密码'] = newpassword
administrators.to_csv(resource_path(os.path.join("res",'administrators.csv')),index=False)
else:
user.loc[user['邮箱']==email,'密码'] = newpassword
user.to_csv(resource_path(os.path.join("res",'user.csv')),index=False)
print('密码修改成功!')
return desktop.home_page()
# 登录(记录操作者)
def Sign_in(x): #管理员账号表或读者账号表,0管理员,1读者
global administrators
global user
#判断学号密码是否正确
#学号必须是数字
while True:
try:
id = int(input('请输入学号:'))
break
except:
print('学号必须是数字!')
password = input('请输入密码:')
#查询学号是否存在,密码是否正确
if x==0:
sign_in_data = administrators
elif x==1:
sign_in_data = user
#print(sign_in_data)
while (id not in sign_in_data['学号'].values)or(sign_in_data[sign_in_data['学号']==id]['密码'].values[0] != password):
print('账号或密码输入错误!\n')
print('\n* * * * * * * * 请 选 择 所 需 功 能 * * * * * * * * \n\n\t\t 1.重新尝试\n\t\t 2.忘记密码\n\t\t 3.退回首页\n\n* * * * * * * * * * * * * * * * * * * * * * * * * *\n')
pick = Pick(3)
if pick == 1:
return 11
elif pick == 2: #忘记密码操作
email = (sign_in_data.loc[sign_in_data['学号']==id]['邮箱'].values[0])
if email != 0: #账号存在
if x == 0:
return forget(email[0],0)
elif x==1:
return forget(email[0],1)
else:
print('账号不存在!')
return 0
elif pick == 3:
return 0
print("正在登录.......")
time.sleep(3)
print('登录成功!')
return id
#注册
def logon(x):
global administrators
global user
while True:
try:
id = int(input('请输入学号:'))
break
except:
print('请输入数字!')
name = input('请输入姓名:')
major = input('请输入专业名:')
#验证学号是否注册过
if x==0: #管理员
idlist = [administrators['学号'].values[0]]
elif x==1: #读者
idlist = [user['学号'].values[0]]
while id in idlist:
print('学号已注册过!\n')
print('\n* * * * * * * * 请 选 择 所 需 功 能 * * * * * * * * \n\n\t\t 1.重新尝试\n\t\t 2.退回首页\n\n* * * * * * * * * * * * * * * * * * * * * * * * * *\n')
pick = Pick(2)
if pick == 1:
while True:
try:
id = int(input('请输入学号:'))
break
except:
print('请输入数字!')
break
else:
return 0
while True:
password = input('请输入密码:')
if (len(password)<6) or (len(password)>10):
print('密码应该长度大于6且小于10')
continue
else:
if count(password): #正确就退出
break
else:
continue
#发邮件
while True:
try:
email = input('请输入邮箱账号:')
code_x = to_email(email,0)
while True:
code_y = input('请输入验证码:')
if code_x == code_y:
pick = 0
break
else:
print('验证码输入错误!')
print('\n* * * * * * * * 请 选 择 所 需 功 能 * * * * * * * * \n\n\t\t 1.重新输入邮箱\n\t\t 2.重新输入验证码\n\t\t 3.返回首页\n\n* * * * * * * * * * * * * * * * * * * * * * * * * *\n')
pick=Pick(3)
if pick == 1:
break
elif pick == 2:
continue
else:
return 0
except:
print('邮箱账号错误!')
print('\n* * * * * * * * 请 选 择 所 需 功 能 * * * * * * * * \n\n\t\t 1.重新输入\n\t\t 2.返回首页\n\n* * * * * * * * * * * * * * * * * * * * * * * * * *\n')
pick = Pick(2)
if pick == 1:
continue
else:
return 0
if pick == 2:
continue
elif pick ==0:
break
print('正在注册......')
time.sleep(3)
#存贮
if x==0:
administrators0 = pd.DataFrame([{
'学号':id,
'姓名':name,
'专业':major,
'邮箱':email,
'密码':password}])
administrators = pd.concat([administrators,administrators0],axis = 0).reset_index(drop=True)
administrators.to_csv(resource_path(os.path.join("res",'administrators.csv')),index=False)
else:
user0 = pd.DataFrame([{
'学号':id,
'姓名':name,
'专业':major,
'邮箱':email,
'密码':password}])
user = pd.concat([user,user0],axis = 0).reset_index(drop=True)
user.to_csv(resource_path(os.path.join("res",'user.csv')),index=False)
print('注册成功!\n')
return 0
二、管理员系统
功能介绍:
1.书籍录入
判断书籍有没有存在,存在表现为书籍数量的增加,不存在为种类和数量的增加,记录操作者和操作时间。
2.书籍查询
可以按编号、书籍名、作者进行模糊查询
3.书籍删除
删除对应书籍的全部信息,记录操作者和操作时间
4.书籍修改
修改书籍数据,记录操作者和操作时间
5.借阅登记
记录借阅者和借阅数据
6.归还登记
记录归还者和归还记录
7.读者信息
可以改变读者的密码等操作
代码
#administrators_function.py
#用于存放管理员的各种功能
from tabulate import tabulate
import pandas as pd
import desktop
import main_desktop
import time
import sys
import os
#生成资源文件目录访问路径
def resource_path(relative_path):
if getattr(sys, 'frozen', False): #是否Bundle Resource
base_path = sys._MEIPASS
else:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
#访问res文件夹下数据
#各种表格
administrators = pd.read_csv(resource_path(os.path.join("res","administrators.csv"))) #管理员账号表
user = pd.read_csv(resource_path(os.path.join("res","user.csv"))) #读者账号表
books = pd.read_csv(resource_path(os.path.join("res","books.csv"))) #书籍表
readers = pd.read_csv(resource_path(os.path.join("res","readers.csv"))) #读者借阅\归还记录
book_rework = pd.read_csv(resource_path(os.path.join("res","book_rework.csv"))) #数据库修改日志
#选择编码
def Pick(number):
while True:
try:
pick = int(input('请输入需要的编号:'))
if pick not in [i for i in range(1,number+1)]:
print('编号无效!请重新输入!')
continue
else:
break
except:
print('请输入数字!')
return pick
#1.书籍录入
#填写日志
def book_add(id):
global books
global book_rework
while True:
books_name = input('书籍名称:')
books_author = input('作者:')
books_date1 = input('书籍出版日期:')
press = input('出版社:')
books_date2 = time.strftime("%Y-%m-%d %H:%M:%S") #获取当前时间
while True:
try:
books_count = int(input('书籍数量:'))
break
except:
print('请输入数字!')
#书籍编号,判断书籍是否存在
query = books[books['书籍名称']==books_name]
if len(query):
query = query[query['作者']==books_author]
if len(query):
query = query[query['出版日期']==books_date1]
if len(query):#说明书籍存在
print('正在录入.......')
#查找书籍编号
books_no = query.iloc[:1,0].values[0]
#馆藏和可借数量增加
books.loc[books['书籍编号']==books_no,'馆藏'] = books.loc[books['书籍编号']==books_no,'馆藏'] + books_count
books.loc[books['书籍编号']==books_no,'可借'] = books.loc[books['书籍编号']==books_no,'可借'] + books_count
books.to_csv(resource_path(os.path.join("res",'books.csv')),index=False)
#将修改记录保存起来
book_rework0 = pd.DataFrame([{
'姓名':administrators.loc[administrators['学号']==id]['姓名'][0],
'学号':administrators.loc[administrators['学号']==id]['学号'][0],
'书籍编号':books_no,
'修改列':'馆藏和可借',
'原来-现在':f'增加{books_count}',
'修改时间':time.strftime("%Y-%m-%d %H:%M:%S")
}])
book_rework = pd.concat([book_rework,book_rework0],axis = 0).reset_index(drop=True)
book_rework.to_csv(resource_path(os.path.join("res",'book_rework.csv')),index=False)
#不存在时(按编号最大的加1)
if len(query) == 0:
print('正在录入.......')
books_id = max(books['书籍编号']) + 1
books0 = pd.DataFrame([{
'书籍编号':books_id,
'书籍名称':books_name,
'作者':books_author,
'出版日期':books_date1,
'出版社':press,
'导入日期':books_date2,
'馆藏':books_count,
'可借':books_count
}])
books = pd.concat([books,books0],axis = 0).reset_index(drop=True) #合并并重置索引
books.to_csv(resource_path(os.path.join("res",'books.csv')),index=False)
#将修改记录保存起来
book_rework0 = pd.DataFrame([{
'姓名':administrators.loc[administrators['学号']==id]['姓名'].values[0],
'学号':administrators.loc[administrators['学号']==id]['学号'].values[0],
'书籍编号':books_id,
'修改列':'全部',
'原来-现在':f'增加《{books_name}》{books_count}本',
'修改时间':time.strftime("%Y-%m-%d %H:%M:%S")
}])
book_rework = pd.concat([book_rework,book_rework0]).reset_index(drop=True)
book_rework.to_csv(resource_path(os.path.join("res",'book_rework.csv')),index=False)
print('录入成功!')
print('\n* * * * * * * * 请 选 择 所 需 功 能 * * * * * * * * \n\n\t\t 1.继续录入\n\t\t 2.返回首页\n\n* * * * * * * * * * * * * * * * * * * * * * * * * *\n')
num = Pick(2)
if num == 1:
continue
else:
return desktop.root_menu()
#2.书籍查询
def book_query(id):
while True:
print('\n* * * * * * * * 请 选 择 查 询 方 式 * * * * * * * * \n\n\t\t 1.按书籍编号查询\n\t\t 2.按书籍名称查询\n\t\t 3.按作者查询\n\n* * * * * * * * * * * * * * * * * * * * * * * * * *\n')
query_x = Pick(3)
if query_x==1:
while True:
try:
query_y = int(input('请输入查询信息:'))
break
except:
print('请输入数字!')
book_ = books.loc[books['书籍编号']==int(query_y)]
elif query_x==2:
query_y = input('请输入查询信息:')
book_ = books[books.书籍名称.str.contains(query_y,na=False)]
elif query_x==3:
query_y = input('请输入查询信息:')
book_ = books[books.作者.str.contains(query_y,na=False)]
if len(book_)==0:
print('查询书籍不存在!')
print('\n* * * * * * * * 请 选 择 所 需 功 能 * * * * * * * * \n\n\t\t 1.重新查询\n\t\t 2.返回首页\n\n* * * * * * * * * * * * * * * * * * * * * * * * * *\n')
num = Pick(2)
if num == 1:
continue
else:
return desktop.root_menu()
else:
print('\n')
print(tabulate(book_, headers = 'keys', tablefmt = 'pretty'))
print('\n* * * * * * * * 请 选 择 所 需 功 能 * * * * * * * * \n\n\t\t 1.重新查询\n\t\t 2.书籍删除\n\t\t 3.借阅登记\n\t\t 4.返回首页\n\n* * * * * * * * * * * * * * * * * * * * * * * * * *\n')
num = Pick(4)
if num == 1:
continue
elif num == 2:
return book_delete(id)
elif num == 3:
return book_borrow()
else:
return desktop.root_menu()
#3.书籍删除
#会直接删除书籍的全部信息
def book_delete(id):
global books
global book_rework
while True:
while True:
try:
query_y = int(input('请输入书籍编号:'))
break
except:
print('请输入数字!')
book_ = books.loc[books['书籍编号']==query_y]
if len(book_)==0:
print('书籍不存在!')
print('\n* * * * * * * * 请 选 择 所 需 功 能 * * * * * * * * \n\n\t\t 1.重新输入\n\t\t 2.返回首页\n\n* * * * * * * * * * * * * * * * * * * * * * * * * *\n')
pick = Pick(2)
if pick == 1:
continue
else:
return desktop.root_menu()
else:
print(book_)
print('\n* * * * * * * * 请 选 择 所 需 功 能 * * * * * * * * \n\n\t\t 1.删除书籍\n\t\t 2.重新输入\n\t\t 3.返回首页\n\n* * * * * * * * * * * * * * * * * * * * * * * * * *\n')
pick = Pick(3)
if pick ==1:
print('正在删除......')
#删除书籍
books = books.drop(books.loc[books['书籍编号']==query_y].index, axis=0)
books.to_csv(resource_path(os.path.join("res",'books.csv')),index=False)
#将修改记录保存起来
book_rework0 = pd.DataFrame([{
'姓名':administrators.loc[administrators['学号']==id]['姓名'].values[0],
'学号':administrators.loc[administrators['学号']==id]['学号'].values[0],
'书籍编号':query_y,
'修改列':'全部列',
'原来-现在':list(books.loc[books['书籍编号']==query_y].values),
'修改时间':time.strftime("%Y-%m-%d %H:%M:%S")
}])
book_rework = pd.concat([book_rework,book_rework0]).reset_index(drop=True)
books.to_csv(resource_path(os.path.join("res",'books.csv')),index=False)
print('删除成功!')
print('\n* * * * * * * * 请 选 择 所 需 功 能 * * * * * * * * \n\n\t\t 1.继续删除\n\t\t 2.返回首页\n\n* * * * * * * * * * * * * * * * * * * * * * * * * *\n')
pick = Pick(2)
if pick == 1:
continue
else:
return desktop.root_menu()
elif pick == 2:
continue
else:
return desktop.root_menu()
#4.书籍修改
#需要填写修改日志
def book_Rework(id):
global books
global book_rework
while True:
while True:
try:
query_y = int(input('请输入书籍编号:'))
break
except:
print('请输入数字!')
book_ = books.loc[books['书籍编号']==query_y]
if len(book_)==0:
print('书籍不存在!')
print('\n* * * * * * * * 请 选 择 所 需 功 能 * * * * * * * * \n\n\t\t 1.重新输入\n\t\t 2.返回首页\n\n* * * * * * * * * * * * * * * * * * * * * * * * * *\n')
pick = Pick(2)
if pick == 1:
continue
else:
return desktop.root_menu()
else:
print(tabulate(book_, headers = 'keys', tablefmt = 'pretty'))
print('\n* * * * * * * * 请 选 择 所 需 功 能 * * * * * * * * \n\n\t\t 1.修改书籍\n\t\t 2.重新输入\n\t\t 3.返回首页\n\n* * * * * * * * * * * * * * * * * * * * * * * * * *\n')
pick = Pick(3)
if pick ==1:
print('\n* * * * * * * * 请 选 择 修 改 字 段 * * * * * * * * \n\n\t\t 1.修改书籍名称\n\t\t 2.修改书籍作者\n\t\t 3.修改馆藏\n\t\t 4.修改可借\n\n* * * * * * * * * * * * * * * * * * * * * * * * * *\n')
pick = Pick(3)
if pick == 1:
book_newname = input('请输入新的书籍名称:')
#将修改记录保存起来
old = books.loc[books['书籍编号'] == query_y,'书籍名称'].values
book_rework0 = pd.DataFrame([{
'姓名':administrators.loc[administrators['学号']==id]['姓名'].values[0],
'学号':administrators.loc[administrators['学号']==id]['学号'].values[0],
'书籍编号':query_y,
'修改列':'书籍名称',
'原来-现在':f'{old[0]}'+'-'+f'{book_newname}',
'修改时间':time.strftime("%Y-%m-%d %H:%M:%S")
}])
books.loc[books['书籍编号'] == query_y,'书籍名称'] = book_newname
elif pick == 2:
book_author = input('请输入新的作者:')
#将修改记录保存起来
old = books.loc[books['书籍编号'] == query_y,'作者'].values
book_rework0 = pd.DataFrame([{
'姓名':administrators.loc[administrators['学号']==id]['姓名'].values[0],
'学号':administrators.loc[administrators['学号']==id]['学号'].values[0],
'书籍编号':query_y,
'修改列':'作者',
'原来-现在':f'{old[0]}'+'-'+f'{book_author}',
'修改时间':time.strftime("%Y-%m-%d %H:%M:%S")
}])
books.loc[books['书籍编号'] == query_y,'作者'] = book_author
elif pick == 3:
while True:
try:
book_collection = int(input('请输入新的馆藏:'))
break
except:
print('请输入数字!')
#将修改记录保存起来
old = books.loc[books['书籍编号'] == query_y,'馆藏'].values
book_rework0 = pd.DataFrame([{
'姓名':administrators.loc[administrators['学号']==id]['姓名'].values[0],
'学号':administrators.loc[administrators['学号']==id]['学号'].values[0],
'书籍编号':query_y,
'修改列':'馆藏',
'原来-现在':f'{old[0]}'+'-'+f'{book_collection}',
'修改时间':time.strftime("%Y-%m-%d %H:%M:%S")
}])
books.loc[books['书籍编号'] == query_y,'馆藏'] = book_collection
else:
while True:
try:
book_borrow = int(input('请输入新的可借:'))
break
except:
print('请输入数字!')
#将修改记录保存起来
old = books.loc[books['书籍编号'] == query_y,'可借'].values
book_rework0 = pd.DataFrame([{
'姓名':administrators.loc[administrators['学号']==id]['姓名'].values[0],
'学号':administrators.loc[administrators['学号']==id]['学号'].values[0],
'书籍编号':query_y,
'修改列':'全部列',
'原来-现在':f'{old[0]}'+'-'+f'{book_borrow}',
'修改时间':time.strftime("%Y-%m-%d %H:%M:%S")
}])
books.loc[books['书籍编号'] == query_y,'可借'] = book_borrow
print('正在修改........')
book_rework = pd.concat([book_rework,book_rework0]).reset_index(drop=True)
books.to_csv(resource_path(os.path.join("res",'books.csv')),index=False)
book_rework.to_csv(resource_path(os.path.join("res",'book_rework.csv')),index=False)
print('修改成功!')
print('\n* * * * * * * * 请 选 择 所 需 功 能 * * * * * * * * \n\n\t\t 1.继续修改\n\t\t 2.返回首页\n\n* * * * * * * * * * * * * * * * * * * * * * * * * *\n')
pick = Pick(2)
if pick == 1:
continue
else:
return desktop.root_menu()
elif pick == 2:
continue
else:
return desktop.root_menu()
#5.借阅登记
def book_borrow():
global books
global readers
while True:
while True:
try:
query_y = int(input('请输入书籍编号:'))
break
except:
print('请输入数字!')
book_ = books.loc[books['书籍编号']==query_y]
if len(book_)==0:
print('书籍不存在!')
print('\n* * * * * * * * 请 选 择 所 需 功 能 * * * * * * * * \n\n\t\t 1.重新输入\n\t\t 2.返回首页\n\n* * * * * * * * * * * * * * * * * * * * * * * * * *\n')
pick = Pick(2)
if pick == 1:
return book_borrow()
else:
return desktop.root_menu()
else:
print('\n')
print(tabulate(book_, headers = 'keys', tablefmt = 'pretty'))
print('\n')
#可借为0
if book_['可借'].values==0 :
print('书籍数量为0,无法借阅!')
else:
print('\n* * * * * * * * 请 选 择 所 需 功 能 * * * * * * * * \n\n\t\t 1.借阅书籍\n\t\t 2.重新输入\n\t\t 3.返回首页\n\n* * * * * * * * * * * * * * * * * * * * * * * * * *\n')
pick = Pick(3)
if pick == 1:
while True:
try:
booknum = int(input('请输入借阅数量:'))
if booknum < 1:
print('借阅数量不能小于1!')
elif booknum > book_['可借'].values:
print('借阅数量不能多于可借书籍!')
else:
break
except:
print('请输入数字!')
while True:
try:
sno = int(input('请输入学号:'))
break
except:
print('请输入数字!')
name = input('请输入姓名:')
major = input('请输入专业名:')
#记录借阅 readers.csv
print('正在借阅......')
readers0 = pd.DataFrame([{
'学号':sno,
'姓名':name,
'专业':major,
'书籍编号':query_y,
'书籍名':(books.loc[books['书籍编号']==query_y]['书籍名称'].values)[0],
'作者':(books.loc[books['书籍编号']==query_y]['作者'].values)[0],
'数量':booknum,
'借阅/归还':'借阅',
'时间':time.strftime("%Y-%m-%d %H:%M:%S")
}])
readers = pd.concat([readers,readers0]).reset_index(drop=True)
readers.to_csv(resource_path(os.path.join("res",'readers.csv')),index=False)
#书籍表更改
books.loc[books['书籍编号'] == query_y,'可借'] -= booknum
books.to_csv(resource_path(os.path.join("res",'books.csv')),index=False)
elif pick == 2:
continue
else:
return desktop.root_menu()
print('借阅成功!')
print('\n* * * * * * * * 请 选 择 所 需 功 能 * * * * * * * * \n\n\t\t 1.继续借阅\n\t\t 2.返回首页\n\n* * * * * * * * * * * * * * * * * * * * * * * * * *\n')
pick = Pick(2)
if pick == 1:
continue
else:
return desktop.root_menu()
#6.归还登记
def book_return():
global readers
global books
while True:
while True:
try:
sno = int(input('请输入学号:'))
break
except:
print('请输入数字!')
name = input('请输入姓名:')
major = input('请输入专业名:')
newdf = pd.DataFrame(readers[readers['学号']==sno].groupby(by=['书籍编号','书籍名称','作者'],as_index=False)['数量'].agg(sum)).rename(columns={'数量':'未归还数量'})
newdf = newdf[newdf['未归还数量']>0]
if len(newdf) == 0:
print('您未借阅过任何书籍!')
else:
print(newdf)
while True:
try:
query_y = int(input('请输入书籍编号:'))
break
except:
print('请输入数字!')
newdf_ = newdf.loc[newdf['书籍编号']==query_y]
if len(newdf_)==0:
print('书籍不存在!')
print('\n* * * * * * * * 请 选 择 所 需 功 能 * * * * * * * * \n\n\t\t 1.重新输入\n\t\t 2.返回首页\n\n* * * * * * * * * * * * * * * * * * * * * * * * * *\n')
pick = Pick(2)
if pick == 1:
continue
else:
return desktop.root_menu()
else:
print(newdf_)
print('\n* * * * * * * * 请 选 择 所 需 功 能 * * * * * * * * \n\n\t\t 1.归还书籍\n\t\t 2.重新输入\n\t\t 3.返回首页\n\n* * * * * * * * * * * * * * * * * * * * * * * * * *\n')
pick = Pick(3)
if pick == 1:
while True:
try:
booknum = int(input('请输入归还数量:'))
if booknum < 1:
print('归还数量不能小于1!')
elif booknum > newdf_['未归还数量'].values:
print('归还数量不能多于借出的数量!')
else:
break
except:
print('请输入数字!')
#归还登记 readers.csv
print('正在归还......')
readers0 = pd.DataFrame([{
'学号':sno,
'姓名':name,
'专业':major,
'书籍编号':query_y,
'书籍名称':(books.loc[books['书籍编号']==query_y]['书籍名称'].values)[0],
'作者':(books.loc[books['书籍编号']==query_y]['作者'].values)[0],
'数量':-booknum,
'借阅/归还':'归还',
'时间':time.strftime("%Y-%m-%d %H:%M:%S")
}])
readers = pd.concat([readers,readers0]).reset_index(drop=True)
readers.to_csv(resource_path(os.path.join("res",'readers.csv')),index=False)
#bookS表改变
books.loc[books['书籍编号'] == query_y,'可借'] += booknum
books.to_csv(resource_path(os.path.join("res",'books.csv')),index=False)
print('归还成功!')
print('\n* * * * * * * * 请 选 择 所 需 功 能 * * * * * * * * \n\n\t\t 1.继续归还\n\t\t 2.返回首页\n\n* * * * * * * * * * * * * * * * * * * * * * * * * *\n')
pick = Pick(2)
if pick == 1:
continue
else:
return desktop.root_menu()
elif pick == 2:
continue
else:
return desktop.root_menu()
#7.读者信息
def readers_news():
#读者可以通过这个找回丢失的密码
global user
while True:
while True:
try:
u_id = int(input('请输入用户学号:'))
break
except:
print('请输入数字!')
name = input('请输入用户姓名:')
major = input('请输入用户专业名:')
#查找
user_ = user.loc[(user['学号'] == u_id )].loc[user['姓名']==name].loc[user['专业'] == major]
if len(user_)==0:
print('用户不存在!')
print('\n* * * * * * * * 请 选 择 所 需 功 能 * * * * * * * * \n\n\t\t 1.重新输入\n\t\t 2.返回首页\n\n* * * * * * * * * * * * * * * * * * * * * * * * * *\n')
pick = Pick(2)
if pick == 1:
continue
else:
return desktop.root_menu()
else:
print(tabulate(user_, headers = 'keys', tablefmt = 'pretty'))
#可以对学生信息进行修改
print('\n* * * * * * * * 请 选 择 所 需 功 能 * * * * * * * * \n\n\t\t 1.修改邮箱\n\t\t 2.修改密码\n\t\t 3.返回首页\n\n* * * * * * * * * * * * * * * * * * * * * * * * * *\n')
pick = Pick(3)
if pick == 1 :
while True:
try:
new_email = input('请输入新的邮箱账号:')
code_x = main_desktop.to_email(new_email,0)
while True:
code_y = input('请输入验证码:')
if code_x == code_y:
print('正在修改.......')
user.loc[(user['学号'] == u_id) & (user['姓名']==name) &(user['专业'] == major),'邮箱'] = new_email
print('修改成功!')
pick = 0 #退出
break
else:
print('验证码输入错误!')
print('\n* * * * * * * * 请 选 择 所 需 功 能 * * * * * * * * \n\n\t\t 1.重新输入邮箱\n\t\t 2.重新输入验证码\n\t\t 3.返回首页\n\n* * * * * * * * * * * * * * * * * * * * * * * * * *\n')
pick = Pick(3)
if pick == 1:
break
elif pick == 2:
continue
else:
return desktop.root_menu()
except:
print('邮箱账号错误!')
print('\n* * * * * * * * 请 选 择 所 需 功 能 * * * * * * * * \n\n\t\t 1.重新输入\n\t\t 2.返回首页\n\n* * * * * * * * * * * * * * * * * * * * * * * * * *\n')
pick = Pick(2)
if pick == 1:
continue
else:
return desktop.root_menu()
if pick == 2:
continue
elif pick == 0:
break
elif pick == 2:
while True:
new_password = input('请输入密码:')
if (len(new_password)<6) or (len(new_password)>10):
print('密码应该长度大于6且小于10')
else:
if main_desktop.count(new_password): #正确就退出
break
user.loc[(user['学号'] == u_id) & (user['姓名']==name) &(user['专业'] == major),'密码'] = new_password
user.to_csv(resource_path(os.path.join("res",'user.csv')),index=False)
print('修改成功!')
print('\n* * * * * * * * 请 选 择 所 需 功 能 * * * * * * * * \n\n\t\t 1.继续修改\n\t\t 2.返回首页\n\n* * * * * * * * * * * * * * * * * * * * * * * * * *\n')
pick = Pick(2)
if pick == 1:
continue
else:
return desktop.root_menu()
def sign_out():
return desktop.home_page()
三、读者系统
功能
1.书籍查询
2.自主还书
3.个人信息
4.自主借阅
类似于管理员系统
代码
#user_function.py
#用于存放读者的各种功能
from tabulate import tabulate
import pandas as pd
import desktop
import main_desktop
import time
import sys
import os
#生成资源文件目录访问路径
def resource_path(relative_path):
if getattr(sys, 'frozen', False): #是否Bundle Resource
base_path = sys._MEIPASS
else:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
#访问res文件夹下数据
#各种表格
user = pd.read_csv(resource_path(os.path.join("res","user.csv"))) #读者账号表
books = pd.read_csv(resource_path(os.path.join("res","books.csv"))) #书籍表
readers = pd.read_csv(resource_path(os.path.join("res","readers.csv"))) #读者借阅\归还记录
#选择编码
def Pick(number):
while True:
try:
pick = int(input('请输入需要的编号:'))
if pick not in [i for i in range(1,number+1)]:
print('编号无效!请重新输入!')
continue
else:
break
except:
print('请输入数字!')
return pick
#1.书籍查询
def book_query():
while True:
print('\n* * * * * * * * 请 选 择 查 询 方 式 * * * * * * * * \n\n\t\t 1.按书籍编号查询\n\t\t 2.按书籍名称查询\n\t\t 3.按作者查询\n\n* * * * * * * * * * * * * * * * * * * * * * * * * *\n')
query_x = Pick(3)
if query_x==1:
while True:
try:
query_y = int(input('请输入查询信息:'))
break
except:
print('请输入数字!')
book_ = books.loc[books['书籍编号']==int(query_y)]
elif query_x==2:
query_y = input('请输入查询信息:')
book_ = books[books.书籍名称.str.contains(query_y)]
elif query_x==3:
query_y = input('请输入查询信息:')
book_ = books[books.作者.str.contains(query_y)]
if len(book_)==0:
print('查询书籍不存在!')
else:
print('\n')
print(tabulate(book_, headers = 'keys', tablefmt = 'pretty'))
print('\n* * * * * * * * 请 选 择 所 需 功 能 * * * * * * * * \n\n\t\t 1.重新查询\n\t\t 2.返回首页\n\n* * * * * * * * * * * * * * * * * * * * * * * * * *\n')
num = Pick(2)
if num == 1:
continue
else:
return desktop.user_menu()
#2.自主还书(先打印这个读者借阅的书籍数据)
def book_return(id):
global readers
global books
while True:
newdf = pd.DataFrame(readers[readers['学号']==id].groupby(by=['书籍编号','书籍名称','作者'],as_index=False)['数量'].agg(sum)).rename(columns={'数量':'未归还数量'})
newdf = newdf[newdf['未归还数量']>0]
if len(newdf) == 0:
print('您未借阅过任何书籍!')
else:
print(tabulate(newdf, headers = 'keys', tablefmt = 'pretty'))
while True:
try:
query_y = int(input('请输入要归还的书籍编号:'))
break
except:
print('请输入数字!')
newdf_ = newdf.loc[newdf['书籍编号']==query_y]
if len(newdf_)==0:
print('您未借阅该书籍!')
print('\n* * * * * * * * 请 选 择 所 需 功 能 * * * * * * * * \n\n\t\t 1.重新输入\n\t\t 2.返回首页\n\n* * * * * * * * * * * * * * * * * * * * * * * * * *\n')
pick = Pick(2)
if pick == 1:
continue
else:
return desktop.user_menu()
else:
print(tabulate(newdf_, headers = 'keys', tablefmt = 'pretty'))
print('\n* * * * * * * * 请 选 择 所 需 功 能 * * * * * * * * \n\n\t\t 1.归还书籍\n\t\t 2.重新输入\n\t\t 3.返回首页\n\n* * * * * * * * * * * * * * * * * * * * * * * * * *\n')
pick = Pick(3)
if pick == 1:
while True:
try:
booknum = int(input('请输入归还数量:'))
if booknum < 1:
print('归还数量不能小于1!')
elif booknum > newdf_['未归还数量'].values:
print('归还数量不能多于借出的数量!')
else:
break
except:
print('请输入数字!')
#归还登记 readers.csv
print('正在归还......')
readers0 = pd.DataFrame([{
'学号':id,
'姓名':user.loc[user['学号']==id]['姓名'][0],
'专业':user.loc[user['学号']==id]['专业'][0],
'书籍编号':query_y,
'书籍名称':(books.loc[books['书籍编号']==query_y]['书籍名称'].values)[0],
'作者':(books.loc[books['书籍编号']==query_y]['作者'].values)[0],
'数量':-booknum,
'借阅/归还':'归还',
'时间':time.strftime("%Y-%m-%d %H:%M:%S")
}])
readers = pd.concat([readers,readers0]).reset_index(drop=True)
readers.to_csv(resource_path(os.path.join("res",'readers.csv')),index=False)
#bookS表改变
books.loc[books['书籍编号'] == query_y,'可借'] += booknum
books.to_csv(resource_path(os.path.join("res",'books.csv')),index=False)
print('归还成功!')
print('\n* * * * * * * * 请 选 择 所 需 功 能 * * * * * * * * \n\n\t\t 1.继续归还\n\t\t 2.返回首页\n\n* * * * * * * * * * * * * * * * * * * * * * * * * *\n')
pick = Pick(2)
if pick == 1:
continue
else:
return desktop.user_menu()
elif pick == 2:
continue
else:
return desktop.user_menu()
#3.个人信息
def readers_news(id):
#读者可以通过这个找更改密码
global user
#查找
while True:
user_ = user.loc[(user['学号'] == id )]
print(tabulate(user_, headers = 'keys', tablefmt = 'pretty'))
#可以对学生信息进行修改
print('\n* * * * * * * * 请 选 择 所 需 功 能 * * * * * * * * \n\n\t\t 1.修改邮箱\n\t\t 2.修改密码\n\t\t 3.返回首页\n\n* * * * * * * * * * * * * * * * * * * * * * * * * *\n')
pick = Pick(3)
if pick == 1 :
while True:
try:
new_email = input('请输入新的邮箱账号:')
code_x = main_desktop.to_email(new_email,0)
while True:
code_y = input('请输入验证码:')
if code_x == code_y:
print('正在修改.......')
user.loc[(user['学号'] == id),'邮箱'] = new_email
print('修改成功!')
pick = 0 #退出
break
else:
print('验证码输入错误!')
print('\n* * * * * * * * 请 选 择 所 需 功 能 * * * * * * * * \n\n\t\t 1.重新输入邮箱\n\t\t 2.重新输入验证码\n\t\t 3.返回首页\n\n* * * * * * * * * * * * * * * * * * * * * * * * * *\n')
pick = Pick(3)
if pick == 1:
break
elif pick == 2:
continue
else:
return desktop.user_menu()
except:
print('邮箱账号错误!')
print('\n* * * * * * * * 请 选 择 所 需 功 能 * * * * * * * * \n\n\t\t 1.重新输入\n\t\t 2.返回首页\n\n* * * * * * * * * * * * * * * * * * * * * * * * * *\n')
pick = Pick(2)
if pick == 1:
continue
else:
return desktop.user_menu()
if pick == 2:
continue
elif pick == 0:
break
elif pick == 2:
while True:
new_password = input('请输入密码:')
if (len(new_password)<6) or (len(new_password)>10):
print('密码应该长度大于6且小于10')
else:
if main_desktop.count(new_password): #正确就退出
break
user.loc[(user['学号'] == id),'密码'] = new_password
user.to_csv(resource_path(os.path.join("res",'user.csv')),index=False)
print('修改成功!')
print('\n* * * * * * * * 请 选 择 所 需 功 能 * * * * * * * * \n\n\t\t 1.继续修改\n\t\t 2.返回首页\n\n* * * * * * * * * * * * * * * * * * * * * * * * * *\n')
pick = Pick(2)
if pick == 1:
continue
else:
return desktop.user_menu()
else:
return desktop.user_menu()
#4.自主借阅
def book_borrow(id):
global books
global readers
while True:
while True:
try:
query_y = int(input('请输入书籍编号:'))
break
except:
print('请输入数字!')
book_ = books.loc[books['书籍编号']==query_y]
if len(book_)==0:
print('书籍不存在!')
print('\n* * * * * * * * 请 选 择 所 需 功 能 * * * * * * * * \n\n\t\t 1.重新输入\n\t\t 2.返回首页\n\n* * * * * * * * * * * * * * * * * * * * * * * * * *\n')
pick = Pick(2)
if pick == 1:
return book_borrow()
else:
return desktop.root_menu()
else:
print('\n')
print(tabulate(book_, headers = 'keys', tablefmt = 'pretty'))
print('\n')
#可借为0
if book_['可借'].values==0 :
print('书籍数量为0,无法借阅!')
else:
print('\n* * * * * * * * 请 选 择 所 需 功 能 * * * * * * * * \n\n\t\t 1.借阅书籍\n\t\t 2.重新输入\n\t\t 3.返回首页\n\n* * * * * * * * * * * * * * * * * * * * * * * * * *\n')
pick = Pick(3)
if pick == 1:
while True:
try:
booknum = int(input('请输入借阅数量:'))
if booknum < 1:
print('借阅数量不能小于1!')
elif booknum > book_['可借'].values:
print('借阅数量不能多于可借书籍!')
else:
break
except:
print('请输入数字!')
#记录借阅 readers.csv
print('正在借阅......')
readers0 = pd.DataFrame([{
'学号':id,
'姓名':user.loc[user['学号']==id]['姓名'][0],
'专业':user.loc[user['学号']==id]['专业'][0],
'书籍编号':query_y,
'书籍名':(books.loc[books['书籍编号']==query_y]['书籍名称'].values)[0],
'作者':(books.loc[books['书籍编号']==query_y]['作者'].values)[0],
'数量':booknum,
'借阅/归还':'借阅',
'时间':time.strftime("%Y-%m-%d %H:%M:%S")
}])
readers = pd.concat([readers,readers0]).reset_index(drop=True)
readers.to_csv(resource_path(os.path.join("res",'readers.csv')),index=False)
#书籍表更改
books.loc[books['书籍编号'] == query_y,'可借'] -= booknum
books.to_csv(resource_path(os.path.join("res",'books.csv')),index=False)
elif pick == 2:
continue
else:
return desktop.root_menu()
print('借阅成功!')
print('\n* * * * * * * * 请 选 择 所 需 功 能 * * * * * * * * \n\n\t\t 1.继续借阅\n\t\t 2.返回首页\n\n* * * * * * * * * * * * * * * * * * * * * * * * * *\n')
pick = Pick(2)
if pick == 1:
continue
else:
return desktop.root_menu()
#5.退出登录
def sign_out():
return desktop.home_page()