当前位置:   article > 正文

python项目开发——简易图书管理系统_简单通用的图书系统后端管理模板python

简单通用的图书系统后端管理模板python

项目名称:简易图书管理系统

  • 项目简介

开发一个简单的图书管理系统,帮助用户记录和管理图书信息。该系统应具备添加图书、查看图书、搜索图书、更新图书信息和删除图书等基本功能,并将数据存储在文件中。

  • 项目需求

1. 用户界面
  1. 提供简易的命令行界面供用户操作。
  2. 用户可以选择不同的操作:添加图书、查看图书、搜索图书、更新图书信息、删除图书和退出程序。
2. 功能模块
     a.添加图书

     - 用户输入书名、作者、出版年份和ISBN(也就是国际标准书号)。

     - 将图书信息保存到一个数据结构中(如列表)。

     b.查看图书

     - 显示所有已记录的图书信息。

     - 每条记录包括书名、作者、出版年份和ISBN。

     c.搜索图书

     - 用户可以按书名或作者搜索图书,显示匹配的结果。

     d.更新图书信息

     - 用户可以选择某本图书并更新其信息。

     e.删除图书

     - 用户可以按ISBN删除特定的图书记录。

     f.退出程序

     - 用户选择退出程序后,系统保存所有图书记录到一个文件中,以便下次启动时读取。

3. 数据存储

   - 使用文本文件(如.txt文件)保存和读取图书记录,确保数据在程序关闭后不丢失。

  •  项目步骤

1. 初始化项目

   - 创建一个新的Python项目文件。

   - 创建必要的函数和数据结构(如列表)。

2. 实现功能模块

   - 编写添加图书的函数,提示用户输入必要信息,并保存到数据结构中。

   - 编写查看图书的函数,遍历数据结构并打印所有记录。

   - 编写搜索图书的函数,根据用户输入的条件查询数据结构并显示匹配结果。

   - 编写更新图书信息的函数,根据用户选择更新数据结构中的记录。

   - 编写删除图书的函数,根据用户输入的ISBN从数据结构中删除相应记录。

   - 编写文件读写函数,在程序启动时读取文件并加载记录,在程序退出时保存记录到文件。

3. 用户交互界面

   - 使用循环结构创建用户交互界面,根据用户选择调用相应的功能模块。

   - 提供明确的提示信息和错误处理机制,确保用户输入的有效性。

4. 测试和调试

   - 对每个功能模块进行单独测试,确保其正确性。

   - 测试整个系统的工作流程,检查各功能模块之间的交互是否正常。

   - 处理可能出现的异常情况,如文件读写错误、用户输入错误等。

  •  项目扩展

完成基本功能后,可以考虑添加以下扩展功能,完成者将获得更高评分。

- 提供更复杂的搜索功能,例如按出版年份搜索图书。

- 添加用户登录功能,支持不同用户管理自己的图书列表。

- 提供图形用户界面(GUI)实现,使用如Tkinter等库。

实验器材、设备和平台

  1. 计算机
  2. 操作系统:Windows、macOS或Linux
  3. 编程语言:Python 3.x
  4. 开发环境:任意Python集成开发环境(IDE),例如PyCharm、VSCode,或文本编辑器如Sublime Text
  5. 必要的Python库:标准库Tkinter、json、os、hashlib

 接下来,将代码划分成各个模块,解释说明其功能与实现。

项目呈现

一、导入库和变量初始化的实现

  1. import tkinter as tk
  2. from tkinter import messagebox, simpledialog
  3. import json
  4. import os
  5. import hashlib
  6. FILE_PATH_TEMPLATE = "{}_books.json"
  7. current_user = None
  8. books = []
'
运行
  • 导入了 Tkinter 库用以创建图形用户界面。
  • 导入了 messagebox 和 simpledialog 模块用于消息对话框和简单对话框。
  • 导入了 json 库用于处理数据的序列化和反序列化。
  • 导入了 os 库用于文件操作。
  • 导入了 hashlib 库用于密码哈希处理。
  • 初始化文件路径的模板 FILE_PATH_TEMPLATE 以及当前用户变量 current_user 以及图书列表 books

二、数据加载和保存功能的实现

  1. def load_data(username):
  2. global books
  3. file_path = FILE_PATH_TEMPLATE.format(username)
  4. if os.path.exists(file_path):
  5. with open(file_path, "r", encoding="utf-8") as file:
  6. books = json.load(file)
  7. else:
  8. books = []
  9. def save_data(username):
  10. file_path = FILE_PATH_TEMPLATE.format(username)
  11. with open(file_path, "w", encoding="utf-8") as file:
  12. json.dump(books, file, ensure_ascii=False, indent=4)
'
运行
  • load_data(username):根据用户名加载用户对应的图书数据文件,如果文件存在,则读取数据,否则将会初始化为空列表。
  • save_data(username):将当前用户的图书数据保存到文件中。

三、图书管理功能的实现

①添加图书
  1. def add_book():
  2. book_name = simpledialog.askstring("添加图书", "所添书名:")
  3. author = simpledialog.askstring("添加图书", "何许人所著::")
  4. year = simpledialog.askstring("添加图书", "何时所著:")
  5. ISBN = simpledialog.askstring("添加图书", "图书的ISBN(国际标准书号):")
  6. if book_name and author and year and ISBN:
  7. books.append({"书名": book_name, "作者": author, "出版年份": year, "ISBN": ISBN})
  8. messagebox.showinfo("成功", "添加成功了哟!!!")
  9. save_data(current_user)
  10. cat_books()
  11. else:
  12. messagebox.showwarning("错误输入!", "添加失败!!!。")
'
运行
  • 通过对话框来获取书名、作者、出版年份和以及ISBN 信息,并添加到图书列表中。
  • 检查是否为完整的输入,不完整则显示警告信息。
  • 添加成功后保存数据并刷新图书列表。
②查看图书
  1. def cat_books():
  2. listbox.delete(0, tk.END)
  3. for idx, book in enumerate(books, 1):
  4. listbox.insert(tk.END, f"{idx}. 书名: {book['书名']}, 作者: {book['作者']}, 出版年份: {book['出版年份']}, ISBN: {book['ISBN']}")
'
运行
  • 将所有图书信息显示在列表框中。
③搜索图书
  1. def search_books():
  2. search_term = simpledialog.askstring("搜索图书", "请输入书名、作者或出版年份进行搜索:")
  3. if search_term:
  4. listbox.delete(0, tk.END)
  5. results = [book for book in books if search_term in book['书名'] or search_term in book['作者'] or search_term == book['出版年份']]
  6. if results:
  7. for idx, book in enumerate(results, 1):
  8. listbox.insert(tk.END, f"{idx}. 书名: {book['书名']}, 作者: {book['作者']}, 出版年份: {book['出版年份']}, ISBN: {book['ISBN']}")
  9. else:
  10. messagebox.showinfo("伤心", "没有找到/(ㄒoㄒ)/~~。")
'
运行
  • 通过对话框获取搜索关键词,根据书名、作者或出版年份进行搜索。
  • 将搜索结果显示在列表框中,如果没有找到则显示信息框提示。
④更新图书信息
  1. def update_book():
  2. isbn = simpledialog.askstring("更新图书", "请输入图书的ISBN来更新信息:")
  3. for book in books:
  4. if book['ISBN'] == isbn:
  5. book['书名'] = simpledialog.askstring("更新图书", f"请输入新的书名(当前是: {book['书名']}):")
  6. book['作者'] = simpledialog.askstring("更新图书", f"请输入新的作者(当前是: {book['作者']}):")
  7. book['出版年份'] = simpledialog.askstring("更新图书", f"请输入新的出版年份(当前是: {book['出版年份']}):")
  8. messagebox.showinfo("成功", "图书相关信息更新成功o(* ̄▽ ̄*)ブ!")
  9. save_data(current_user)
  10. cat_books()
  11. return
  12. messagebox.showwarning("未找到", "坏了,彻查无果!!")
'
运行
  • 根据输入的 ISBN 查找图书并更新其信息,未找到则提示。
⑤删除图书
  1. def delete_book():
  2. isbn = simpledialog.askstring("删除图书", "请输入要删除图书的ISBN:")
  3. global books
  4. books = [book for book in books if book['ISBN'] != isbn]
  5. messagebox.showinfo("成功", "图书删除成功!")
  6. save_data(current_user)
  7. cat_books()
'
运行
  • 根据输入的 ISBN 删除图书,删除成功后保存数据并刷新列表。

四、用户管理功能的实现

①用户登录
  1. def login():
  2. global current_user
  3. username = simpledialog.askstring("登录", "请输入用户名:")
  4. password = simpledialog.askstring("登录", "请输入密码:", show='*')
  5. if username and password:
  6. hashed_password = hashlib.sha256(password.encode()).hexdigest()
  7. if os.path.exists("users.json"):
  8. with open("users.json", "r", encoding="utf-8") as file:
  9. users = json.load(file)
  10. else:
  11. users = {}
  12. if username in users and users[username] == hashed_password:
  13. current_user = username
  14. messagebox.showinfo("成功", "登录成功!")
  15. load_data(current_user)
  16. cat_books()
  17. enable_buttons()
  18. else:
  19. messagebox.showwarning("失败", "用户名或密码错误!")
  20. else:
  21. messagebox.showwarning("输入错误", "所有字段均为必填。")
'
运行
  • 通过对话框获取用户名和密码,验证用户信息是否正确。
  • 如果验证成功则加载用户数据并启用功能按钮。
②用户注册
  1. def register():
  2. username = simpledialog.askstring("注册", "请输入用户名:")
  3. password = simpledialog.askstring("注册", "请输入密码:", show='*')
  4. if username and password:
  5. hashed_password = hashlib.sha256(password.encode()).hexdigest()
  6. if os.path.exists("users.json"):
  7. with open("users.json", "r", encoding="utf-8") as file:
  8. users = json.load(file)
  9. else:
  10. users = {}
  11. if username not in users:
  12. users[username] = hashed_password
  13. with open("users.json", "w", encoding="utf-8") as file:
  14. json.dump(users, file, ensure_ascii=False, indent=4)
  15. messagebox.showinfo("成功", "注册成功,请登录!")
  16. else:
  17. messagebox.showwarning("失败", "用户名已存在!")
  18. else:
  19. messagebox.showwarning("输入错误", "所有字段均为必填。")
'
运行
  • 通过对话框获取用户名和密码,检查用户名是否已存在,不存在则注册新用户。

③功能按钮的启动

  1. def enable_buttons():
  2. button_add.config(state=tk.NORMAL)
  3. button_view.config(state=tk.NORMAL)
  4. button_search.config(state=tk.NORMAL)
  5. button_update.config(state=tk.NORMAL)
  6. button_delete.config(state=tk.NORMAL)
'
运行
  • 登录成功后将开启各个功能按钮。

④主窗口的创建

  1. window = tk.Tk()
  2. window.title("图书管理系统")
  3. window.geometry("600x400")
  4. tk.Button(window, text="登录", command=login).pack(fill=tk.X)
  5. tk.Button(window, text="注册", command=register).pack(fill=tk.X)
  6. tk.Button(window, text="退出程序", command=window.quit).pack(fill=tk.X)
  7. button_add = tk.Button(window, text="添加图书", command=add_book, state=tk.DISABLED)
  8. button_view = tk.Button(window, text="查看图书", command=cat_books, state=tk.DISABLED)
  9. button_search = tk.Button(window, text="搜索图书", command=search_books, state=tk.DISABLED)
  10. button_update = tk.Button(window, text="更新图书信息", command=update_book, state=tk.DISABLED)
  11. button_delete = tk.Button(window, text="删除图书", command=delete_book, state=tk.DISABLED)
  12. button_add.pack(fill=tk.X)
  13. button_view.pack(fill=tk.X)
  14. button_search.pack(fill=tk.X)
  15. button_update.pack(fill=tk.X)
  16. button_delete.pack(fill=tk.X)
  17. listbox = tk.Listbox(window)
  18. listbox.pack(fill=tk.BOTH, expand=True)
  19. window.mainloop()
  • 创建主窗口并设置其标题和尺寸。
  • 添加登录、注册和退出按钮。
  • 添加功能按钮并初始化为禁用状态。
  • 创建列表框用于显示图书信息。
  • 启动事件循环,显示窗口。

完整代码呈现:

  1. # 导入必要的库以及变量初始化
  2. import tkinter as tk
  3. from tkinter import messagebox, simpledialog
  4. import json
  5. import os
  6. import hashlib
  7. FILE_PATH_TEMPLATE = "{}_books.json"
  8. current_user = None
  9. books = []
  10. '''加载和保存数据'''
  11. # 加载图书数据
  12. def load_data(username):
  13. global books
  14. file_path = FILE_PATH_TEMPLATE.format(username)
  15. if os.path.exists(file_path):
  16. with open(file_path, "r", encoding="utf-8") as file:
  17. books = json.load(file)
  18. else:
  19. books = []
  20. # 保存图书数据
  21. def save_data(username):
  22. file_path = FILE_PATH_TEMPLATE.format(username)
  23. with open(file_path, "w", encoding="utf-8") as file:
  24. json.dump(books, file, ensure_ascii=False, indent=4)
  25. '''图书管理功能'''
  26. # 添加图书
  27. def add_book():
  28. book_name = simpledialog.askstring("添加图书", "所添书名:")
  29. author = simpledialog.askstring("添加图书", "何许人所著::")
  30. year = simpledialog.askstring("添加图书", "何时所著:")
  31. ISBN = simpledialog.askstring("添加图书", "图书的ISBN(国际标准书号):")
  32. if book_name and author and year and ISBN:
  33. books.append({"书名": book_name, "作者": author, "出版年份": year, "ISBN": ISBN})
  34. messagebox.showinfo("成功", "添加成功了哟!!!")
  35. save_data(current_user)
  36. cat_books()
  37. else:
  38. messagebox.showwarning("错误输入!", "添加失败!!!。")
  39. # 查看图书
  40. def cat_books():
  41. listbox.delete(0, tk.END)
  42. for idx, book in enumerate(books, 1):
  43. listbox.insert(tk.END,
  44. f"{idx}. 书名: {book['书名']}, 作者: {book['作者']}, 出版年份: {book['出版年份']}, ISBN: {book['ISBN']}")
  45. # 搜索图书
  46. def search_books():
  47. search_term = simpledialog.askstring("搜索图书", "请输入书名、作者或出版年份进行搜索:")
  48. if search_term:
  49. listbox.delete(0, tk.END)
  50. results = [book for book in books if
  51. search_term in book['书名'] or search_term in book['作者'] or search_term == book['出版年份']]
  52. if results:
  53. for idx, book in enumerate(results, 1):
  54. listbox.insert(tk.END,
  55. f"{idx}. 书名: {book['书名']}, 作者: {book['作者']}, 出版年份: {book['出版年份']}, ISBN: {book['ISBN']}")
  56. else:
  57. messagebox.showinfo("伤心", "没有找到/(ㄒoㄒ)/~~。")
  58. # 更新图书信息
  59. def update_book():
  60. isbn = simpledialog.askstring("更新图书", "请输入图书的ISBN来更新信息:")
  61. for book in books:
  62. if book['ISBN'] == isbn:
  63. book['书名'] = simpledialog.askstring("更新图书", f"请输入新的书名(当前是: {book['书名']}):")
  64. book['作者'] = simpledialog.askstring("更新图书", f"请输入新的作者(当前是: {book['作者']}):")
  65. book['出版年份'] = simpledialog.askstring("更新图书", f"请输入新的出版年份(当前是: {book['出版年份']}):")
  66. messagebox.showinfo("成功", "图书相关信息更新成功o(* ̄▽ ̄*)ブ!")
  67. save_data(current_user)
  68. cat_books()
  69. return
  70. messagebox.showwarning("未找到", "坏了,彻查无果!!")
  71. # 删除图书
  72. def delete_book():
  73. isbn = simpledialog.askstring("删除图书", "请输入要删除图书的ISBN:")
  74. global books
  75. books = [book for book in books if book['ISBN'] != isbn]
  76. messagebox.showinfo("成功", "图书删除成功!")
  77. save_data(current_user)
  78. cat_books()
  79. # 用户登录
  80. def login():
  81. global current_user
  82. username = simpledialog.askstring("登录", "请输入用户名:")
  83. password = simpledialog.askstring("登录", "请输入密码:", show='*')
  84. if username and password:
  85. hashed_password = hashlib.sha256(password.encode()).hexdigest()
  86. if os.path.exists("users.json"):
  87. with open("users.json", "r", encoding="utf-8") as file:
  88. users = json.load(file)
  89. else:
  90. users = {}
  91. if username in users and users[username] == hashed_password:
  92. current_user = username
  93. messagebox.showinfo("成功", "登录成功!")
  94. load_data(current_user)
  95. cat_books()
  96. enable_buttons()
  97. else:
  98. messagebox.showwarning("失败", "用户名或密码错误!")
  99. else:
  100. messagebox.showwarning("输入错误", "所有字段均为必填。")
  101. '''用户管理系统'''
  102. # 用户注册
  103. def register():
  104. username = simpledialog.askstring("注册", "请输入用户名:")
  105. password = simpledialog.askstring("注册", "请输入密码:", show='*')
  106. if username and password:
  107. hashed_password = hashlib.sha256(password.encode()).hexdigest()
  108. if os.path.exists("users.json"):
  109. with open("users.json", "r", encoding="utf-8") as file:
  110. users = json.load(file)
  111. else:
  112. users = {}
  113. if username not in users:
  114. users[username] = hashed_password
  115. with open("users.json", "w", encoding="utf-8") as file:
  116. json.dump(users, file, ensure_ascii=False, indent=4)
  117. messagebox.showinfo("成功", "注册成功,请登录!")
  118. else:
  119. messagebox.showwarning("失败", "用户名已存在!")
  120. else:
  121. messagebox.showwarning("输入错误", "所有字段均为必填。")
  122. '''功能按钮的启动'''
  123. def enable_buttons():
  124. button_add.config(state=tk.NORMAL)
  125. button_view.config(state=tk.NORMAL)
  126. button_search.config(state=tk.NORMAL)
  127. button_update.config(state=tk.NORMAL)
  128. button_delete.config(state=tk.NORMAL)
  129. '''主窗口的创建'''
  130. # 创建主窗口
  131. window = tk.Tk()
  132. window.title("图书管理系统")
  133. window.geometry("600x400")
  134. # 登录和注册按钮
  135. tk.Button(window, text="登录", command=login).pack(fill=tk.X)
  136. tk.Button(window, text="注册", command=register).pack(fill=tk.X)
  137. tk.Button(window, text="退出程序", command=window.quit).pack(fill=tk.X)
  138. # 添加按钮(在用户登录后启用)
  139. button_add = tk.Button(window, text="添加图书", command=add_book, state=tk.DISABLED)
  140. button_view = tk.Button(window, text="查看图书", command=cat_books, state=tk.DISABLED)
  141. button_search = tk.Button(window, text="搜索图书", command=search_books, state=tk.DISABLED)
  142. button_update = tk.Button(window, text="更新图书信息", command=update_book, state=tk.DISABLED)
  143. button_delete = tk.Button(window, text="删除图书", command=delete_book, state=tk.DISABLED)
  144. button_add.pack(fill=tk.X)
  145. button_view.pack(fill=tk.X)
  146. button_search.pack(fill=tk.X)
  147. button_update.pack(fill=tk.X)
  148. button_delete.pack(fill=tk.X)
  149. # 列表示例显示
  150. listbox = tk.Listbox(window)
  151. listbox.pack(fill=tk.BOTH, expand=True)
  152. # 启动事件循环
  153. window.mainloop()

项目特色

1. 用户管理系统

  • 注册和登录:系统具备用户注册和登录功能,通过用户名和密码确保每个用户的数据独立和安全。密码采用SHA-256哈希加密存储,增加了系统的安全性。
  • 数据隔离:每个用户的图书数据存储在独立的JSON文件中,保证了数据的隔离和隐私性。

2. 图书管理功能

  • 全面的图书管理:提供添加、查看、搜索、更新和删除图书的功能,满足了基本的图书管理需求。
  • 搜索功能:支持按书名、作者和出版年份进行搜索,方便用户快速查找图书。

3. 友好的用户界面

  • 命令行界面:通过简单直观的命令行界面与用户交互,操作方便。
  • 图形用户界面(GUI):使用Tkinter库开发的图形界面,使用户体验更加友好和直观。

4. 数据持久化

  • 文件存储:所有数据均保存在本地JSON文件中,保证了数据的持久化。每次操作后自动保存数据,确保不会丢失。

5.未来小改进方向

  • 引入数据库:在数据存储方面,未来可以考虑使用数据库来管理数据,以此提高数据处理的效率和安全性。

谢谢观看,点个小赞~~~o(* ̄▽ ̄*)ブ

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/828157
推荐阅读
相关标签
  

闽ICP备14008679号