python+tkinter实现图书管理系统(首发)

7 篇文章 5 订阅 ¥29.90 ¥99.00
  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,以下是用 Python+MySQL+tkinter 实现图书管理系统,并实现图书数据分析的代码示例: 1. 创建数据库并建立连接 ``` import MySQLdb # 建立数据库连接 db = MySQLdb.connect(host="localhost", user="root", passwd="password", db="library") # 使用 cursor() 方法创建一个游标对象 cursor cursor = db.cursor() # 创建图书表 cursor.execute("""CREATE TABLE books ( id INT(11) NOT NULL AUTO_INCREMENT, title VARCHAR(255) NOT NULL, author VARCHAR(255) NOT NULL, publisher VARCHAR(255) NOT NULL, pubdate DATE NOT NULL, price FLOAT NOT NULL, PRIMARY KEY (id) )""") ``` 2. 创建图书管理系统界面 ``` import tkinter as tk # 创建主窗口 root = tk.Tk() root.title("图书管理系统") root.geometry("500x400") # 标题 title_label = tk.Label(root, text="图书管理系统", font=("Arial", 20)) title_label.pack(pady=20) # 添加图书表单 title_label = tk.Label(root, text="添加图书", font=("Arial", 16)) title_label.pack() title_entry = tk.Entry(root) title_entry.pack(pady=10) author_entry = tk.Entry(root) author_entry.pack(pady=10) publisher_entry = tk.Entry(root) publisher_entry.pack(pady=10) pubdate_entry = tk.Entry(root) pubdate_entry.pack(pady=10) price_entry = tk.Entry(root) price_entry.pack(pady=10) add_button = tk.Button(root, text="添加图书", command=add_book) add_button.pack(pady=20) # 查询图书表单 title_label = tk.Label(root, text="查询图书", font=("Arial", 16)) title_label.pack() query_entry = tk.Entry(root) query_entry.pack(pady=10) query_button = tk.Button(root, text="查询图书", command=query_book) query_button.pack(pady=20) # 图书列表 books_listbox = tk.Listbox(root, height=10, width=80) books_listbox.pack(pady=20) # 删除图书按钮 delete_button = tk.Button(root, text="删除图书", command=delete_book) delete_button.pack() root.mainloop() ``` 3. 实现图书管理功能 ``` def add_book(): # 获取表单数据 title = title_entry.get() author = author_entry.get() publisher = publisher_entry.get() pubdate = pubdate_entry.get() price = price_entry.get() # 插入数据到数据库 cursor.execute("INSERT INTO books (title, author, publisher, pubdate, price) VALUES (%s, %s, %s, %s, %s)", (title, author, publisher, pubdate, price)) db.commit() # 清空表单 title_entry.delete(0, tk.END) author_entry.delete(0, tk.END) publisher_entry.delete(0, tk.END) pubdate_entry.delete(0, tk.END) price_entry.delete(0, tk.END) def query_book(): # 获取查询关键词 keyword = query_entry.get() # 查询数据 cursor.execute("SELECT * FROM books WHERE title LIKE %s OR author LIKE %s OR publisher LIKE %s", ('%' + keyword + '%', '%' + keyword + '%', '%' + keyword + '%')) books = cursor.fetchall() # 清空列表 books_listbox.delete(0, tk.END) # 添加查询结果到列表 for book in books: books_listbox.insert(tk.END, book) def delete_book(): # 获取选中的图书 ID selected_books = books_listbox.curselection() book_ids = [books_listbox.get(index)[0] for index in selected_books] # 删除数据 for book_id in book_ids: cursor.execute("DELETE FROM books WHERE id=%s", (book_id,)) db.commit() # 刷新列表 query_book() ``` 4. 数据分析功能 ``` import pandas as pd import matplotlib.pyplot as plt # 从数据库中读取数据 cursor.execute("SELECT * FROM books") books = cursor.fetchall() # 转化为 pandas DataFrame df = pd.DataFrame(list(books), columns=['id', 'title', 'author', 'publisher', 'pubdate', 'price']) # 按年份统计图书数量 df['year'] = pd.to_datetime(df['pubdate']).dt.year year_count = df.groupby('year')['id'].count() # 绘制图书借阅量随时间的变化曲线 plt.plot(year_count.index, year_count.values) plt.xlabel('Year') plt.ylabel('Number of books') plt.show() ``` 5. 完善功能 你可以根据需求添加更多的功能,例如图书分类、借阅记录等。 希望这些代码能帮助你完成图书管理系统和数据分析功能。注意修改连接数据库的用户名、密码和数据库名等信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天天501

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值