赞
踩
项目名称:简易图书管理系统
开发一个简单的图书管理系统,帮助用户记录和管理图书信息。该系统应具备添加图书、查看图书、搜索图书、更新图书信息和删除图书等基本功能,并将数据存储在文件中。
- 用户输入书名、作者、出版年份和ISBN(也就是国际标准书号)。
- 将图书信息保存到一个数据结构中(如列表)。
- 显示所有已记录的图书信息。
- 每条记录包括书名、作者、出版年份和ISBN。
- 用户可以按书名或作者搜索图书,显示匹配的结果。
- 用户可以选择某本图书并更新其信息。
- 用户可以按ISBN删除特定的图书记录。
- 用户选择退出程序后,系统保存所有图书记录到一个文件中,以便下次启动时读取。
- 使用文本文件(如.txt文件)保存和读取图书记录,确保数据在程序关闭后不丢失。
- 创建一个新的Python项目文件。
- 创建必要的函数和数据结构(如列表)。
- 编写添加图书的函数,提示用户输入必要信息,并保存到数据结构中。
- 编写查看图书的函数,遍历数据结构并打印所有记录。
- 编写搜索图书的函数,根据用户输入的条件查询数据结构并显示匹配结果。
- 编写更新图书信息的函数,根据用户选择更新数据结构中的记录。
- 编写删除图书的函数,根据用户输入的ISBN从数据结构中删除相应记录。
- 编写文件读写函数,在程序启动时读取文件并加载记录,在程序退出时保存记录到文件。
- 使用循环结构创建用户交互界面,根据用户选择调用相应的功能模块。
- 提供明确的提示信息和错误处理机制,确保用户输入的有效性。
- 对每个功能模块进行单独测试,确保其正确性。
- 测试整个系统的工作流程,检查各功能模块之间的交互是否正常。
- 处理可能出现的异常情况,如文件读写错误、用户输入错误等。
完成基本功能后,可以考虑添加以下扩展功能,完成者将获得更高评分。
- 提供更复杂的搜索功能,例如按出版年份搜索图书。
- 添加用户登录功能,支持不同用户管理自己的图书列表。
- 提供图形用户界面(GUI)实现,使用如Tkinter等库。
接下来,将代码划分成各个模块,解释说明其功能与实现。
- import tkinter as tk
- from tkinter import messagebox, simpledialog
- import json
- import os
- import hashlib
-
- FILE_PATH_TEMPLATE = "{}_books.json"
- current_user = None
- books = []
'运行
messagebox
和 simpledialog
模块用于消息对话框和简单对话框。json
库用于处理数据的序列化和反序列化。os
库用于文件操作。hashlib
库用于密码哈希处理。FILE_PATH_TEMPLATE
以及当前用户变量 current_user
以及图书列表 books
。- def load_data(username):
- global books
- file_path = FILE_PATH_TEMPLATE.format(username)
- if os.path.exists(file_path):
- with open(file_path, "r", encoding="utf-8") as file:
- books = json.load(file)
- else:
- books = []
-
- def save_data(username):
- file_path = FILE_PATH_TEMPLATE.format(username)
- with open(file_path, "w", encoding="utf-8") as file:
- json.dump(books, file, ensure_ascii=False, indent=4)
'运行
load_data(username)
:根据用户名加载用户对应的图书数据文件,如果文件存在,则读取数据,否则将会初始化为空列表。save_data(username)
:将当前用户的图书数据保存到文件中。- def add_book():
- book_name = simpledialog.askstring("添加图书", "所添书名:")
- author = simpledialog.askstring("添加图书", "何许人所著::")
- year = simpledialog.askstring("添加图书", "何时所著:")
- ISBN = simpledialog.askstring("添加图书", "图书的ISBN(国际标准书号):")
-
- if book_name and author and year and ISBN:
- books.append({"书名": book_name, "作者": author, "出版年份": year, "ISBN": ISBN})
- messagebox.showinfo("成功", "添加成功了哟!!!")
- save_data(current_user)
- cat_books()
- else:
- messagebox.showwarning("错误输入!", "添加失败!!!。")
'运行
- def cat_books():
- listbox.delete(0, tk.END)
- for idx, book in enumerate(books, 1):
- listbox.insert(tk.END, f"{idx}. 书名: {book['书名']}, 作者: {book['作者']}, 出版年份: {book['出版年份']}, ISBN: {book['ISBN']}")
'运行
- def search_books():
- search_term = simpledialog.askstring("搜索图书", "请输入书名、作者或出版年份进行搜索:")
- if search_term:
- listbox.delete(0, tk.END)
- results = [book for book in books if search_term in book['书名'] or search_term in book['作者'] or search_term == book['出版年份']]
-
- if results:
- for idx, book in enumerate(results, 1):
- listbox.insert(tk.END, f"{idx}. 书名: {book['书名']}, 作者: {book['作者']}, 出版年份: {book['出版年份']}, ISBN: {book['ISBN']}")
- else:
- messagebox.showinfo("伤心", "没有找到/(ㄒoㄒ)/~~。")
'运行
- def update_book():
- isbn = simpledialog.askstring("更新图书", "请输入图书的ISBN来更新信息:")
- for book in books:
- if book['ISBN'] == isbn:
- book['书名'] = simpledialog.askstring("更新图书", f"请输入新的书名(当前是: {book['书名']}):")
- book['作者'] = simpledialog.askstring("更新图书", f"请输入新的作者(当前是: {book['作者']}):")
- book['出版年份'] = simpledialog.askstring("更新图书", f"请输入新的出版年份(当前是: {book['出版年份']}):")
-
- messagebox.showinfo("成功", "图书相关信息更新成功o(* ̄▽ ̄*)ブ!")
- save_data(current_user)
- cat_books()
- return
- messagebox.showwarning("未找到", "坏了,彻查无果!!")
'运行
- def delete_book():
- isbn = simpledialog.askstring("删除图书", "请输入要删除图书的ISBN:")
- global books
- books = [book for book in books if book['ISBN'] != isbn]
- messagebox.showinfo("成功", "图书删除成功!")
- save_data(current_user)
- cat_books()
'运行
- def login():
- global current_user
- username = simpledialog.askstring("登录", "请输入用户名:")
- password = simpledialog.askstring("登录", "请输入密码:", show='*')
- if username and password:
- hashed_password = hashlib.sha256(password.encode()).hexdigest()
- if os.path.exists("users.json"):
- with open("users.json", "r", encoding="utf-8") as file:
- users = json.load(file)
- else:
- users = {}
-
- if username in users and users[username] == hashed_password:
- current_user = username
- messagebox.showinfo("成功", "登录成功!")
- load_data(current_user)
- cat_books()
- enable_buttons()
- else:
- messagebox.showwarning("失败", "用户名或密码错误!")
- else:
- messagebox.showwarning("输入错误", "所有字段均为必填。")
'运行
- def register():
- username = simpledialog.askstring("注册", "请输入用户名:")
- password = simpledialog.askstring("注册", "请输入密码:", show='*')
- if username and password:
- hashed_password = hashlib.sha256(password.encode()).hexdigest()
- if os.path.exists("users.json"):
- with open("users.json", "r", encoding="utf-8") as file:
- users = json.load(file)
- else:
- users = {}
-
- if username not in users:
- users[username] = hashed_password
- with open("users.json", "w", encoding="utf-8") as file:
- json.dump(users, file, ensure_ascii=False, indent=4)
- messagebox.showinfo("成功", "注册成功,请登录!")
- else:
- messagebox.showwarning("失败", "用户名已存在!")
- else:
- messagebox.showwarning("输入错误", "所有字段均为必填。")
'运行
- def enable_buttons():
- button_add.config(state=tk.NORMAL)
- button_view.config(state=tk.NORMAL)
- button_search.config(state=tk.NORMAL)
- button_update.config(state=tk.NORMAL)
- button_delete.config(state=tk.NORMAL)
'运行
- window = tk.Tk()
- window.title("图书管理系统")
- window.geometry("600x400")
-
- tk.Button(window, text="登录", command=login).pack(fill=tk.X)
- tk.Button(window, text="注册", command=register).pack(fill=tk.X)
- tk.Button(window, text="退出程序", command=window.quit).pack(fill=tk.X)
-
- button_add = tk.Button(window, text="添加图书", command=add_book, state=tk.DISABLED)
- button_view = tk.Button(window, text="查看图书", command=cat_books, state=tk.DISABLED)
- button_search = tk.Button(window, text="搜索图书", command=search_books, state=tk.DISABLED)
- button_update = tk.Button(window, text="更新图书信息", command=update_book, state=tk.DISABLED)
- button_delete = tk.Button(window, text="删除图书", command=delete_book, state=tk.DISABLED)
-
- button_add.pack(fill=tk.X)
- button_view.pack(fill=tk.X)
- button_search.pack(fill=tk.X)
- button_update.pack(fill=tk.X)
- button_delete.pack(fill=tk.X)
-
- listbox = tk.Listbox(window)
- listbox.pack(fill=tk.BOTH, expand=True)
-
- window.mainloop()

- # 导入必要的库以及变量初始化
- import tkinter as tk
- from tkinter import messagebox, simpledialog
- import json
- import os
- import hashlib
-
- FILE_PATH_TEMPLATE = "{}_books.json"
- current_user = None
- books = []
-
- '''加载和保存数据'''
-
-
- # 加载图书数据
- def load_data(username):
- global books
- file_path = FILE_PATH_TEMPLATE.format(username)
- if os.path.exists(file_path):
- with open(file_path, "r", encoding="utf-8") as file:
- books = json.load(file)
- else:
- books = []
-
-
- # 保存图书数据
- def save_data(username):
- file_path = FILE_PATH_TEMPLATE.format(username)
- with open(file_path, "w", encoding="utf-8") as file:
- json.dump(books, file, ensure_ascii=False, indent=4)
-
-
- '''图书管理功能'''
-
-
- # 添加图书
- def add_book():
- book_name = simpledialog.askstring("添加图书", "所添书名:")
- author = simpledialog.askstring("添加图书", "何许人所著::")
- year = simpledialog.askstring("添加图书", "何时所著:")
- ISBN = simpledialog.askstring("添加图书", "图书的ISBN(国际标准书号):")
-
- if book_name and author and year and ISBN:
- books.append({"书名": book_name, "作者": author, "出版年份": year, "ISBN": ISBN})
- messagebox.showinfo("成功", "添加成功了哟!!!")
- save_data(current_user)
- cat_books()
- else:
- messagebox.showwarning("错误输入!", "添加失败!!!。")
-
-
- # 查看图书
- def cat_books():
- listbox.delete(0, tk.END)
- for idx, book in enumerate(books, 1):
- listbox.insert(tk.END,
- f"{idx}. 书名: {book['书名']}, 作者: {book['作者']}, 出版年份: {book['出版年份']}, ISBN: {book['ISBN']}")
-
-
- # 搜索图书
- def search_books():
- search_term = simpledialog.askstring("搜索图书", "请输入书名、作者或出版年份进行搜索:")
- if search_term:
- listbox.delete(0, tk.END)
- results = [book for book in books if
- search_term in book['书名'] or search_term in book['作者'] or search_term == book['出版年份']]
-
- if results:
- for idx, book in enumerate(results, 1):
- listbox.insert(tk.END,
- f"{idx}. 书名: {book['书名']}, 作者: {book['作者']}, 出版年份: {book['出版年份']}, ISBN: {book['ISBN']}")
- else:
- messagebox.showinfo("伤心", "没有找到/(ㄒoㄒ)/~~。")
-
-
- # 更新图书信息
- def update_book():
- isbn = simpledialog.askstring("更新图书", "请输入图书的ISBN来更新信息:")
- for book in books:
- if book['ISBN'] == isbn:
- book['书名'] = simpledialog.askstring("更新图书", f"请输入新的书名(当前是: {book['书名']}):")
- book['作者'] = simpledialog.askstring("更新图书", f"请输入新的作者(当前是: {book['作者']}):")
- book['出版年份'] = simpledialog.askstring("更新图书", f"请输入新的出版年份(当前是: {book['出版年份']}):")
-
- messagebox.showinfo("成功", "图书相关信息更新成功o(* ̄▽ ̄*)ブ!")
- save_data(current_user)
- cat_books()
- return
- messagebox.showwarning("未找到", "坏了,彻查无果!!")
-
-
- # 删除图书
- def delete_book():
- isbn = simpledialog.askstring("删除图书", "请输入要删除图书的ISBN:")
- global books
- books = [book for book in books if book['ISBN'] != isbn]
- messagebox.showinfo("成功", "图书删除成功!")
- save_data(current_user)
- cat_books()
-
-
- # 用户登录
- def login():
- global current_user
- username = simpledialog.askstring("登录", "请输入用户名:")
- password = simpledialog.askstring("登录", "请输入密码:", show='*')
- if username and password:
- hashed_password = hashlib.sha256(password.encode()).hexdigest()
- if os.path.exists("users.json"):
- with open("users.json", "r", encoding="utf-8") as file:
- users = json.load(file)
- else:
- users = {}
-
- if username in users and users[username] == hashed_password:
- current_user = username
- messagebox.showinfo("成功", "登录成功!")
- load_data(current_user)
- cat_books()
- enable_buttons()
- else:
- messagebox.showwarning("失败", "用户名或密码错误!")
- else:
- messagebox.showwarning("输入错误", "所有字段均为必填。")
-
-
- '''用户管理系统'''
-
-
- # 用户注册
- def register():
- username = simpledialog.askstring("注册", "请输入用户名:")
- password = simpledialog.askstring("注册", "请输入密码:", show='*')
- if username and password:
- hashed_password = hashlib.sha256(password.encode()).hexdigest()
- if os.path.exists("users.json"):
- with open("users.json", "r", encoding="utf-8") as file:
- users = json.load(file)
- else:
- users = {}
-
- if username not in users:
- users[username] = hashed_password
- with open("users.json", "w", encoding="utf-8") as file:
- json.dump(users, file, ensure_ascii=False, indent=4)
- messagebox.showinfo("成功", "注册成功,请登录!")
- else:
- messagebox.showwarning("失败", "用户名已存在!")
- else:
- messagebox.showwarning("输入错误", "所有字段均为必填。")
-
-
- '''功能按钮的启动'''
-
-
- def enable_buttons():
- button_add.config(state=tk.NORMAL)
- button_view.config(state=tk.NORMAL)
- button_search.config(state=tk.NORMAL)
- button_update.config(state=tk.NORMAL)
- button_delete.config(state=tk.NORMAL)
-
-
- '''主窗口的创建'''
-
- # 创建主窗口
- window = tk.Tk()
- window.title("图书管理系统")
- window.geometry("600x400")
-
- # 登录和注册按钮
- tk.Button(window, text="登录", command=login).pack(fill=tk.X)
- tk.Button(window, text="注册", command=register).pack(fill=tk.X)
- tk.Button(window, text="退出程序", command=window.quit).pack(fill=tk.X)
-
- # 添加按钮(在用户登录后启用)
- button_add = tk.Button(window, text="添加图书", command=add_book, state=tk.DISABLED)
- button_view = tk.Button(window, text="查看图书", command=cat_books, state=tk.DISABLED)
- button_search = tk.Button(window, text="搜索图书", command=search_books, state=tk.DISABLED)
- button_update = tk.Button(window, text="更新图书信息", command=update_book, state=tk.DISABLED)
- button_delete = tk.Button(window, text="删除图书", command=delete_book, state=tk.DISABLED)
-
- button_add.pack(fill=tk.X)
- button_view.pack(fill=tk.X)
- button_search.pack(fill=tk.X)
- button_update.pack(fill=tk.X)
- button_delete.pack(fill=tk.X)
-
- # 列表示例显示
- listbox = tk.Listbox(window)
- listbox.pack(fill=tk.BOTH, expand=True)
-
- # 启动事件循环
- window.mainloop()

谢谢观看,点个小赞~~~o(* ̄▽ ̄*)ブ
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。