python图书管理系统基于sqlite实现增删查改并且用tk做一个循环的交互式界面
时间: 2023-07-18 11:21:39 浏览: 54
好的,这是一个比较全面的需求,下面是一个 Python 图书管理系统基于 SQLite 实现增删查改的代码实现,以及一个基于 Tk 的交互式界面:
```python
import sqlite3
import tkinter as tk
from tkinter import messagebox
# 连接 SQLite 数据库
conn = sqlite3.connect('books.db')
c = conn.cursor()
# 创建图书表
c.execute('''CREATE TABLE IF NOT EXISTS books
(id INTEGER PRIMARY KEY,
title TEXT,
author TEXT,
year INTEGER,
isbn INTEGER)''')
# 添加图书函数
def add_book():
title = title_entry.get()
author = author_entry.get()
year = year_entry.get()
isbn = isbn_entry.get()
c.execute("INSERT INTO books VALUES (NULL,?,?,?,?)", (title, author, year, isbn))
conn.commit()
messagebox.showinfo("Success", "Book added successfully!")
clear_entries()
# 删除图书函数
def delete_book():
selected = tree.selection()
if not selected:
messagebox.showerror("Error", "Please select a book to delete!")
else:
confirmation = messagebox.askyesno("Confirmation", "Are you sure you want to delete the selected book?")
if confirmation:
for item in selected:
c.execute("DELETE FROM books WHERE id=?", (tree.item(item)["text"],))
conn.commit()
messagebox.showinfo("Success", "Book deleted successfully!")
clear_entries()
# 更新图书函数
def update_book():
selected = tree.selection()
if not selected:
messagebox.showerror("Error", "Please select a book to update!")
else:
title = title_entry.get()
author = author_entry.get()
year = year_entry.get()
isbn = isbn_entry.get()
confirmation = messagebox.askyesno("Confirmation", "Are you sure you want to update the selected book?")
if confirmation:
for item in selected:
c.execute("UPDATE books SET title=?, author=?, year=?, isbn=? WHERE id=?",
(title, author, year, isbn, tree.item(item)["text"]))
conn.commit()
messagebox.showinfo("Success", "Book updated successfully!")
clear_entries()
# 查询图书函数
def search_book():
title = title_entry.get()
author = author_entry.get()
year = year_entry.get()
isbn = isbn_entry.get()
c.execute("SELECT * FROM books WHERE title=? OR author=? OR year=? OR isbn=?", (title, author, year, isbn))
rows = c.fetchall()
if rows:
for item in tree.get_children():
tree.delete(item)
for row in rows:
tree.insert("", tk.END, text=row[0], values=(row[1], row[2], row[3], row[4]))
else:
messagebox.showwarning("Not Found", "No matching books found!")
# 清空输入框函数
def clear_entries():
title_entry.delete(0, tk.END)
author_entry.delete(0, tk.END)
year_entry.delete(0, tk.END)
isbn_entry.delete(0, tk.END)
# 创建 Tk 窗口
root = tk.Tk()
root.title("Book Manager")
# 创建标签和输入框
title_label = tk.Label(root, text="Title")
title_label.grid(row=0, column=0, padx=10, pady=10)
title_entry = tk.Entry(root)
title_entry.grid(row=0, column=1)
author_label = tk.Label(root, text="Author")
author_label.grid(row=1, column=0, padx=10, pady=10)
author_entry = tk.Entry(root)
author_entry.grid(row=1, column=1)
year_label = tk.Label(root, text="Year")
year_label.grid(row=2, column=0, padx=10, pady=10)
year_entry = tk.Entry(root)
year_entry.grid(row=2, column=1)
isbn_label = tk.Label(root, text="ISBN")
isbn_label.grid(row=3, column=0, padx=10, pady=10)
isbn_entry = tk.Entry(root)
isbn_entry.grid(row=3, column=1)
# 创建按钮
add_button = tk.Button(root, text="Add Book", command=add_book)
add_button.grid(row=0, column=2, padx=10)
delete_button = tk.Button(root, text="Delete Book", command=delete_book)
delete_button.grid(row=1, column=2, padx=10)
update_button = tk.Button(root, text="Update Book", command=update_book)
update_button.grid(row=2, column=2, padx=10)
search_button = tk.Button(root, text="Search Book", command=search_book)
search_button.grid(row=3, column=2, padx=10)
clear_button = tk.Button(root, text="Clear Entries", command=clear_entries)
clear_button.grid(row=4, column=2, padx=10)
# 创建表格
tree = tk.ttk.Treeview(root, columns=("title", "author", "year", "isbn"))
tree.column("#0", width=50, minwidth=50)
tree.column("title", width=200, minwidth=200)
tree.column("author", width=150, minwidth=150)
tree.column("year", width=100, minwidth=100)
tree.column("isbn", width=100, minwidth=100)
tree.heading("#0", text="ID", anchor=tk.W)
tree.heading("title", text="Title", anchor=tk.W)
tree.heading("author", text="Author", anchor=tk.W)
tree.heading("year", text="Year", anchor=tk.W)
tree.heading("isbn", text="ISBN", anchor=tk.W)
tree.grid(row=5, column=0, columnspan=3, padx=10, pady=10)
# 加载图书列表
c.execute("SELECT * FROM books")
rows = c.fetchall()
for row in rows:
tree.insert("", tk.END, text=row[0], values=(row[1], row[2], row[3], row[4]))
# 循环运行 Tk 窗口
root.mainloop()
# 关闭 SQLite 连接
conn.close()
```
这个代码实现了图书管理系统的基本功能,包括添加、删除、更新、查询和清空输入框。它还使用了 Tkinter 库创建了一个交互式界面,其中包括标签、输入框、按钮和表格。您可以将此代码保存为 `book_manager.py` 并在终端中运行,或使用 PyCharm 进行编辑和运行。
请注意,此代码仅作为示例和指导,您可以根据自己的需求和喜好进行修改和扩展。