文章目录
- 一 实现功能
- (1)管理员功能
- (2)读者功能
- 二 数据流图
- 三 概念结构设计
- 四 文件目录
- 五 源码:main.py
- 六 运行截图
一 实现功能
(1)管理员功能
- 一个管理员编号对应一个密码,且需要有管理员注册密匙。
- 可以在图书信息表中录入、修改、删除图书。
- 可以在图书信息表中查询书籍。
- 可以编辑图书借阅、归还信息。
- 可以编辑欠款信息。
- 可以编辑读者信息表。
(2)读者功能
- 图书馆注册,获得读者编号。
- 可以在图书信息表中查阅书籍。
- 可以查看自己的借阅、归还和欠款信息,不能查看他人的。
- 可以自主借阅图书,将改变该书的借阅状态。
- 可以归还图书,系统自动计算应交的罚金。
- 向管理员提交罚金后,管理员更新欠款信息表。
二 数据流图
三 概念结构设计
四 文件目录
五 源码:main.py
from sqlite3 import *
from tkinter import *
from tkinter.messagebox import *import datetime
import time# 初始化图形界面
win=Tk() # 创建窗口
win.geometry('600x600+200+200') # 设置窗口大小和位置rt.geometry('宽 x 高+左边距+上边距')
win.title('图书馆') # 设置窗口名
win.iconbitmap('pro.ico') # 设置窗口图标(左上角)
# 把背景图放到一个标签里再显示到屏幕上
background=PhotoImage(file='bg.gif') # 画布显示图像图像对象=PhotoImage(file=图像名称)必须是gif图片
lab1=Label(image=background) # 创建标签
lab1.place(x=0,y=0)
# 主要标签框架
manfra=LabelFrame() # 标签框架
manfra.pack() # 控件摆放
# 存数据库名
dbfile='Library.dat'# 返回主界面
def back_main():menubar = Menu(win)win.config(menu=menubar)file_back=Menu(menubar, tearoff=0)file_back.add_command(label='返回',command=identity)menubar.add_cascade(label='主页面',menu=file_back)# 读者
def Reader():cid='0'# 查找读者(根据读者编号找)def find(key_id):cn = connect(dbfile)cur = cn.execute('select * from Readers where R_id=?', (key_id,))user = cur.fetchall()if len(user) > 0:n = 1else:n = -1cn.close()return n# 匹配读者编号和密码def match(key_id, key_sec):cn = connect(dbfile)cur = cn.execute('select R_secret from Readers where R_id=?', (key_id,))s = cur.fetchall()x=" ".join('%s' %i for i in s) # s是列表,先转化为字符串再比较if x == key_sec:n = 1else:n = -1cn.close()return n# 读者注册def Reader_sign():global manframanfra.destroy()manfra = LabelFrame(text='读者注册')manfra.pack(anchor=CENTER, pady=120, ipadx=5, ipady=5)## 输入区域框架tf = Frame(manfra) # 定义放置控件的框架tf.pack()## 输入需要添加的读者编号Label(tf, text='编号', anchor=E).grid(row=1, column=1)R_id = StringVar()txtcno = Entry(tf, textvariable=R_id)txtcno.grid(row=1, column=2)## 输入需要添加的姓名Label(tf, text='姓名', anchor=E).grid(row=2, column=1)R_name = StringVar()txtcna = Entry(tf, textvariable=R_name)txtcna.grid(row=2, column=2)## 输入需要添加的读者密码Label(tf, text='密码', anchor=E).grid(row=3, column=1)R_secret = StringVar()txtcns = Entry(tf, textvariable=R_secret)txtcns.grid(row=3, column=2)## 输入需要添加的读者电话Label(tf, text='电话', anchor=E).grid(row=4, column=1)R_telephone = StringVar()txtcnt = Entry(tf, textvariable=R_telephone)txtcnt.grid(row=4, column=2)# 添加清空按钮区域框架tf2 = Frame(manfra)tf2.pack()btok = Button(tf2, text='确定') # 确定按钮btok.grid(row=1, column=1)btclear = Button(tf2, text='清空') # 清空按钮btclear.grid(row=1, column=2)# 清空已经输入的def clearall():R_id.set('')R_name.set('')R_secret.set('')R_telephone.set('')# 读者注册def savenew():global cidcid = R_id.get()cna = R_name.get()csec = R_secret.get()ctel = R_telephone.get()if cid == '':showerror('读者注册', '编号无效!')else:if find(cid) == 1:showerror('读者注册', '编号已经存在!')txtcno.focus() # 光标集中到户名输入框else:if cna == '':showerror('读者注册', '名字输入无效!')txtcna.focus()else:if csec=='':showerror('读者注册', '密码输入无效!')txtcns.focus()else:cn = connect(dbfile)cn.execute('insert into Readers(R_id,R_name,R_secret,R_telephone)\values(?,?,?,?)',(cid, cna, csec, ctel))cn.commit()cn.close()showinfo('读者注册', '注册成功!')global manframanfra.destroy()manfra = LabelFrame(text='亲爱的读者')manfra.pack(anchor='center', pady=150, ipadx=10, ipady=10)Label(manfra, text='欢迎来到图书馆~', anchor=E).grid(row=1, column=1)Reader_menu()# 按钮命令触发btclear.config(command=clearall) # 清空输入框按钮触发btok.config(command=savenew) # 增加读者按钮触发# 读者登录def Reader_log():global manframanfra.destroy()manfra = LabelFrame(text='读者登录')manfra.pack(anchor=CENTER, pady=120, ipadx=5, ipady=5)## 输入区域框架tf = Frame(manfra) # 定义放置控件的框架tf.pack()## 输入读者编号Label(tf, text='读者编号', anchor=E).grid(row=1, column=1)R_id = StringVar()txtcno = Entry(tf, textvariable=R_id)txtcno.grid(row=1, column=2)## 输入密码Label(tf, text='密码', anchor=E).grid(row=2, column=1)R_secret = StringVar()txtcns = Entry(tf, textvariable=R_secret)txtcns.grid(row=2, column=2)# 添加按钮区域框架tf2 = Frame(manfra)tf2.pack()btok = Button(tf2, text='确定') # 确定按钮btok.grid(row=1, column=1)btclear = Button(tf2, text='清空') # 清空按钮btclear.grid(row=1, column=2)# 确定登录后def sign_in():global cidcid = R_id.get()cse = R_secret.get()if cid == '':showerror('读者登录', '编号无效!')txtcno.focus()else:if find(cid) < 0:showerror('读者登录', '编号不存在,请重新输入!')txtcno.focus()else:if cse == '':showerror('读者登录', '密码无效!')txtcns.focus()else:if match(cid, cse) < 0:showerror('读者登录', '密码错误!')else:showinfo('读者登录', '登录成功!')global manframanfra.destroy()manfra = LabelFrame(text='亲爱的读者')manfra.pack(anchor=CENTER, pady=150, ipadx=10, ipady=10)Label(manfra, text='欢迎来到图书馆~', anchor=E).grid(row=1, column=1)Reader_menu()# 清空已经输入的def clearall():R_id.set('')R_secret.set('')# 按钮命令触发btclear.config(command=clearall) # 清空输入框按钮触发btok.config(command=sign_in) # 增加读者按钮触发# 阅览图书def Read_Books():global manfra # 标签的父级对象,本函数中就是该标签放置的位置(标签框架)cn = connect(dbfile) # 连接数据库cur = cn.execute('select * from Books')Bo = cur.fetchall() # 获取所有记录cn.close() # 关闭数据库manfra.destroy() # 需要销毁控件时触发## 开始输出读者信息if len(Bo) == 0:showwarning('书籍管理', '没有书籍!') # 警告信息else:## 设置表格大小、位置manfra = LabelFrame(text='图书信息') # 标签框架manfra.pack(anchor='center', pady=50, ipadx=5, ipady=5) # 标签框架摆放位置# 改变表格框的尺寸columnconfigure(表格框的列,weight=缩放权重,minsuze=最小宽度)manfra.columnconfigure(1, minsize=50)manfra.columnconfigure(2, minsize=70)manfra.columnconfigure(3, minsize=100)manfra.columnconfigure(4, minsize=80)manfra.columnconfigure(5, minsize=100)manfra.columnconfigure(6, minsize=80)manfra.columnconfigure(7, minsize=50)# 在标签框架里创建标签,sticky是对齐方法Label(manfra, text='序号',font=('微软雅黑', 9, 'normal'), bd=1,relief=SOLID).grid(row=1, column=1, sticky=N + E + S + W)Label(manfra, text='编号',font=('微软雅黑', 9, 'normal'), bd=1,relief=SOLID).grid(row=1, column=2, sticky=N + E + S + W)Label(manfra, text='书名',font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=1, column=3, sticky=N + E + S + W)Label(manfra, text='作者',font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=1, column=4, sticky=N + E + S + W)Label(manfra, text='出版社',font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=1, column=5, sticky=N + E + S + W)Label(manfra, text='类别',font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=1, column=6, sticky=N + E + S + W)Label(manfra, text='状态',font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=1, column=7, sticky=N + E + S + W)rn = 2 # 记录行for x in Bo: # 循环每行cn = 1 # 记录列Label(manfra, text=str(rn - 1),font=('微软雅黑', 9, 'bold'), bd=1,relief=SOLID).grid(row=rn, column=cn, sticky=N + E + S + W)for a in x: # 循环每列cn += 1Label(manfra, text=str(a),font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=rn, column=cn, sticky=N + E + S + W)rn += 1# 查找书def Fine_Book():global manframanfra.destroy()manfra = LabelFrame(text='查找图书')manfra.pack(anchor='center', pady=50, ipadx=5, ipady=5)## 把标签框架manfra细分,即在manfra中再设置标签框架tf = LabelFrame(manfra)tf.pack(anchor='center', pady=10, ipadx=5, ipady=5)Label(tf, text='输入书名', anchor=E).grid(row=1, column=1)cno = StringVar() # 能自动刷新的字符串变量,能获取文本框中输入的值txtcno = Entry(tf, textvariable=cno) # 定义单行输入框txtcno.grid(row=1, column=2) # 设置单行输入框大小def dofind():bookname = cno.get()if bookname == '':showerror('查找图书', '书名无效!')else:cn = connect(dbfile)cur = cn.execute('select * from Books where B_name=?', (bookname,))inf = cur.fetchall()if len(inf) <= 0:showerror('查找图书', '查无此书!')else:global manframanfra.destroy()## 设置表格大小、位置manfra = LabelFrame(text='查询结果') # 标签框架manfra.pack(anchor='center', pady=20, ipadx=5, ipady=5) # 标签框架摆放位置# 改变表格框的尺寸columnconfigure(表格框的列,weight=缩放权重,minsuze=最小宽度)manfra.columnconfigure(1, minsize=50)manfra.columnconfigure(2, minsize=70)manfra.columnconfigure(3, minsize=100)manfra.columnconfigure(4, minsize=80)manfra.columnconfigure(5, minsize=100)manfra.columnconfigure(6, minsize=80)manfra.columnconfigure(7, minsize=50)# 在标签框架里创建标签,sticky是对齐方法Label(manfra, text='序号',font=('微软雅黑', 9, 'normal'), bd=1,relief=SOLID).grid(row=1, column=1, sticky=N + E + S + W)Label(manfra, text='编号',font=('微软雅黑', 9, 'normal'), bd=1,relief=SOLID).grid(row=1, column=2, sticky=N + E + S + W)Label(manfra, text='书名',font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=1, column=3, sticky=N + E + S + W)Label(manfra, text='作者',font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=1, column=4, sticky=N + E + S + W)Label(manfra, text='出版社',font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=1, column=5, sticky=N + E + S + W)Label(manfra, text='类别',font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=1, column=6, sticky=N + E + S + W)Label(manfra, text='状态',font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=1, column=7, sticky=N + E + S + W)rn = 2 # 记录行for x in inf: # 循环每行cn = 1 # 记录列Label(manfra, text=str(rn - 1),font=('微软雅黑', 9, 'bold'), bd=1,relief=SOLID).grid(row=rn, column=cn, sticky=N + E + S + W)for a in x: # 循环每列cn += 1Label(manfra, text=str(a),font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=rn, column=cn, sticky=N + E + S + W)rn += 1btok = Button(tf, text='查找') # 定义按钮btok.grid(row=1, column=3) # 设置按钮位置## 触发命令按钮btok.config(command=dofind) # 查找按钮触发# 欠款信息def Read_Fine():global manfra # 标签的父级对象,本函数中就是该标签放置的位置(标签框架)manfra.destroy()manfra=LabelFrame(text='欠款信息')manfra.pack(anchor='center', pady=50, ipadx=5, ipady=5)global cidcn = connect(dbfile) # 连接数据库cur = cn.execute('select * from Fine where R_id=?',(cid,))Rea = cur.fetchall() # 获取所有记录cn.close() # 关闭数据库## 开始输出读者信息if len(Rea) == 0:showwarning('欠款管理', '暂无欠款信息!') # 警告信息else:manfra.destroy()## 设置表格大小、位置manfra = LabelFrame(text='欠款信息') # 标签框架manfra.pack(anchor='center', pady=50, ipadx=5, ipady=5) # 标签框架摆放位置# 改变表格框的尺寸columnconfigure(表格框的列,weight=缩放权重,minsuze=最小宽度)manfra.columnconfigure(1, minsize=80)manfra.columnconfigure(2, minsize=100)manfra.columnconfigure(3, minsize=100)manfra.columnconfigure(4, minsize=100)# 在标签框架里创建标签,sticky是对齐方法Label(manfra, text='读者编号',font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=1, column=1, sticky=N + E + S + W)Label(manfra, text='总罚款',font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=1, column=2, sticky=N + E + S + W)Label(manfra, text='已交罚款',font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=1, column=3, sticky=N + E + S + W)Label(manfra, text='未交罚款',font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=1, column=4, sticky=N + E + S + W)rn = 2 # 记录行for x in Rea: # 循环每行cn = 1 # 记录列Label(manfra, text=str(rn - 1),font=('微软雅黑', 9, 'bold'), bd=1,relief=SOLID).grid(row=rn, column=cn, sticky=N + E + S + W)for a in x: # 循环每列Label(manfra, text=str(a),font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=rn, column=cn, sticky=N + E + S + W)cn += 1rn += 1# 借阅信息def Read_Borrow():global manfra # 标签的父级对象,本函数中就是该标签放置的位置(标签框架)manfra.destroy()manfra = LabelFrame(text='借阅信息')manfra.pack(anchor='center', pady=50, ipadx=5, ipady=5)global cidcn = connect(dbfile) # 连接数据库cur = cn.execute('select * from Borrow where R_id=?', (cid,))Rea = cur.fetchall() # 获取所有记录cn.close() # 关闭数据库manfra.destroy() # 需要销毁控件时触发## 开始输出读者信息if len(Rea) == 0:showwarning('借阅管理', '暂无借阅信息!') # 警告信息else:## 设置表格大小、位置manfra = LabelFrame(text='借阅信息') # 标签框架manfra.pack(anchor='center', pady=50, ipadx=5, ipady=5) # 标签框架摆放位置# 改变表格框的尺寸columnconfigure(表格框的列,weight=缩放权重,minsuze=最小宽度)manfra.columnconfigure(1, minsize=30)manfra.columnconfigure(2, minsize=50)manfra.columnconfigure(3, minsize=50)manfra.columnconfigure(4, minsize=100)manfra.columnconfigure(5, minsize=100)manfra.columnconfigure(6, minsize=100)manfra.columnconfigure(7, minsize=80)# 在标签框架里创建标签,sticky是对齐方法Label(manfra, text='序号',font=('微软雅黑', 9, 'normal'), bd=1,relief=SOLID).grid(row=1, column=1, sticky=N + E + S + W)Label(manfra, text='书籍编号',font=('微软雅黑', 9, 'normal'), bd=1,relief=SOLID).grid(row=1, column=2, sticky=N + E + S + W)Label(manfra, text='读者编号',font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=1, column=3, sticky=N + E + S + W)Label(manfra, text='借阅日期',font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=1, column=4, sticky=N + E + S + W)Label(manfra, text='应还日期',font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=1, column=5, sticky=N + E + S + W)Label(manfra, text='实际归还日期',font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=1, column=6, sticky=N + E + S + W)Label(manfra, text='罚款',font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=1, column=7, sticky=N + E + S + W)rn = 2 # 记录行for x in Rea: # 循环每行cn = 1 # 记录列Label(manfra, text=str(rn - 1),font=('微软雅黑', 9, 'bold'), bd=1,relief=SOLID).grid(row=rn, column=cn, sticky=N + E + S + W)for a in x: # 循环每列cn += 1Label(manfra, text=str(a),font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=rn, column=cn, sticky=N + E + S + W)rn += 1# 借书def Borrow():global manframanfra.destroy()manfra = LabelFrame(text='借阅图书')manfra.pack(anchor='center', pady=50, ipadx=5, ipady=5)Label(manfra, text='输入书号', anchor=E).grid(row=1, column=1)cno = StringVar() # 能自动刷新的字符串变量,能获取文本框中输入的值txtcno = Entry(manfra, textvariable=cno) # 定义单行输入框txtcno.grid(row=1, column=2) # 设置单行输入框大小def BB():global cidbookid=cno.get()cn = connect(dbfile) # 连接数据库cur = cn.execute('select B_state from Books where B_id=?', (bookid,))bookstate = cur.fetchall() # 获取所有记录if bookid=='':showerror('借阅书籍','书号错误!')txtcno.focus()else:if len(bookstate)<=0:showerror('借阅书籍','无此书号!')txtcno.focus()else:state=[(0,)]if state==bookstate:showerror('借阅书籍','已借出!')txtcno.focus()else:cur=cn.execute('select * from Books where B_id=?',(bookid,))inf=cur.fetchall()Label(manfra, text=inf, anchor=E).grid(row=2, column=2)# 更新书信息borrowdate=datetime.date.today()returndate=borrowdate+datetime.timedelta(days=30)cn.execute('update Books set B_state=?\where B_id=?', (0, bookid))cn.execute('insert into Borrow(B_id,R_id,Borrow_date,Return_date)\values(?,?,?,?)',(bookid,cid,borrowdate,returndate))cn.commit()cn.close()showinfo('借阅书籍', '借阅成功!\n注意:30天内免费借阅,超过30天罚款10元,每超过一天增加1元!')btok = Button(manfra, text='借阅') # 定义按钮btok.grid(row=1, column=3) # 设置按钮位置## 触发命令按钮btok.config(command=BB) # 按钮触发# 还书def Return():global manframanfra.destroy()manfra = LabelFrame(text='归还图书')manfra.pack(anchor='center', pady=50, ipadx=5, ipady=5)Label(manfra, text='输入书号', anchor=E).grid(row=1, column=1)Label(manfra, text='还书日期', anchor=E).grid(row=2, column=1)cno = StringVar() # 能自动刷新的字符串变量,能获取文本框中输入的值txtcno = Entry(manfra, textvariable=cno) # 定义单行输入框txtcno.grid(row=1, column=2) # 设置单行输入框大小cdate=StringVar()txtcdate=Entry(manfra,textvariable=cdate)txtcdate.grid(row=2,column=2)def RR():global cidbookid = cno.get()realdate = cdate.get()cn = connect(dbfile) # 连接数据库cur = cn.execute('select B_state from Books where B_id=?', (bookid,))bookstate = cur.fetchall() # 获取所有记录if bookid == '':showerror('归还书籍', '书号错误!')txtcno.focus()else:if realdate=='':showerror('归还书籍','日期错误!')txtcdate.focus()else:state=[(1,)]if bookstate == state:showerror('借阅书籍', '本书未借出!')txtcno.focus()else:cur = cn.execute('select * from Books where B_id=?', (bookid,))inf = cur.fetchall()Label(manfra, text=inf, anchor=E).grid(row=2, column=2)# 更新书信息realdate = datetime.date(*map(int, realdate.split('-')))cur = cn.execute('select Return_date from Borrow where B_id=?', (bookid,))returndate = cur.fetchall()returndate = datetime.date(*map(int, returndate[0][0].split('-')))if returndate>realdate:realfine=0else:day=(realdate-returndate).daysrealfine=10+int(day)cn.execute('update Books set B_state=?\where B_id=?', (1, bookid))cn.execute('update Borrow set Real_date=?,Fine_real=? where B_id=?', (realdate,realfine,bookid))cur=cn.execute('select Fine_all from Fine where R_id=? ',(cid,))inf=cur.fetchall()if len(inf)<=0:cn.execute('insert into Fine(R_id,Fine_all,Fine_yes,Fine_no) values(?,?,?,?)',(cid,realfine,0,realfine))else:infall=int(inf[0][0])+realfinecur = cn.execute('select Fine_no from Fine where R_id=? ', (cid,))inf = cur.fetchall()infno=int(inf[0][0])+realfinecn.execute('update Fine set Fine_all=?,Fine_no=? where R_id=?',(infall,infno,cid))cn.commit()cn.close()showinfo('归还书籍', '还书成功!\n罚款:%s元' %realfine)btok = Button(manfra, text='还书') # 定义按钮btok.grid(row=1, column=3) # 设置按钮位置## 触发命令按钮btok.config(command=RR) # 按钮触发# 交罚金def Give_Fine():global cidglobal manframanfra.destroy()manfra = LabelFrame(text='提交罚金')manfra.pack(anchor='center', pady=50, ipadx=5, ipady=5)cn = connect(dbfile) # 连接数据库cur = cn.execute('select Fine_no from Fine where R_id=?', (cid,))fm = cur.fetchall() # 获取所有记录txt='应交罚金:%s'%fm[0][0]Label(manfra, text=txt, anchor=E).grid(row=1, column=1)Label(manfra, text='提交金额', anchor=E).grid(row=2, column=1)cno = StringVar() # 能自动刷新的字符串变量,能获取文本框中输入的值txtcno = Entry(manfra, textvariable=cno) # 定义单行输入框txtcno.grid(row=2, column=2) # 设置单行输入框大小def FF():global cidmoney = cno.get()cur=cn.execute('select Fine_no from Fine where R_id=?', (cid,))mno=cur.fetchall()mno=mno[0][0]mno=int(mno)-int(money)cur = cn.execute('select Fine_yes from Fine where R_id=?', (cid,))myes = cur.fetchall()myes=myes[0][0]myes = int(money) + int(myes)cn.execute('update Fine set Fine_no=?,Fine_yes=? where R_id=?', (mno,myes,cid))cn.commit()cn.close()showinfo('提交罚金','提交成功!')manfra.destroy()btok = Button(manfra, text='提交') # 定义按钮btok.grid(row=1, column=3) # 设置按钮位置## 触发命令按钮btok.config(command=FF) # 按钮触发# 读者登录后的菜单def Reader_menu():menubar = Menu(win)win.config(menu=menubar)file = Menu(menubar, tearoff=0)file.add_command(label='阅览图书',command=Read_Books)file.add_command(label='查找图书',command=Fine_Book)file.add_separator() # 添加分隔符file.add_command(label='借书',command=Borrow)file.add_command(label='还书',command=Return)file.add_command(label='借阅信息',command=Read_Borrow)file.add_separator()file.add_command(label='提交罚款',command=Give_Fine)file.add_command(label='欠款信息',command=Read_Fine)menubar.add_cascade(label='读者', menu=file)backfile = Menu(menubar, tearoff=0)backfile.add_command(label='主界面', command=identity)menubar.add_cascade(label='退出', menu=backfile)# 读者登录注册主界面global manframanfra.destroy()manfra=LabelFrame(text='读者')manfra.pack(anchor='center', pady=150, ipadx=5, ipady=5)button_log=Button(manfra,text='登录',height=3,width=20)button_log.pack()button_sign=Button(manfra,text='注册',height=3,width=20)button_sign.pack()# 菜单menubar = Menu(win)win.config(menu=menubar)file_back = Menu(menubar, tearoff=0)file_back.add_command(label='返回', command=identity)menubar.add_cascade(label='主页面', menu=file_back)file_signlog=Menu(menubar, tearoff=0)file_signlog.add_command(label='读者登录',command=Reader_log)file_signlog.add_command(label='读者注册',command=Reader_sign)menubar.add_cascade(label='读者', menu=file_signlog)# Reader按钮触发button_sign.config(command=Reader_sign)button_log.config(command=Reader_log)# 管理员
def Manager():# 查找管理员def find(key_id):cn = connect(dbfile)cur = cn.execute('select * from Managers where M_id=?', (key_id,))user = cur.fetchall()if len(user) > 0:n = 1else:n = -1cn.close()return n# 匹配管理员者编号和密码def match(key_id, key_sec):cn = connect(dbfile)cur = cn.execute('select M_secret from Managers where M_id=?', (key_id,))s = cur.fetchall()x = " ".join('%s' % i for i in s) # s是列表,先转化为字符串再比较if x == key_sec:n = 1else:n = -1cn.close()return n# 阅览管理员def Read_Managers():global manfra # 标签的父级对象,本函数中就是该标签放置的位置(标签框架)cn = connect(dbfile) # 连接数据库cur = cn.execute('select * from Managers')Mana = cur.fetchall() # 获取所有记录cn.close() # 关闭数据库manfra.destroy() # 需要销毁控件时触发## 开始输出管理员内容if len(Mana) == 0:showwarning('管理员管理', '没有管理员!') # 警告信息else:## 设置表格大小、位置manfra = LabelFrame(text='管理员信息') # 标签框架manfra.pack(anchor='center', pady=50, ipadx=5, ipady=5) # 标签框架摆放位置# 改变表格框的尺寸columnconfigure(表格框的列,weight=缩放权重,minsuze=最小宽度)manfra.columnconfigure(1, minsize=50)manfra.columnconfigure(2, minsize=80)manfra.columnconfigure(3, minsize=100)manfra.columnconfigure(4, minsize=150)manfra.columnconfigure(5, minsize=150)# 在标签框架里创建标签,sticky是对齐方法Label(manfra, text='序号',font=('微软雅黑', 9, 'normal'), bd=1,relief=SOLID).grid(row=1, column=1, sticky=N + E + S + W)Label(manfra, text='编号',font=('微软雅黑', 9, 'normal'), bd=1,relief=SOLID).grid(row=1, column=2, sticky=N + E + S + W)Label(manfra, text='姓名',font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=1, column=3, sticky=N + E + S + W)Label(manfra, text='电话',font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=1, column=4, sticky=N + E + S + W)Label(manfra, text='密码',font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=1, column=5, sticky=N + E + S + W)rn = 2 # 记录行for x in Mana: # 循环每行cn = 1 # 记录列Label(manfra, text=str(rn - 1),font=('微软雅黑', 9, 'bold'), bd=1,relief=SOLID).grid(row=rn, column=cn, sticky=N + E + S + W)for a in x: # 循环每列cn += 1Label(manfra, text=str(a),font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=rn, column=cn, sticky=N + E + S + W)rn += 1# 查找管理员def find_R(key_id):cn = connect(dbfile)cur = cn.execute('select * from Readers where R_id=?', (key_id,))user = cur.fetchall()if len(user) > 0:n = 1else:n = -1cn.close()return n# 编辑读者def Edit_Readers():## 设置标签框架manfraglobal manframanfra.destroy()manfra = LabelFrame(text='编辑读者')manfra.pack(anchor='center', pady=50, ipadx=5, ipady=5)## 把标签框架manfra细分,即在manfra中再设置标签框架tf = LabelFrame(manfra, text='查找读者')tf.pack(anchor='center', pady=10, ipadx=5, ipady=5)Label(tf, text='输入编号', anchor=E).grid(row=1, column=1)cno = StringVar() # 能自动刷新的字符串变量,能获取文本框中输入的值txtcno = Entry(tf, textvariable=cno) # 定义单行输入框txtcno.grid(row=1, column=2) # 设置单行输入框大小btok = Button(tf, text='查找') # 定义按钮btok.grid(row=1, column=3) # 设置按钮大小## 编辑总标签框架editframe = LabelFrame(manfra, text='编辑读者')editframe.pack(anchor='center', pady=20, ipadx=5, ipady=5)## 删除按钮btdel = Button(editframe, text='删除读者', state=DISABLED)btdel.pack(anchor=NW)## 修改标签框架op = LabelFrame(editframe, text='修改读者')op.pack(anchor='center', pady=10, ipadx=5, ipady=5)## 输入修改的新信息Label(op, text='新编号', anchor=E).grid(row=1, column=1) # 新输入提示标签newmid = StringVar() # 定义可以储存输入信息的变量newtxtmid = Entry(op, textvariable=newmid) # 获取输入框的输入信息newtxtmid.grid(row=1, column=2)Label(op, text='新姓名', anchor=E).grid(row=2, column=1, sticky=E) # 新输入提示标签newmna = StringVar()newtxtmna = Entry(op, textvariable=newmna)newtxtmna.grid(row=2, column=2)Label(op, text='新密码', anchor=E).grid(row=3, column=1, sticky=E) # 新输入提示标签newmse = StringVar()newtxtmse = Entry(op, textvariable=newmse)newtxtmse.grid(row=3, column=2)Label(op, text='新电话', anchor=E).grid(row=4, column=1, sticky=E) # 新输入提示标签newmte = StringVar()newtxtmte = Entry(op, textvariable=newmte)newtxtmte.grid(row=4, column=2)bteditsave = Button(op, text=' 修改', state=DISABLED) # 确定修改的按钮bteditsave.grid(row=1, column=3, rowspan=2, sticky=N + E + S + W)## 判断是否找到def dofind():cid = cno.get()if find_R(cid) == -1:showinfo('读者管理', '%s 没有注册!' % cid)else:# 输出查找到的信息cn = connect(dbfile)cur = cn.execute('select * from Readers where R_id=?', (cid,))inf = cur.fetchall()Label(tf, text=inf, anchor=E).grid(row=2,column=2)btdel.config(state='normal') # 让删除按钮btdel处于默认状态bteditsave.config(state='normal') # 修改按钮处于默认状态## 删除def dodelete():cid = cno.get() # 获取编号if askokcancel('读者管理', "确认删除:%s?" % cid): # 询问是否进行该操作cn = connect(dbfile) # 连接数据库cn.execute('delete from Readers where R_id=?', (cid,)) # 执行删除指令cn.commit()cn.close()showinfo('读者管理', "成功删除:%s" % cid)## 修改def saveedit():cid = cno.get() # 获取编号newcid=newmid.get() # 新编号newname = newmna.get() # 获新姓名newtel=newmte.get()if newname == '':showerror('读者管理', '新姓名错误:%s' % newname)newtxtmna.focus_set() # 将输入新姓名的框设为焦点。焦点是当前正在操作的控件。else:if find(newcid) == 1:if cid!=newcid:showerror('读者管理', '编号 %s 已经存在:' % newcid)newtxtmid.focus_set()else:newse = newmse.get() # 获取新密码if newse == '':showerror('读者管理', '密码无效!')newtxtmse.focus_set()else:cn = connect(dbfile) # 连接数据库cn.execute('update Readers set R_id=?,R_name=?,R_telephone=?,R_secret=?\where R_id=?', (newcid, newname,newtel,newse, cid)) # 更新cn.commit()cn.close()showinfo('读者管理', '修改成功!')## 触发命令按钮btok.config(command=dofind) # 查找按钮触发btdel.config(command=dodelete) # 删除按钮触发bteditsave.config(command=saveedit) # 修改按钮触发# 编辑管理员def Edit_Managers():## 设置标签框架manfraglobal manframanfra.destroy()manfra = LabelFrame(text='编辑管理员')manfra.pack(anchor='center', pady=50, ipadx=5, ipady=5)## 把标签框架manfra细分,即在manfra中再设置标签框架tf = LabelFrame(manfra, text='查找管理员')tf.pack(anchor='center', pady=10, ipadx=5, ipady=5)Label(tf, text='输入编号', anchor=E).grid(row=1, column=1)cno = StringVar() # 能自动刷新的字符串变量,能获取文本框中输入的值txtcno = Entry(tf, textvariable=cno) # 定义单行输入框txtcno.grid(row=1, column=2) # 设置单行输入框大小btok = Button(tf, text='查找') # 定义按钮btok.grid(row=1, column=3) # 设置按钮大小## 编辑总标签框架editframe = LabelFrame(manfra, text='编辑管理员')editframe.pack(anchor='center', pady=20, ipadx=5, ipady=5)## 删除按钮btdel = Button(editframe, text='删除管理员', state=DISABLED)btdel.pack(anchor=NW)## 修改标签框架op = LabelFrame(editframe, text='修改管理员')op.pack(anchor='center', pady=10, ipadx=5, ipady=5)## 输入修改的新信息Label(op, text='新编号', anchor=E).grid(row=1, column=1) # 新输入提示标签newmid = StringVar() # 定义可以储存输入信息的变量newtxtmid = Entry(op, textvariable=newmid) # 获取输入框的输入信息newtxtmid.grid(row=1, column=2)Label(op, text='新姓名', anchor=E).grid(row=2, column=1, sticky=E) # 新输入提示标签newmna = StringVar()newtxtmna = Entry(op, textvariable=newmna)newtxtmna.grid(row=2, column=2)Label(op, text='新密码', anchor=E).grid(row=3, column=1, sticky=E) # 新输入提示标签newmse = StringVar()newtxtmse = Entry(op, textvariable=newmse)newtxtmse.grid(row=3, column=2)Label(op, text='新电话', anchor=E).grid(row=4, column=1, sticky=E) # 新输入提示标签newmte = StringVar()newtxtmte = Entry(op, textvariable=newmte)newtxtmte.grid(row=4, column=2)bteditsave = Button(op, text=' 修改', state=DISABLED) # 确定修改的按钮bteditsave.grid(row=1, column=3, rowspan=2, sticky=N + E + S + W)## 判断是否找到def dofind():cid = cno.get()if find(cid) == -1:showinfo('管理员管理', '%s 没有注册!' % cid)else:# 输出查找到的信息cn = connect(dbfile)cur = cn.execute('select * from Managers where M_id=?', (cid,))inf = cur.fetchall()Label(tf, text=inf, anchor=E).grid(row=2, column=2)btdel.config(state='normal') # 让删除按钮btdel处于默认状态bteditsave.config(state='normal') # 修改按钮处于默认状态## 删除def dodelete():cid = cno.get() # 获取编号if askokcancel('管理员管理', "确认删除:%s?" % cid): # 询问是否进行该操作cn = connect(dbfile) # 连接数据库cn.execute('delete from Managers where M_id=?', (cid,)) # 执行删除指令cn.commit()cn.close()showinfo('管理员管理', "成功删除:%s" % cid)## 修改def saveedit():cid = cno.get() # 获取编号newcid = newmid.get() # 新编号newname = newmna.get() # 获新姓名newtel = newmte.get()if newname == '':showerror('管理员管理', '新姓名错误:%s' % newname)newtxtmna.focus_set() # 将输入新姓名的框设为焦点。焦点是当前正在操作的控件。else:if find(newcid) == 1:if cid != newcid:showerror('管理员管理', '编号 %s 已经存在:' % newcid)newtxtmid.focus_set()else:newse = newmse.get() # 获取新密码if newse == '':showerror('管理员管理', '密码无效!')newtxtmse.focus_set()else:cn = connect(dbfile) # 连接数据库cn.execute('update Managers set M_id=?,M_name=?,M_telephone=?,M_secret=?\where M_id=?', (newcid, newname, newtel, newse, cid)) # 更新cn.commit()cn.close()showinfo('管理员管理', '修改成功!')## 触发命令按钮btok.config(command=dofind) # 查找按钮触发btdel.config(command=dodelete) # 删除按钮触发bteditsave.config(command=saveedit) # 修改按钮触发# 阅览读者def Read_Readers():global manfra # 标签的父级对象,本函数中就是该标签放置的位置(标签框架)cn = connect(dbfile) # 连接数据库cur = cn.execute('select * from Readers')Rea = cur.fetchall() # 获取所有记录cn.close() # 关闭数据库manfra.destroy() # 需要销毁控件时触发## 开始输出读者信息if len(Rea) == 0:showwarning('读者管理', '没有读者!') # 警告信息else:## 设置表格大小、位置manfra = LabelFrame(text='读者信息') # 标签框架manfra.pack(anchor='center', pady=50, ipadx=5, ipady=5) # 标签框架摆放位置# 改变表格框的尺寸columnconfigure(表格框的列,weight=缩放权重,minsuze=最小宽度)manfra.columnconfigure(1, minsize=50)manfra.columnconfigure(2, minsize=80)manfra.columnconfigure(3, minsize=100)manfra.columnconfigure(4, minsize=150)manfra.columnconfigure(5, minsize=150)# 在标签框架里创建标签,sticky是对齐方法Label(manfra, text='序号',font=('微软雅黑', 9, 'normal'), bd=1,relief=SOLID).grid(row=1, column=1, sticky=N + E + S + W)Label(manfra, text='编号',font=('微软雅黑', 9, 'normal'), bd=1,relief=SOLID).grid(row=1, column=2, sticky=N + E + S + W)Label(manfra, text='姓名',font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=1, column=3, sticky=N + E + S + W)Label(manfra, text='密码',font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=1, column=4, sticky=N + E + S + W)Label(manfra, text='电话',font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=1, column=5, sticky=N + E + S + W)rn = 2 # 记录行for x in Rea: # 循环每行cn = 1 # 记录列Label(manfra, text=str(rn - 1),font=('微软雅黑', 9, 'bold'), bd=1,relief=SOLID).grid(row=rn, column=cn, sticky=N + E + S + W)for a in x: # 循环每列cn += 1Label(manfra, text=str(a),font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=rn, column=cn, sticky=N + E + S + W)rn += 1# 管理员注册def Manager_sign():global manframanfra.destroy()manfra = LabelFrame(text='管理员注册')manfra.pack(anchor=CENTER, pady=120, ipadx=5, ipady=5)## 输入区域框架tf = Frame(manfra) # 定义放置控件的框架tf.pack()## 输入需要添加的编号Label(tf, text='编号', anchor=E).grid(row=1, column=1)M_id = StringVar()txtcno = Entry(tf, textvariable=M_id)txtcno.grid(row=1, column=2)## 输入需要添加的姓名Label(tf, text='姓名', anchor=E).grid(row=2, column=1)M_name = StringVar()txtcna = Entry(tf, textvariable=M_name)txtcna.grid(row=2, column=2)## 输入需要添加的密码Label(tf, text='密码', anchor=E).grid(row=3, column=1)M_secret = StringVar()txtcnm = Entry(tf, textvariable=M_secret)txtcnm.grid(row=3, column=2)## 输入需要添加的电话Label(tf, text='电话', anchor=E).grid(row=4, column=1)M_telephone = StringVar()txtcno = Entry(tf, textvariable=M_telephone)txtcno.grid(row=4, column=2)## 管理员注册密匙Label(tf, text='注册密匙', anchor=E).grid(row=5, column=1)MM = StringVar()txtcno = Entry(tf, textvariable=MM)txtcno.grid(row=5, column=2)# 添加清空按钮区域框架tf2 = Frame(manfra)tf2.pack()btok = Button(tf2, text='确定') # 确定按钮btok.grid(row=1, column=1)btclear = Button(tf2, text='清空') # 清空按钮btclear.grid(row=1, column=2)# 清空已经输入的def clearall():M_id.set('')M_name.set('')M_gender.set('')M_telephone.set('')MM.set('')# 管理员注册def savenew():cid = M_id.get()cna = M_name.get()cse =M_secret.get()ctel = M_telephone.get()cmm=MM.get()if cmm=='123':if cid == '':showerror('管理员注册', '编号无效!')else:if find(cid) == 1:showerror('管理员注册', '编号已经存在!')txtcno.focus() # 光标集中到户名输入框else:if cna == '':showerror('管理员注册', '名字输入无效!')txtcna.focus()else:if cse=='':showerror('管理员注册','密码无效!')else:cn = connect(dbfile)cn.execute('insert into Managers(M_id,M_name,M_secret,M_telephone) values(?,?,?,?)',(cid, cna, cse, ctel))cn.commit()cn.close()showinfo('管理员注册', '注册成功!')global manframanfra.destroy()Manager_menu()else:showerror('管理员注册','密匙错误')# 按钮命令触发btclear.config(command=clearall) # 清空输入框按钮触发btok.config(command=savenew) # 增加读者按钮触发# 登录def Manage_log():global manframanfra.destroy()manfra = LabelFrame(text='管理员登录')manfra.pack(anchor=CENTER, pady=120, ipadx=5, ipady=5)## 输入区域框架tf = Frame(manfra) # 定义放置控件的框架tf.pack()## 输入编号Label(tf, text='编号', anchor=E).grid(row=1, column=1)M_id = StringVar()txtcno = Entry(tf, textvariable=M_id)txtcno.grid(row=1, column=2)## 输入密码Label(tf, text='密码', anchor=E).grid(row=2, column=1)M_secret = StringVar()txtcns = Entry(tf, textvariable=M_secret)txtcns.grid(row=2, column=2)# 添加按钮区域框架tf2 = Frame(manfra)tf2.pack()btok = Button(tf2, text='确定') # 确定按钮btok.grid(row=1, column=1)btclear = Button(tf2, text='清空') # 清空按钮btclear.grid(row=1, column=2)# 确定登录后def sign_in():cid = M_id.get()cse = M_secret.get()if cid == '':showerror('管理员登录', '编号无效!')txtcno.focus()else:if find(cid) < 0:showerror('管理员登录', '编号不存在,请重新输入!')txtcno.focus()else:if cse == '':showerror('管理登录', '密码无效!')txtcna.focus()else:if match(cid, cse) < 0:showerror('管理员登录', '密码错误!')else:showinfo('管理员登录', '登录成功!')global manframanfra.destroy()manfra = LabelFrame(text='亲爱的管理员')manfra.pack(anchor=CENTER, pady=150, ipadx=10, ipady=10)Label(manfra, text='欢迎来到图书馆~', anchor=E).grid(row=1, column=1)Manager_menu()# 清空已经输入的def clearall():M_id.set('')M_seccret.set('')# 按钮命令触发btclear.config(command=clearall) # 清空输入框按钮触发btok.config(command=sign_in) # 增加读者按钮触发# 判断书是否已经存在def find_B(key_id):cn = connect(dbfile)cur = cn.execute('select * from Books where B_id=?', (key_id,))user = cur.fetchall()if len(user) > 0:n = 1else:n = -1cn.close()return n# 新增图书def Add_Book():## 主标签框架global manframanfra.destroy()manfra = LabelFrame(text='新增图书')manfra.pack(anchor=CENTER, pady=120, ipadx=5, ipady=5)## 输入区域框架tf = Frame(manfra) # 定义放置控件的框架tf.pack()## 输入需要添加Label(tf, text='编号', anchor=E).grid(row=1, column=1)cno = StringVar()txtcno = Entry(tf, textvariable=cno)txtcno.grid(row=1, column=2)## 输入需要添加Label(tf, text='书名', anchor=E).grid(row=2, column=1, sticky=E)cname = StringVar()txtcna = Entry(tf, textvariable=cname)txtcna.grid(row=2, column=2)## 输入需要添加Label(tf, text='作者', anchor=E).grid(row=3, column=1, sticky=E)cwrite = StringVar()txtcwr = Entry(tf, textvariable=cwrite)txtcwr.grid(row=3, column=2)## 输入需要添加Label(tf, text='出版社', anchor=E).grid(row=4, column=1, sticky=E)cpub = StringVar()txtcpub = Entry(tf, textvariable=cpub)txtcpub.grid(row=4, column=2)## 输入需要添加Label(tf, text='类别', anchor=E).grid(row=5, column=1, sticky=E)ccla = StringVar()txtccla = Entry(tf, textvariable=ccla)txtccla.grid(row=5, column=2)# 添加清空按钮区域框架tf2 = Frame(manfra)tf2.pack()btok = Button(tf2, text='添加') # 添加按钮btok.grid(row=1, column=1)btclear = Button(tf2, text='清空') # 清空按钮btclear.grid(row=1, column=2)# 清空已经输入的def clearall():cno.set('')cname.set('')cwrite.set('')cpub.set('')ccla.set('')# 添加def savenew():cid = cno.get()cna = cname.get()cwr=cwrite.get()cpu=cpub.get()ccl=ccla.get()if cid == '':showerror('图书管理', '编号无效!')else:if find_B(cid) == 1:showerror('图书管理', '编号已经存在!')txtcno.focus() # 光标集中到户名输入框else:if cna == '':showerror('图书管理', '书名输入无效!')txtcna.focus()else:cn = connect(dbfile)t=1cn.execute('insert into Books(B_id,B_name,\B_writer,B_publishing,B_classify,B_state) \values(?,?,?,?,?,?)', (cid, cna,cwr,cpu,ccl,t))cn.commit()cn.close()showinfo('图书管理', '已成功添加新书!')# 按钮命令触发btclear.config(command=clearall) # 清空输入框按钮触发btok.config(command=savenew) # 增加客户按钮触发# 阅览图书# 阅览图书def Read_Books():global manfra # 标签的父级对象,本函数中就是该标签放置的位置(标签框架)cn = connect(dbfile) # 连接数据库cur = cn.execute('select * from Books')Bo = cur.fetchall() # 获取所有记录cn.close() # 关闭数据库manfra.destroy() # 需要销毁控件时触发## 开始输出读者信息if len(Bo) == 0:showwarning('书籍管理', '没有书籍!') # 警告信息else:## 设置表格大小、位置manfra = LabelFrame(text='图书信息') # 标签框架manfra.pack(anchor='center', pady=50, ipadx=5, ipady=5) # 标签框架摆放位置# 改变表格框的尺寸columnconfigure(表格框的列,weight=缩放权重,minsuze=最小宽度)manfra.columnconfigure(1, minsize=50)manfra.columnconfigure(2, minsize=70)manfra.columnconfigure(3, minsize=100)manfra.columnconfigure(4, minsize=80)manfra.columnconfigure(5, minsize=100)manfra.columnconfigure(6, minsize=80)manfra.columnconfigure(7, minsize=50)# 在标签框架里创建标签,sticky是对齐方法Label(manfra, text='序号',font=('微软雅黑', 9, 'normal'), bd=1,relief=SOLID).grid(row=1, column=1, sticky=N + E + S + W)Label(manfra, text='编号',font=('微软雅黑', 9, 'normal'), bd=1,relief=SOLID).grid(row=1, column=2, sticky=N + E + S + W)Label(manfra, text='书名',font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=1, column=3, sticky=N + E + S + W)Label(manfra, text='作者',font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=1, column=4, sticky=N + E + S + W)Label(manfra, text='出版社',font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=1, column=5, sticky=N + E + S + W)Label(manfra, text='类别',font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=1, column=6, sticky=N + E + S + W)Label(manfra, text='状态',font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=1, column=7, sticky=N + E + S + W)rn = 2 # 记录行for x in Bo: # 循环每行cn = 1 # 记录列Label(manfra, text=str(rn - 1),font=('微软雅黑', 9, 'bold'), bd=1,relief=SOLID).grid(row=rn, column=cn, sticky=N + E + S + W)for a in x: # 循环每列cn += 1Label(manfra, text=str(a),font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=rn, column=cn, sticky=N + E + S + W)rn += 1# 编辑图书def Edit_Book():## 设置标签框架manfraglobal manframanfra.destroy()manfra = LabelFrame(text='编辑图书')manfra.pack(anchor='center', pady=50, ipadx=5, ipady=5)## 把标签框架manfra细分,即在manfra中再设置标签框架tf = LabelFrame(manfra, text='查找图书')tf.pack(anchor='center', pady=10, ipadx=5, ipady=5)Label(tf, text='输入编号', anchor=E).grid(row=1, column=1)cno = StringVar() # 能自动刷新的字符串变量,能获取文本框中输入的值txtcno = Entry(tf, textvariable=cno) # 定义单行输入框txtcno.grid(row=1, column=2) # 设置单行输入框大小btok = Button(tf, text='查找') # 定义按钮btok.grid(row=1, column=3) # 设置按钮大小## 编辑总标签框架editframe = LabelFrame(manfra, text='编辑图书')editframe.pack(anchor='center', pady=20, ipadx=5, ipady=5)## 删除按钮btdel = Button(editframe, text='删除图书', state=DISABLED)btdel.pack(anchor=NW)## 修改标签框架op = LabelFrame(editframe, text='修改图书')op.pack(anchor='center', pady=10, ipadx=5, ipady=5)## 输入修改的新信息Label(op, text='新编号', anchor=E).grid(row=1, column=1) # 新输入提示标签newid = StringVar() # 定义可以储存输入信息的变量newtxtid = Entry(op, textvariable=newid) # 获取输入框的输入信息newtxtid.grid(row=1, column=2)Label(op, text='新书名', anchor=E).grid(row=2, column=1, sticky=E) # 新输入提示标签newna = StringVar()newtxtna = Entry(op, textvariable=newna)newtxtna.grid(row=2, column=2)Label(op, text='新作者', anchor=E).grid(row=3, column=1, sticky=E) # 新输入提示标签newwr = StringVar()newtxtwr = Entry(op, textvariable=newwr)newtxtwr.grid(row=3, column=2)Label(op, text='新出版社', anchor=E).grid(row=4, column=1, sticky=E) # 新输入提示标签newpu = StringVar()newtxtpu = Entry(op, textvariable=newpu)newtxtpu.grid(row=4, column=2)Label(op, text='新类别', anchor=E).grid(row=5, column=1, sticky=E) # 新输入提示标签newcl = StringVar()newtxtcl = Entry(op, textvariable=newcl)newtxtcl.grid(row=5, column=2)Label(op, text='新状态', anchor=E).grid(row=6, column=1, sticky=E) # 新输入提示标签newst = StringVar()newtxtst = Entry(op, textvariable=newst)newtxtst.grid(row=6, column=2)bteditsave = Button(op, text=' 修改', state=DISABLED) # 确定修改的按钮bteditsave.grid(row=1, column=3, rowspan=2, sticky=N + E + S + W)## 判断是否找到def dofind():cid = cno.get()if find_B(cid) == -1:showinfo('图书管理', '%s 没有注册!' % cid)else:# 输出查找到的信息cn = connect(dbfile)cur = cn.execute('select * from Books where B_id=?', (cid,))inf = cur.fetchall()Label(tf, text=inf, anchor=E).grid(row=2, column=2)cur = cn.execute('select B_id from Books where B_id=?', (cid,))inf = cur.fetchall()newtxtid.insert(0,inf)cur = cn.execute('select B_name from Books where B_id=?', (cid,))inf = cur.fetchall()newtxtna.insert(0, inf)cur = cn.execute('select B_writer from Books where B_id=?', (cid,))inf = cur.fetchall()newtxtwr.insert(0, inf)cur = cn.execute('select B_publishing from Books where B_id=?', (cid,))inf = cur.fetchall()newtxtpu.insert(0, inf)cur = cn.execute('select B_classify from Books where B_id=?', (cid,))inf = cur.fetchall()newtxtcl.insert(0, inf)cur = cn.execute('select B_state from Books where B_id=?', (cid,))inf = cur.fetchall()newtxtst.insert(0, inf)cn.commit()cn.close()btdel.config(state='normal') # 让删除按钮btdel处于默认状态bteditsave.config(state='normal') # 修改按钮处于默认状态## 删除def dodelete():cid = cno.get() # 获取编号if askokcancel('图书管理', "确认删除:%s?" % cid): # 询问是否进行该操作cn = connect(dbfile) # 连接数据库cn.execute('delete from Books where B_id=?', (cid,)) # 执行删除指令cn.commit()cn.close()showinfo('图书管理', "成功删除:%s" % cid)## 修改def saveedit():cid = cno.get() # 获取编号newcid = newid.get() # 新编号newname = newna.get()newwriter = newwr.get()newpublish=newpu.get()newclassify=newcl.get()newstate=newst.get()if newname == '':showerror('图书管理', '新书名错误:%s' % newname)newtxtna.focus_set() # 将输入新姓名的框设为焦点。焦点是当前正在操作的控件。else:if find_B(newcid) == 1:if cid != newcid:showerror('图书管理', '编号 %s 已经存在:' % newcid)newtxtid.focus_set()else:if newstate !='0' and newstate!='1':showerror('图书管理', '状态无效!')newtxtst.focus_set()else:cn = connect(dbfile) # 连接数据库cn.execute('update Books set B_id=?,B_name=?,B_writer=?,B_publishing=?,B_classify=?,B_state=? \where B_id=?',(newcid, newname, newwriter, newpublish, newclassify,newstate,cid)) # 更新cn.commit()cn.close()showinfo('图书管理', '修改成功!')## 触发命令按钮btok.config(command=dofind) # 查找按钮触发btdel.config(command=dodelete) # 删除按钮触发bteditsave.config(command=saveedit) # 修改按钮触发# 查找书def Find_Book():global manframanfra.destroy()manfra = LabelFrame(text='查找图书')manfra.pack(anchor='center', pady=50, ipadx=5, ipady=5)## 把标签框架manfra细分,即在manfra中再设置标签框架tf = LabelFrame(manfra)tf.pack(anchor='center', pady=10, ipadx=5, ipady=5)Label(tf, text='输入书名', anchor=E).grid(row=1, column=1)cno = StringVar() # 能自动刷新的字符串变量,能获取文本框中输入的值txtcno = Entry(tf, textvariable=cno) # 定义单行输入框txtcno.grid(row=1, column=2) # 设置单行输入框大小def dofind():bookname = cno.get()if bookname=='':showerror('查找图书','书名无效!')else:cn = connect(dbfile)cur = cn.execute('select * from Books where B_name=?', (bookname,))inf = cur.fetchall()if len(inf)<=0:showerror('查找图书','查无此书!')else:global manframanfra.destroy()## 设置表格大小、位置manfra = LabelFrame(text='查询结果') # 标签框架manfra.pack(anchor='center', pady=20, ipadx=5, ipady=5) # 标签框架摆放位置# 改变表格框的尺寸columnconfigure(表格框的列,weight=缩放权重,minsuze=最小宽度)manfra.columnconfigure(1, minsize=50)manfra.columnconfigure(2, minsize=70)manfra.columnconfigure(3, minsize=100)manfra.columnconfigure(4, minsize=80)manfra.columnconfigure(5, minsize=100)manfra.columnconfigure(6, minsize=80)manfra.columnconfigure(7, minsize=50)# 在标签框架里创建标签,sticky是对齐方法Label(manfra, text='序号',font=('微软雅黑', 9, 'normal'), bd=1,relief=SOLID).grid(row=1, column=1, sticky=N + E + S + W)Label(manfra, text='编号',font=('微软雅黑', 9, 'normal'), bd=1,relief=SOLID).grid(row=1, column=2, sticky=N + E + S + W)Label(manfra, text='书名',font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=1, column=3, sticky=N + E + S + W)Label(manfra, text='作者',font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=1, column=4, sticky=N + E + S + W)Label(manfra, text='出版社',font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=1, column=5, sticky=N + E + S + W)Label(manfra, text='类别',font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=1, column=6, sticky=N + E + S + W)Label(manfra, text='状态',font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=1, column=7, sticky=N + E + S + W)rn = 2 # 记录行for x in inf: # 循环每行cn = 1 # 记录列Label(manfra, text=str(rn - 1),font=('微软雅黑', 9, 'bold'), bd=1,relief=SOLID).grid(row=rn, column=cn, sticky=N + E + S + W)for a in x: # 循环每列cn += 1Label(manfra, text=str(a),font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=rn, column=cn, sticky=N + E + S + W)rn += 1btok = Button(tf, text='查找') # 定义按钮btok.grid(row=1, column=3) # 设置按钮位置## 触发命令按钮btok.config(command=dofind) # 查找按钮触发# 阅览欠款信息def Read_Fine():global manfra # 标签的父级对象,本函数中就是该标签放置的位置(标签框架)cn = connect(dbfile) # 连接数据库cur = cn.execute('select * from Fine')Rea = cur.fetchall() # 获取所有记录cn.close() # 关闭数据库manfra.destroy() # 需要销毁控件时触发## 开始输出读者信息if len(Rea) == 0:showwarning('欠款管理', '暂无欠款信息!') # 警告信息else:## 设置表格大小、位置manfra = LabelFrame(text='欠款信息') # 标签框架manfra.pack(anchor='center', pady=50, ipadx=5, ipady=5) # 标签框架摆放位置# 改变表格框的尺寸columnconfigure(表格框的列,weight=缩放权重,minsuze=最小宽度)manfra.columnconfigure(1, minsize=30)manfra.columnconfigure(2, minsize=50)manfra.columnconfigure(3, minsize=100)manfra.columnconfigure(4, minsize=100)manfra.columnconfigure(5, minsize=100)# 在标签框架里创建标签,sticky是对齐方法Label(manfra, text='序号',font=('微软雅黑', 9, 'normal'), bd=1,relief=SOLID).grid(row=1, column=1, sticky=N + E + S + W)Label(manfra, text='读者编号',font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=1, column=2, sticky=N + E + S + W)Label(manfra, text='总罚款',font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=1, column=3, sticky=N + E + S + W)Label(manfra, text='已交罚款',font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=1, column=4, sticky=N + E + S + W)Label(manfra, text='未交罚款',font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=1, column=5, sticky=N + E + S + W)rn = 2 # 记录行for x in Rea: # 循环每行cn = 1 # 记录列Label(manfra, text=str(rn - 1),font=('微软雅黑', 9, 'bold'), bd=1,relief=SOLID).grid(row=rn, column=cn, sticky=N + E + S + W)for a in x: # 循环每列cn += 1Label(manfra, text=str(a),font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=rn, column=cn, sticky=N + E + S + W)rn += 1# 阅览借阅信息def Read_Borrow():global manfra # 标签的父级对象,本函数中就是该标签放置的位置(标签框架)cn = connect(dbfile) # 连接数据库cur = cn.execute('select * from Borrow')Rea = cur.fetchall() # 获取所有记录cn.close() # 关闭数据库manfra.destroy() # 需要销毁控件时触发## 开始输出读者信息if len(Rea) == 0:showwarning('借阅管理', '暂无借阅信息!') # 警告信息else:## 设置表格大小、位置manfra = LabelFrame(text='借阅信息') # 标签框架manfra.pack(anchor='center', pady=50, ipadx=5, ipady=5) # 标签框架摆放位置# 改变表格框的尺寸columnconfigure(表格框的列,weight=缩放权重,minsuze=最小宽度)manfra.columnconfigure(1, minsize=30)manfra.columnconfigure(2, minsize=50)manfra.columnconfigure(3, minsize=50)manfra.columnconfigure(4, minsize=100)manfra.columnconfigure(5, minsize=100)manfra.columnconfigure(6, minsize=100)manfra.columnconfigure(7, minsize=80)# 在标签框架里创建标签,sticky是对齐方法Label(manfra, text='序号',font=('微软雅黑', 9, 'normal'), bd=1,relief=SOLID).grid(row=1, column=1, sticky=N + E + S + W)Label(manfra, text='书籍编号',font=('微软雅黑', 9, 'normal'), bd=1,relief=SOLID).grid(row=1, column=2, sticky=N + E + S + W)Label(manfra, text='读者编号',font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=1, column=3, sticky=N + E + S + W)Label(manfra, text='借阅日期',font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=1, column=4, sticky=N + E + S + W)Label(manfra, text='应还日期',font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=1, column=5, sticky=N + E + S + W)Label(manfra, text='实际归还日期',font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=1, column=6, sticky=N + E + S + W)Label(manfra, text='罚款',font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=1, column=7, sticky=N + E + S + W)rn = 2 # 记录行for x in Rea: # 循环每行cn = 1 # 记录列Label(manfra, text=str(rn - 1),font=('微软雅黑', 9, 'bold'), bd=1,relief=SOLID).grid(row=rn, column=cn, sticky=N + E + S + W)for a in x: # 循环每列cn += 1Label(manfra, text=str(a),font=('微软雅黑', 9), bd=1,relief=SOLID).grid(row=rn, column=cn, sticky=N + E + S + W)rn += 1# 编辑欠款信息def Edit_Fine():## 设置标签框架manfraglobal manframanfra.destroy()manfra = LabelFrame(text='编辑欠款')manfra.pack(anchor='center', pady=50, ipadx=5, ipady=5)## 把标签框架manfra细分,即在manfra中再设置标签框架tf = LabelFrame(manfra, text='查找欠款信息')tf.pack(anchor='center', pady=10, ipadx=5, ipady=5)Label(tf, text='输入读者编号', anchor=E).grid(row=1, column=1)cno = StringVar() # 能自动刷新的字符串变量,能获取文本框中输入的值txtcno = Entry(tf, textvariable=cno) # 定义单行输入框txtcno.grid(row=1, column=2) # 设置单行输入框大小btok = Button(tf, text='查找') # 定义按钮btok.grid(row=1, column=3) # 设置按钮大小## 编辑总标签框架editframe = LabelFrame(manfra, text='编辑欠款')editframe.pack(anchor='center', pady=20, ipadx=5, ipady=5)## 删除按钮btdel = Button(editframe, text='删除欠款', state=DISABLED)btdel.pack(anchor=NW)## 修改标签框架op = LabelFrame(editframe, text='更新欠款')op.pack(anchor='center', pady=10, ipadx=5, ipady=5)## 输入修改的新信息Label(op, text='总罚金', anchor=E).grid(row=1, column=1) # 新输入提示标签newall = StringVar() # 定义可以储存输入信息的变量newtxtall = Entry(op, textvariable=newall) # 获取输入框的输入信息newtxtall.grid(row=1, column=2)Label(op, text='已交罚金', anchor=E).grid(row=2, column=1, sticky=E) # 新输入提示标签newyes = StringVar()newtxtyes = Entry(op, textvariable=newyes)newtxtyes.grid(row=2, column=2)Label(op, text='未交罚金', anchor=E).grid(row=3, column=1, sticky=E) # 新输入提示标签newno = StringVar()newtxtno = Entry(op, textvariable=newno)newtxtno.grid(row=3, column=2)bteditsave = Button(op, text='修改', state=DISABLED) # 确定修改的按钮bteditsave.grid(row=1, column=3, rowspan=2, sticky=N + E + S + W)def find_F(key):cn = connect(dbfile)cur = cn.execute('select * from Fine where R_id=?', (key,))user = cur.fetchall()if len(user) > 0:n = 1else:n = -1cn.close()return n## 判断是否找到def dofind():cid = cno.get()if find_F(cid) == -1:showinfo('欠款管理', '%s 没有该条欠款!' % cid)else:# 输出查找到的信息cn = connect(dbfile)cur = cn.execute('select * from Fine where R_id=?', (cid,))inf = cur.fetchall()Label(tf, text=inf, anchor=E).grid(row=2, column=2)cur = cn.execute('select Fine_all from Fine where R_id=?', (cid,))inf = cur.fetchall()newtxtall.insert(0, inf)cur = cn.execute('select Fine_yes from Fine where R_id=?', (cid,))inf = cur.fetchall()newtxtyes.insert(0, inf)cur = cn.execute('select Fine_no from Fine where R_id=?', (cid,))inf = cur.fetchall()newtxtno.insert(0, inf)cn.commit()cn.close()btdel.config(state='normal') # 让删除按钮btdel处于默认状态bteditsave.config(state='normal') # 修改按钮处于默认状态## 删除def dodelete():cid = cno.get() # 获取编号if askokcancel('欠款管理', "确认删除:%s?" % cid): # 询问是否进行该操作cn = connect(dbfile) # 连接数据库cn.execute('delete from Fine where R_id=?', (cid,)) # 执行删除指令cn.commit()cn.close()showinfo('欠款管理', "成功删除:%s" % cid)## 修改def saveedit():cid = cno.get() # 获取编号newal = newall.get()newye = newyes.get()newnoo = newno.get()if newal == '' or newye=='' or newnoo=='':showerror('欠款管理', '罚金错误!')else:cn = connect(dbfile) # 连接数据库cn.execute('update Fine set R_id=?,Fine_all=?,Fine_yes=?,Fine_no=? \where R_id=?',(cid, newal, newye, newnoo, cid)) # 更新cn.commit()cn.close()showinfo('欠款管理', '更新成功!')## 触发命令按钮btok.config(command=dofind) # 查找按钮触发btdel.config(command=dodelete) # 删除按钮触发bteditsave.config(command=saveedit) # 修改按钮触发# 管理员登录后的菜单def Manager_menu():menubar = Menu(win)win.config(menu=menubar)file_M = Menu(menubar, tearoff=0)file_M.add_command(label='阅览管理员', command=Read_Managers)file_M.add_command(label='编辑管理员', command=Edit_Managers)menubar.add_cascade(label='管理员管理', menu=file_M)file_R = Menu(menubar, tearoff=0)file_R.add_command(label='阅览读者', command=Read_Readers)file_R.add_command(label='编辑和查找读者', command=Edit_Readers)menubar.add_cascade(label='读者管理', menu=file_R)file_B = Menu(menubar, tearoff=0)file_B.add_command(label='阅览图书', command=Read_Books)file_B.add_command(label='新增图书', command=Add_Book)file_B.add_command(label='编辑图书',command=Edit_Book)file_B.add_command(label='查找图书',command=Find_Book)menubar.add_cascade(label='图书管理', menu=file_B)file_F = Menu(menubar, tearoff=0)file_F.add_command(label='借阅信息',command=Read_Borrow)file_F.add_separator()file_F.add_command(label='阅览欠款信息',command=Read_Fine)file_F.add_command(label='编辑欠款信息',command=Edit_Fine)menubar.add_cascade(label='借阅管理', menu=file_F)file = Menu(menubar, tearoff=0)file.add_command(label='退出', command=identity)menubar.add_cascade(label='主界面', menu=file)# 管理员登录注册主界面global manframanfra.destroy()manfra = LabelFrame(text='管理员')manfra.pack(anchor='center', pady=150, ipadx=5, ipady=5)button_log = Button(manfra, text='登录', height=3, width=20)button_log.pack()button_sign = Button(manfra, text='注册', height=3, width=20)button_sign.pack()# 菜单menubar = Menu(win)win.config(menu=menubar)file_back = Menu(menubar, tearoff=0)file_back.add_command(label='返回', command=identity)menubar.add_cascade(label='主页面', menu=file_back)file_signlog = Menu(menubar, tearoff=0)file_signlog.add_command(label='管理员登录', command=Manage_log)file_signlog.add_command(label='管理员注册', command=Manager_sign)menubar.add_cascade(label='管理员', menu=file_signlog)# Reader按钮触发button_sign.config(command=Manager_sign)button_log.config(command=Manage_log)def identity():menubar = Menu(win)win.config(menu=menubar)file_back = Menu(menubar, tearoff=0)# file_back.add_command(label='返回', command=identity)# menubar.add_cascade(label='主页面', menu=file_back)global manfra# 按钮选择是管理员还是读者manfra.destroy()manfra=LabelFrame(text='身份选择')manfra.pack(anchor='center', pady=150, ipadx=5, ipady=5)button_M=Button(manfra,text='管理员',height=3,width=20)button_M.pack()button_R=Button(manfra,text='读者',height=3,width=20)button_R.pack()button_R.config(command=Reader)button_M.config(command=Manager)# 主函数
back_main()
identity()
win.mainloop()
六 运行截图