0


Python实现带GUI和连接数据库的图书管理系统

文章目录


前言

作者注:这是一个稚嫩青涩的项目,由于赶时间且自身技术不成熟,所以仅实现了基础功能且只能本地化运行,因此比较低端。

大三上学期的程序设计实训大作业,挑了其中一个我认为最简单的的《图书管理系统》来写。用python写是因为py有自带的GUI,即tkinter模块,对初次接触GUI的新手会比较友好。编译器我用的是Pycharm,你需要检查你的编译器是否带了tkinter模块和pymysql模块,没有的话需要下载安装,具体方法可以百度,很简单。界面很丑,凑合看哦!如果你没有了解过tkinter,建议先去知乎,csdn上面搜索自学一下入门教程,这样就比较容易理解我的东西啦!
** 特别注意:数据库表是我后来根据记忆建的,可能跟代码中的SQL语句的顺序有出入,实际数据库的字段值属性值顺序按照代码里的为准,修改一下数据库就好啦!代码是没问题的o( ̄︶ ̄)o **
在这里插入图片描述

二、建立数据库library

** 特别注意:这个表是我后来根据记忆建的,可能跟代码中的SQL语句的顺序有出入,实际数据库的字段值属性值顺序按照代码里的为准,修改一下数据库就好啦!代码是没问题的o( ̄︶ ̄)o **
在这里插入图片描述
如图所示,软件是Navicat,给library建表。

2.1 book表

存储图书的相关信息,包括书名,作者,类型,数量。主码是name和author。
在这里插入图片描述

2.2 borrow表

借书单,存储借书人ID,书名,作者,借书时间。主码是name和author。
在这里插入图片描述

2.3 user表

使用者,包括ID,password,job是个只有1位的数字,0表示读者,1表示管理员,登录的时候通过检测其job然后选择是跳转到读者界面还是管理员界面。
在这里插入图片描述

三、各个模块介绍

在这里插入图片描述

3.1 初始界面initial

  1. import tkinter as tk
  2. import reader
  3. import manager
  4. defframe():#初始界面global root
  5. root=tk.Tk()
  6. root.geometry('900x700')
  7. root.title('西电图书管理系统')
  8. lable0=tk.Label(root,text='欢迎来到XDU图书馆',bg='pink',font=('微软雅黑',50)).pack()#上#canvas是个画布,想要插入图片的话首先要定义个canvas
  9. canvas=tk.Canvas(root,height=500,width=500)#中
  10. image_file=tk.PhotoImage(file='2.gif')#图片文件的后缀必须是.gif,且亲测不能自行鼠标右键重命名更改成.gif,要用win10里内置的画图功能,打开图片然后另存为的时候选择.gif#图片文件必须放到你的项目目录里边才有效
  11. image=canvas.create_image(250,100,image=image_file)
  12. canvas.place(x=170,y=170)
  13. lable1=tk.Label(root,text='请选择用户类型:',font=('微软雅黑',20)).place(x=80,y=500)#下
  14. tk.Button(root, text='读 者',font=('微软雅黑',15),width=10, height=2,command=exit_reader).place(x=350, y=420)
  15. tk.Button(root, text='管理员',font=('微软雅黑',15),width=10, height=2,command=exit_manager).place(x=350, y=550)
  16. root.mainloop()#必须要有这句话,你的页面才会动态刷新循环,否则页面不会显示 defexit_reader():#跳转至读者界面
  17. root.destroy()
  18. reader.frame()defexit_manager():#跳转至管理员界面
  19. root.destroy()
  20. manager.frame()if __name__ =='__main__':
  21. frame()

在这里插入图片描述
效果就是上面这样的。
这个初始界面就比较简单,点击读者跳转到读者界面,点击管理员跳转到管理员界面。

3.2 manager登录注册模块

当我们从初始界面选择“管理员”,那么这时候调用exit_manager()函数,来到了管理员界面

  1. import tkinter as tk
  2. import tkinter.messagebox as msg #这个是会弹出一个警告/提示小框import initial
  3. import pymysql
  4. import ID
  5. defframe():#管理员界面global root
  6. root= tk.Tk()
  7. root.geometry('900x700')
  8. root.title('西电图书管理系统')
  9. lable0 = tk.Label(root, text='管理员登录', bg='pink', font=('微软雅黑',50)).pack()#
  10. canvas = tk.Canvas(root, height=500, width=500)#
  11. image_file = tk.PhotoImage(file='2.gif')
  12. image = canvas.create_image(250,100, image=image_file)
  13. canvas.place(x=190, y=170)
  14. lable1 = tk.Label(root, text='请选择:', font=('微软雅黑',20)).place(x=80, y=400)#
  15. tk.Button(root, text='登录', font=('微软雅黑',15), width=10, height=2, command=login).place(x=150, y=500)
  16. tk.Button(root, text='注册', font=('微软雅黑',15), width=10, height=2, command=register).place(x=350, y=500)
  17. tk.Button(root, text='退出', font=('微软雅黑',15), width=10, height=2, command=exit_manager).place(x=550, y=500)
  18. root.mainloop()deflogin():#登录小窗口global root1
  19. root1=tk.Tk()
  20. root1.wm_attributes('-topmost',1)#将登录窗口置顶不至于被遮到下面
  21. root1.title('管理员登录')
  22. root1.geometry('500x300')
  23. lable1 = tk.Label(root1, text='账号:', font=25).place(x=100,y=50)
  24. lable2 = tk.Label(root1, text='密码:', font=25).place(x=100, y=100)global entry_name, entry_key
  25. name=tk.StringVar()
  26. key = tk.StringVar()
  27. entry_name = tk.Entry(root1, textvariable=name, font=25)
  28. entry_name.place(x=180, y=50)
  29. entry_key = tk.Entry(root1, textvariable=key, font=25,show='*')
  30. entry_key.place(x=180,y=100)# 百度:tkinter要求由按钮(或者其它的插件)触发的控制器函数不能含有参数,若要给函数传递参数,需要在函数前添加lambda
  31. button1 = tk.Button(root1, text='确定', height=2, width=10, command=lambda: ID.id_check('1'))
  32. button1.place(x=210, y=180)#当我们输入账号和密码,点击确定时候,会调用ID模块里的id_check()函数,1是参数,表示其身份是管理员defregister():#注册小窗口global root2
  33. root2 = tk.Tk()
  34. root2.wm_attributes('-topmost',1)
  35. root2.title('管理员注册')
  36. root2.geometry('500x300')
  37. lable1 = tk.Label(root2, text='账号:', font=25).place(x=100, y=50)
  38. lable2 = tk.Label(root2, text='密码:', font=25).place(x=100, y=100)
  39. lable2 = tk.Label(root2, text='确认密码:', font=25).place(x=80, y=150)global entry_name, entry_key, entry_confirm
  40. name = tk.StringVar()
  41. key = tk.StringVar()
  42. confirm = tk.StringVar()
  43. entry_name = tk.Entry(root2, textvariable=name, font=25)
  44. entry_name.place(x=180, y=50)
  45. entry_key = tk.Entry(root2, textvariable=key, font=25, show='*')
  46. entry_key.place(x=180, y=100)
  47. entry_confirm = tk.Entry(root2, textvariable=confirm,font=25, show='*')
  48. entry_confirm.place(x=180, y=150)# 百度:tkinter要求由按钮(或者其它的插件)触发的控制器函数不能含有参数,若要给函数传递参数,需要在函数前添加lambda
  49. button1 = tk.Button(root2, text='确定', height=2, width=10, command=lambda: ID.id_write('1'))
  50. button1.place(x=210, y=200)#当我们点击确定的时候,会调用ID模块里的id_write()函数,1是参数,表示其身份是管理员defexit_manager():#退出管理员界面,跳转至初始界面
  51. root.destroy()
  52. initial.frame()

在这里插入图片描述
在这里插入图片描述

3.3 ID模块

ID模块相当于后端了,主要实现连接数据库,检查账号的有无,核对密码,注册等功能。

  1. import tkinter as tk
  2. import tkinter.messagebox as msg
  3. import pymysql
  4. import initial
  5. import manager
  6. import reader
  7. import m_operation
  8. import r_operation
  9. defid_check(a):#检查账号globalidif a =='1':#在管理员界面下登录,参数是1#把账号/密码框框里输入的字符串赋值给id/passwordid= manager.entry_name.get()
  10. password = manager.entry_key.get()else:#在读者界面下登录,参数是0id= reader.entry_name.get()
  11. password = reader.entry_key.get()
  12. getid()#最后得到id#连接数据库,root是你数据库的用户名,应该是默认的是root,qwer是你数据库的密码,library是你要连接的数据库名字
  13. db = pymysql.connect("localhost","root","qwer","library")#建立游标cursor,这个游标可以类比指针,这样理解比较直观
  14. cursor = db.cursor()
  15. sql ="SELECT password FROM user WHERE id='%s' AND job='%s'"%(id,a)
  16. cursor.execute(sql)#sql语句被执行
  17. result = cursor.fetchone()#得到的结果返回给result数组if result:#如果查询到了账号存在if password == result[0]:#result[0]是数组中的第一个结果
  18. success_login(a)#密码对上了,进入对应的读者/管理员操作界面else:#有账号但密码没对上
  19. msg._show(title='错误!',message='账号或密码输入错误!')else:#没有账号
  20. msg._show(title='错误!',message='您输入的用户不存在!请先注册!')if a=='1':
  21. manager.root1.destroy()#关闭登录小窗口,回到管理员界面elif a=='0':
  22. reader.root1.destroy()
  23. db.close()#查询完一定要关闭数据库啊defsuccess_login(a):#成功登录if a =='1':
  24. manager.root1.destroy()
  25. m_operation.frame()#销毁登录注册界面,跳转到管理员的操作界面elif a =='0':
  26. reader.root1.destroy()
  27. r_operation.frame()#销毁登录注册界面,跳转到读者的操作界面defid_write(a):#写入(注册)账号
  28. db = pymysql.connect("localhost","root","qwer","library")
  29. cursor = db.cursor()if a=='1':#跟check函数里边十分类似id= manager.entry_name.get()#得到输入的账号
  30. password = manager.entry_key.get()#得到输入的密码
  31. confirm = manager.entry_confirm.get()#得到输入的确认密码elif a=='0':id= reader.entry_name.get()
  32. password = reader.entry_key.get()
  33. confirm = reader.entry_confirm.get()
  34. sql0 ="SELECT id FROM user WHERE id='%s' AND job='%s'"%(id,a)
  35. sql1 ="INSERT INTO user VALUES(%s,%s,%s) "%(id, password, a)#首先检查两次输入的密码是否一致,一致后再检查注册的账号是否已经存在if password == confirm:
  36. cursor.execute(sql0)
  37. result = cursor.fetchone()if result:
  38. msg.showerror(title='错误!', message='该账号已被注册,请重新输入!')else:
  39. cursor.execute(sql1)
  40. db.commit()
  41. db.close()
  42. msg.showinfo(title='成功!', message='注册成功,请登录!')else:
  43. msg.showerror(title='错误!', message='两次密码不一致,请重新输入!')defgetid():returnid

3.4 reader登录注册模块

同2.2,如法炮(pao二声)制。
(咳咳学好语文也好重要呀!)

  1. import tkinter as tk
  2. import initial
  3. import ID
  4. defframe():global root
  5. root= tk.Tk()
  6. root.geometry('900x700')
  7. root.title('西电图书管理系统')
  8. lable0 = tk.Label(root, text='读者登录', bg='pink', font=('微软雅黑',50)).pack()#
  9. canvas = tk.Canvas(root, height=500, width=500)#
  10. image_file = tk.PhotoImage(file='2.gif')
  11. image = canvas.create_image(250,100, image=image_file)
  12. canvas.place(x=190, y=170)
  13. lable1 = tk.Label(root, text='请选择:', font=('微软雅黑',20)).place(x=80, y=400)#
  14. tk.Button(root, text='登录', font=('微软雅黑',15), width=10, height=2, command=login).place(x=150, y=500)
  15. tk.Button(root, text='注册', font=('微软雅黑',15), width=10, height=2, command=register).place(x=350, y=500)
  16. tk.Button(root, text='退出', font=('微软雅黑',15), width=10, height=2, command=exit_reader).place(x=550, y=500)
  17. root.mainloop()deflogin():global root1
  18. root1=tk.Tk()
  19. root1.wm_attributes('-topmost',1)
  20. root1.title('读者登录')
  21. root1.geometry('500x300')
  22. lable1 = tk.Label(root1, text='账号:', font=25).place(x=100, y=50)
  23. lable2 = tk.Label(root1, text='密码:', font=25).place(x=100, y=100)global entry_name, entry_key
  24. name=tk.StringVar()
  25. key = tk.StringVar()
  26. entry_name=tk.Entry(root1,textvariable=name,font=25)
  27. entry_name.place(x=180,y=50)
  28. entry_key=tk.Entry(root1, textvariable=key, font=25,show='*')
  29. entry_key.place(x=180,y=100)# 百度:tkinter要求由按钮(或者其它的插件)触发的控制器函数不能含有参数,若要给函数传递参数,需要在函数前添加lambda
  30. button1=tk.Button(root1,text='确定',height=2,width=10,command=lambda:ID.id_check('0'))
  31. button1.place(x=210,y=180)defregister():global root2
  32. root2 = tk.Tk()
  33. root2.wm_attributes('-topmost',1)
  34. root2.title('读者注册')
  35. root2.geometry('500x300')
  36. lable1 = tk.Label(root2, text='账号:', font=25).place(x=100, y=50)
  37. lable2 = tk.Label(root2, text='密码:', font=25).place(x=100, y=100)
  38. lable2 = tk.Label(root2, text='确认密码:', font=25).place(x=80, y=150)global entry_name, entry_key, entry_confirm
  39. name = tk.StringVar()
  40. key = tk.StringVar()
  41. confirm = tk.StringVar()
  42. entry_name = tk.Entry(root2, textvariable=name, font=25)
  43. entry_name.place(x=180, y=50)
  44. entry_key = tk.Entry(root2, textvariable=key, font=25, show='*')
  45. entry_key.place(x=180, y=100)
  46. entry_confirm = tk.Entry(root2, textvariable=confirm,font=25, show='*')
  47. entry_confirm.place(x=180, y=150)# 百度:tkinter要求由按钮(或者其它的插件)触发的控制器函数不能含有参数,若要给函数传递参数,需要在函数前添加lambda
  48. button1 = tk.Button(root2, text='确定', height=2, width=10, command=lambda: ID.id_write('0'))
  49. button1.place(x=210, y=200)defexit_reader():#退出管理员界面,跳转至初始界面
  50. root.destroy()
  51. initial.frame()

3.5 m_operation管理员操作界面

当我们终于成功注册/登录之后,来到了管理员的操作界面。
在这里插入图片描述

  1. import tkinter as tk
  2. import tkinter.messagebox as msg
  3. import search
  4. from tkinter import ttk
  5. import pymysql
  6. defframe():
  7. window=tk.Tk()
  8. window.title('管理员')
  9. window.geometry('900x700')
  10. lable0 = tk.Label(window, text='欢迎来到XDU图书馆', bg='pink', font=('微软雅黑',50)).pack()#
  11. lable1 = tk.Label(window, text='请选择操作:', font=('微软雅黑',20)).place(x=80, y=400)#
  12. tk.Button(window, text='进购图书', font=('微软雅黑',15), width=10, height=2,command=purchase).place(x=350, y=250)
  13. tk.Button(window, text='注销图书', font=('微软雅黑',15), width=10, height=2,command=cancel).place(x=350, y=350)
  14. tk.Button(window, text='信息查询', font=('微软雅黑',15), width=10, height=2,command=search.frame).place(x=350, y=450)
  15. window.mainloop()defpurchase():#进购图书global win
  16. win = tk.Tk()
  17. win.title('管理员')
  18. win.geometry('900x300')
  19. win.wm_attributes('-topmost',1)
  20. lable1 = tk.Label(win, text='请填写进购图书的信息:', font=('微软雅黑',20)).place(x=30, y=100)
  21. tk.Label(win, text='图书类目:', font=('宋体',12)).place(x=30, y=200)globallist#这个是一个下拉页表项,只能从下面的list['values']里边选
  22. comvalue = tk.StringVar()list= ttk.Combobox(win, textvariable=comvalue, height=10, width=10)list.place(x=100, y=200)list['values']=('全部','人文','艺术','计算机','科技','杂志')list.current(0)#默认显示'全部'global b_name
  23. tk.Label(win, text='书名:', font=('宋体',12)).place(x=200, y=200)
  24. b_name = tk.Entry(win, font=('宋体',12), width=10)
  25. b_name.place(x=250, y=200)global author
  26. tk.Label(win, text='作者:', font=('宋体',12)).place(x=350, y=200)
  27. author = tk.Entry(win, font=('宋体',12), width=10)
  28. author.place(x=400, y=200)global price
  29. tk.Label(win, text='价格:', font=('宋体',12)).place(x=460, y=200)
  30. price = tk.Entry(win, font=('宋体',12), width=10)
  31. price.place(x=510, y=200)global amount
  32. tk.Label(win, text='数量:', font=('宋体',12)).place(x=560, y=200)
  33. amount = tk.Entry(win, font=('宋体',12), width=5)
  34. amount.place(x=610, y=200)
  35. tk.Button(win, text='确认添加', font=('宋体',12), width=10, command=add).place(x=700, y=195)defadd():#添加图书信息到数据库中
  36. sql="INSERT INTO book VALUES('%s','%s','%s','%s','%s')"%(list.get(),b_name.get(),author.get(),price.get(),amount.get())
  37. db = pymysql.connect("localhost","root","qwer","library")
  38. cursor = db.cursor()
  39. cursor.execute(sql)
  40. db.commit()#这句不可或缺,当我们修改数据完成后必须要确认才能真正作用到数据库里
  41. db.close()
  42. msg.showinfo(title='成功!', message='新书已入库!')defcancel():#撤销图书global win
  43. win = tk.Tk()
  44. win.title('管理员')
  45. win.geometry('900x300')
  46. win.wm_attributes('-topmost',1)
  47. lable1 = tk.Label(win, text='请填写注销图书的信息:', font=('微软雅黑',20)).place(x=30, y=100)
  48. tk.Label(win, text='图书类目:', font=('宋体',12)).place(x=30, y=200)globallist
  49. comvalue = tk.StringVar()list= ttk.Combobox(win, textvariable=comvalue, height=10, width=10)list.place(x=100, y=200)list['values']=('全部','人文','艺术','计算机','科技','杂志')list.current(0)global b_name
  50. tk.Label(win, text='书名:', font=('宋体',12)).place(x=200, y=200)
  51. b_name = tk.Entry(win, font=('宋体',12), width=10)
  52. b_name.place(x=250, y=200)global author
  53. tk.Label(win, text='作者:', font=('宋体',12)).place(x=350, y=200)
  54. author = tk.Entry(win, font=('宋体',12), width=10)
  55. author.place(x=400, y=200)
  56. tk.Button(win, text='确认注销', font=('宋体',12), width=10, command=delete).place(x=600, y=195)defdelete():删除图书
  57. sql ="DELETE FROM book WHERE type='%s' AND name='%s' AND author='%s'"%(list.get(),b_name.get(),author.get())
  58. db = pymysql.connect("localhost","root","qwer","library")
  59. cursor = db.cursor()
  60. cursor.execute(sql)
  61. db.commit()#这句不可或缺,当我们修改数据完成后必须要确认才能真正作用到数据库里
  62. msg.showinfo(title='成功!', message='该书已删除!')

在这里插入图片描述

3.6 r_operation读者操作模块

如2.5,如法炮制。但是要引入py内置的datetime模块

  1. import tkinter as tk
  2. import tkinter.messagebox as msg
  3. from tkinter import ttk
  4. import search
  5. import ID
  6. import datetime as dt#datetimeimport pymysql
  7. defframe():
  8. window2=tk.Tk()
  9. window2.title('读者')
  10. window2.geometry('700x600')
  11. lable0 = tk.Label(window2, text='欢迎来到XDU图书馆', bg='pink', font=('微软雅黑',50)).pack()#
  12. lable1 = tk.Label(window2, text='请选择操作:', font=('微软雅黑',20)).place(x=80, y=400)#
  13. tk.Button(window2, text=' 借 书', font=('微软雅黑',15), width=10, height=2,command=borrow).place(x=350, y=250)
  14. tk.Button(window2, text=' 还 书', font=('微软雅黑',15), width=10, height=2,command=turnback).place(x=350, y=350)
  15. tk.Button(window2, text='信息查询', font=('微软雅黑',15), width=10, height=2,command=search.frame).place(x=350, y=450)
  16. window2.mainloop()defborrow():global win
  17. win = tk.Tk()
  18. win.title('读者')
  19. win.geometry('900x300')
  20. win.wm_attributes('-topmost',1)
  21. lable1 = tk.Label(win, text='请填写所借图书的信息:(书名作者都要填写正确无误!)', bg='pink',font=('微软雅黑',20)).place(x=30, y=100)global b_name
  22. tk.Label(win, text='书名:', font=('宋体',12)).place(x=200, y=200)
  23. b_name = tk.Entry(win, font=('宋体',12), width=10)
  24. b_name.place(x=250, y=200)global author
  25. tk.Label(win, text='作者:', font=('宋体',12)).place(x=350, y=200)
  26. author = tk.Entry(win, font=('宋体',12), width=10)
  27. author.place(x=400, y=200)
  28. tk.Button(win, text='确认借书', font=('宋体',12), width=10, command=confirm_borrow).place(x=600, y=195)defconfirm_borrow():
  29. db = pymysql.connect("localhost","root","qwer","library")
  30. cursor = db.cursor()
  31. sql0="SELECT amount FROM book WHERE name='%s' AND author='%s'"%(b_name.get(),author.get())
  32. cursor.execute(sql0)
  33. result=cursor.fetchone()if result:if result !='0':
  34. time = dt.datetime.now().strftime('%F')#得到的时间不是字符串型,我们要把时间转化成字符串型
  35. sql ="INSERT INTO borrow VALUES('%s','%s','%s','%s')"%(ID.getid(),b_name.get(),author.get(),time)
  36. sql1="UPDATE book SET amount=amount-1 WHERE name='%s' AND author='%s'"%(b_name.get(),author.get())
  37. cursor.execute(sql)
  38. cursor.execute(sql1)
  39. db.commit()
  40. db.close()
  41. msg.showinfo(title='成功!', message='借书成功!请一个月之内归还')else:
  42. msg.showinfo(title='失败!', message='您借的书库存不足!')else:
  43. msg.showinfo(title='错误!', message='未找到该书!')defturnback():#还书global win
  44. win = tk.Tk()
  45. win.title('读者')
  46. win.geometry('550x600')
  47. db = pymysql.connect("localhost","root","qwer","library")
  48. cursor = db.cursor()
  49. sql0 ="SELECT COUNT(*) FROM borrow WHERE id='%s'"%(ID.getid())
  50. cursor.execute(sql0)
  51. result = cursor.fetchone()if result[0]==0:
  52. msg.showinfo(title='错误', message='您还没借过书呢!')else:
  53. lable1 = tk.Label(win, text='查询到您有以下书目未还:', bg='pink', font=('微软雅黑',20)).place(x=80, y=20)
  54. tree = ttk.Treeview(win, columns=('1','2'), show="headings")
  55. tree.column('1', width=150, anchor='center')
  56. tree.column('2', width=150, anchor='center')
  57. tree.heading('1', text='书名')
  58. tree.heading('2', text='作者')
  59. tree.place(x=100, y=100)
  60. sql1 ="SELECT bookname,author FROM borrow WHERE id='%s'"%(ID.getid())
  61. cursor.execute(sql1)
  62. result1 = cursor.fetchall()for i inrange(0,result[0]):
  63. tree.insert('', i, values=(result1[i]))
  64. lable2 = tk.Label(win, text='请输入还书信息:', bg='pink', font=('微软雅黑',20)).place(x=80, y=360)
  65. lable22=tk.Label(win, text='书名作者都要填写正确无误!', bg='pink', font=('微软雅黑',20)).place(x=80, y=400)global b_name
  66. tk.Label(win, text='书名:', font=('宋体',12)).place(x=80, y=480)
  67. b_name = tk.Entry(win, font=('宋体',12), width=10)
  68. b_name.place(x=130, y=480)global author
  69. tk.Label(win, text='作者:', font=('宋体',12)).place(x=230, y=480)
  70. author = tk.Entry(win, font=('宋体',12), width=10)
  71. author.place(x=280, y=480)
  72. tk.Button(win, text='确认还书', font=('宋体',12), width=10, command=confirm_turnback).place(x=395, y=480)
  73. db.close()defconfirm_turnback():
  74. db = pymysql.connect("localhost","root","qwer","library")
  75. cursor = db.cursor()
  76. sql1 ="UPDATE book SET amount=amount+1 WHERE name='%s' AND author='%s'"%(b_name.get(), author.get())
  77. cursor.execute(sql1)
  78. db.commit()
  79. time1=dt.datetime.now()#获取现在的时间
  80. sql2="SELECT date FROM borrow WHERE bookname='%s' AND author='%s'"%(b_name.get(),author.get())
  81. cursor.execute(sql2)
  82. result = cursor.fetchone()
  83. day=(time1-result[0]).days#得到时间差,检查图书是否超期print(day)if day>30:
  84. msg.showinfo(title='还书成功', message='还书成功,但您已经超期!请下次按时归还')else:
  85. msg.showinfo(title='还书成功', message='还书成功,且未超过30天')
  86. sql0 ="DELETE FROM borrow WHERE bookname='%s' AND author='%s'"%(b_name.get(), author.get())
  87. cursor.execute(sql0)
  88. db.commit()
  89. db.close()
  90. win.destroy()

3.7 search模块(动态查询)

由于管理员和读者都有查询图书的功能,故为了减少代码,尽量代码重用,将该功能做成模块。search中要有动态查询,即查询条件的个数是可以改变的。

  1. import tkinter as tk
  2. import tkinter.messagebox as msg
  3. from tkinter import ttk
  4. import pymysql
  5. defframe():global window
  6. window = tk.Tk()
  7. window.title('图书查询')
  8. window.geometry('1200x700')
  9. tk.Label(window,text='图书类目:',font=('宋体',12)).place(x=220,y=30)globallist
  10. comvalue=tk.StringVar()list=ttk.Combobox(window,textvariable=comvalue,height=10,width=10)list.place(x=300,y=30)list['values']=('全部','人文','艺术','计算机','科技','杂志')list.current(0)global b_name
  11. tk.Label(window, text='书名:', font=('宋体',12)).place(x=450, y=30)
  12. b_name=tk.Entry(window,font=('宋体',12),width=15)
  13. b_name.place(x=500,y=30)global author
  14. tk.Label(window, text='作者:', font=('宋体',12)).place(x=650, y=30)
  15. author = tk.Entry(window, font=('宋体',12), width=15)
  16. author.place(x=700, y=30)
  17. tk.Button(window,text='搜索',font=('宋体',12), width=10,command=search).place(x=900,y=25)global tree#建立树形图
  18. yscrollbar = ttk.Scrollbar(window, orient='vertical')#右边的滑动按钮
  19. tree = ttk.Treeview(window, columns=('1','2','3','4','5'), show="headings",yscrollcommand=yscrollbar.set)
  20. tree.column('1', width=150, anchor='center')
  21. tree.column('2', width=150, anchor='center')
  22. tree.column('3', width=150, anchor='center')
  23. tree.column('4', width=150, anchor='center')
  24. tree.column('5', width=150, anchor='center')
  25. tree.heading('1', text='类目')
  26. tree.heading('2', text='书名')
  27. tree.heading('3', text='作者')
  28. tree.heading('4', text='价格')
  29. tree.heading('5', text='库存')
  30. tree.place(x=200, y=150)
  31. yscrollbar.place(x=955,y=150)
  32. window.mainloop()defsearch():#我用了最原始的方法来动态查询iflist.get()=='全部'and b_name.get()==''and author.get()=='':
  33. sql="SELECT * FROM book "eliflist.get()=='全部'and b_name.get()==''and author.get()!='':
  34. sql="SELECT * FROM book WHERE author='%s'"%(author.get())eliflist.get()=='全部'and b_name.get()!=''and author.get()=='':
  35. sql ="SELECT * FROM book WHERE name='%s'"%(b_name.get())eliflist.get()!='全部'and b_name.get()==''and author.get()=='':
  36. sql ="SELECT * FROM book WHERE type='%s'"%(list.get())eliflist.get()=='全部'and b_name.get()!=''and author.get()!='':
  37. sql ="SELECT * FROM book WHERE name='%s' AND author='%s'"%(b_name.get(),author.get())eliflist.get()!='全部'and b_name.get()!=''and author.get()=='':
  38. sql ="SELECT * FROM book WHERE type='%s' AND name='%s'"%(list.get(),b_name.get())eliflist.get()!='全部'and b_name.get()==''and author.get()!='':
  39. sql ="SELECT * FROM book WHERE type='%s' AND author ='%s'"%(list.get(), author.get())else:
  40. sql ="SELECT * FROM book WHERE type='%s' AND name='%s' AND author ='%s'"%(list.get(),b_name.get(), author.get())
  41. db = pymysql.connect("localhost","root","qwer","library")
  42. cursor = db.cursor()
  43. cursor.execute(sql)
  44. results=cursor.fetchall()if results:
  45. l=len(results)for i inrange(0,l):#查询到的结果依次插入到表格中
  46. tree.insert('',i,values=(results[i]))else:
  47. tree.insert('',0,values=('查询不到结果','查询不到结果','查询不到结果','查询不到结果','查询不到结果'))
  48. db.close()

四、总结

这是一个较为简单的图书管理系统,其中仍有一些不足。比如人人都能注册管理员账号然后修改图书信息,这会造成系统的不安全。还有就是我们默认的读者借书时候会先查询图书信息,得到图书信息后填写的书名和作者必定是正确的,我没有加上相应的错误处理。然后读者借书超期后有惩罚措施,比如无法借书或限制借书的数量,这个我没有实现,仅仅是提醒了读者有图书已超期。如果有相关问题,欢迎私信作者共同探讨。

提示:转发应得到作者同意,注明出处。


本文转载自: https://blog.csdn.net/cnmlgbnbcs/article/details/110643393
版权归原作者 家兴~ 所有, 如有侵权,请联系我们删除。

“Python实现带GUI和连接数据库的图书管理系统”的评论:

还没有评论