用python编写一个拥有图形界面的稍微复杂的图书管理系统
时间: 2023-09-04 12:11:57 浏览: 52
好的,下面是一个稍微复杂的图书管理系统的Python代码示例,它拥有图形界面:
```python
from tkinter import *
import sqlite3
# 创建图形用户界面
root = Tk()
root.title("图书管理系统")
# 连接到SQLite数据库
conn = sqlite3.connect('books.db')
c = conn.cursor()
# 创建books表格
c.execute('''CREATE TABLE IF NOT EXISTS books
(id INTEGER PRIMARY KEY, title TEXT, author TEXT, year INTEGER, isbn INTEGER, status TEXT)''')
# 添加图书信息到books表格
def add_book():
# 获取用户输入的信息
title = title_entry.get()
author = author_entry.get()
year = year_entry.get()
isbn = isbn_entry.get()
status = "available" # 默认状态为可借阅
# 插入数据到books表格
c.execute("INSERT INTO books (title, author, year, isbn, status) VALUES (?, ?, ?, ?, ?)", (title, author, year, isbn, status))
conn.commit()
# 清空用户输入框
title_entry.delete(0, END)
author_entry.delete(0, END)
year_entry.delete(0, END)
isbn_entry.delete(0, END)
# 删除选中的图书
def delete_book():
# 获取选中的图书的ID
selected_book = book_listbox.curselection()
book_id = book_listbox.get(selected_book)[0]
# 从books表格中删除选中的图书
c.execute("DELETE FROM books WHERE id=?", (book_id,))
conn.commit()
# 更新选中的图书的信息
def update_book():
# 获取选中的图书的ID
selected_book = book_listbox.curselection()
book_id = book_listbox.get(selected_book)[0]
# 获取用户输入的信息
title = title_entry.get()
author = author_entry.get()
year = year_entry.get()
isbn = isbn_entry.get()
status = status_entry.get()
# 更新books表格中选中的图书的信息
c.execute("UPDATE books SET title=?, author=?, year=?, isbn=?, status=? WHERE id=?", (title, author, year, isbn, status, book_id))
conn.commit()
# 搜索图书
def search_book():
# 获取用户输入的关键字
keyword = search_entry.get()
# 查询books表格中匹配的图书
c.execute("SELECT * FROM books WHERE title LIKE ? OR author LIKE ? OR isbn LIKE ?", ('%'+keyword+'%', '%'+keyword+'%', '%'+keyword+'%'))
rows = c.fetchall()
# 清空图书列表框
book_listbox.delete(0, END)
# 将匹配的图书添加到图书列表框
for row in rows:
book_listbox.insert(END, row)
# 显示选中的图书的详细信息
def show_info(event):
# 获取选中的图书的ID
selected_book = book_listbox.curselection()
book_id = book_listbox.get(selected_book)[0]
# 查询books表格中选中的图书的详细信息
c.execute("SELECT * FROM books WHERE id=?", (book_id,))
row = c.fetchone()
title_entry.delete(0, END)
title_entry.insert(END, row[1])
author_entry.delete(0, END)
author_entry.insert(END, row[2])
year_entry.delete(0, END)
year_entry.insert(END, row[3])
isbn_entry.delete(0, END)
isbn_entry.insert(END, row[4])
status_entry.delete(0, END)
status_entry.insert(END, row[5])
# 显示所有图书的列表
def show_list():
# 查询books表格中所有的图书
c.execute("SELECT * FROM books")
rows = c.fetchall()
# 清空图书列表框
book_listbox.delete(0, END)
# 将所有图书添加到图书列表框
for row in rows:
book_listbox.insert(END, row)
# 创建标签和输入框
title_label = Label(root, text="书名")
title_label.grid(row=0, column=0)
title_entry = Entry(root)
title_entry.grid(row=0, column=1)
author_label = Label(root, text="作者")
author_label.grid(row=0, column=2)
author_entry = Entry(root)
author_entry.grid(row=0, column=3)
year_label = Label(root, text="出版年份")
year_label.grid(row=1, column=0)
year_entry = Entry(root)
year_entry.grid(row=1, column=1)
isbn_label = Label(root, text="ISBN号码")
isbn_label.grid(row=1, column=2)
isbn_entry = Entry(root)
isbn_entry.grid(row=1, column=3)
status_label = Label(root, text="状态")
status_label.grid(row=2, column=0)
status_entry = Entry(root)
status_entry.grid(row=2, column=1)
search_label = Label(root, text="搜索")
search_label.grid(row=3, column=0)
search_entry = Entry(root)
search_entry.grid(row=3, column=1)
search_button = Button(root, text="搜索", command=search_book)
search_button.grid(row=3, column=2)
# 创建图书列表框和滚动条
book_listbox = Listbox(root, width=50)
book_listbox.grid(row=4, column=0, rowspan=6, columnspan=3)
scrollbar = Scrollbar(root)
scrollbar.grid(row=4, column=3, rowspan=6)
book_listbox.configure(yscrollcommand=scrollbar.set)
scrollbar.configure(command=book_listbox.yview)
book_listbox.bind('<<ListboxSelect>>', show_info)
# 创建按钮
add_button = Button(root, text="添加图书", command=add_book)
add_button.grid(row=4, column=4)
delete_button = Button(root, text="删除图书", command=delete_book)
delete_button.grid(row=5, column=4)
update_button = Button(root, text="更新图书", command=update_book)
update_button.grid(row=6, column=4)
list_button = Button(root, text="显示所有图书", command=show_list)
list_button.grid(row=7, column=4)
# 运行图书管理系统
root.mainloop()
# 关闭数据库连接
conn.close()
```
这个图书管理系统具有添加、删除、更新、搜索和显示所有图书的功能,并且具有图形用户界面。它使用SQLite作为数据库,并使用Tkinter作为GUI库来创建用户界面。您可以根据自己的需要修改和扩展它。