用python写一个中学图书馆管理系统,,实现系统基本的查询、插入、删除、修改功能,要求有数据库,有图形化gui界面给出代码,
时间: 2024-03-09 21:45:47 浏览: 25
好的,以下是一个简单的中学图书馆管理系统的Python代码,使用了SQLite数据库和Tkinter GUI库:
```python
import sqlite3
import tkinter as tk
from tkinter import messagebox
# 连接数据库
conn = sqlite3.connect('library.db')
cursor = conn.cursor()
# 创建图书馆表
cursor.execute('''CREATE TABLE IF NOT EXISTS library
(id INTEGER PRIMARY KEY AUTOINCREMENT,
book_name TEXT NOT NULL,
author TEXT NOT NULL,
publisher TEXT NOT NULL,
publish_year INTEGER NOT NULL,
price REAL NOT NULL,
quantity INTEGER NOT NULL)''')
# 创建GUI界面
window = tk.Tk()
window.title("中学图书馆管理系统")
# 创建标签和输入框
label_book_name = tk.Label(window, text="书名:")
label_book_name.grid(row=0, column=0)
entry_book_name = tk.Entry(window)
entry_book_name.grid(row=0, column=1)
label_author = tk.Label(window, text="作者:")
label_author.grid(row=1, column=0)
entry_author = tk.Entry(window)
entry_author.grid(row=1, column=1)
label_publisher = tk.Label(window, text="出版社:")
label_publisher.grid(row=2, column=0)
entry_publisher = tk.Entry(window)
entry_publisher.grid(row=2, column=1)
label_publish_year = tk.Label(window, text="出版年份:")
label_publish_year.grid(row=3, column=0)
entry_publish_year = tk.Entry(window)
entry_publish_year.grid(row=3, column=1)
label_price = tk.Label(window, text="价格:")
label_price.grid(row=4, column=0)
entry_price = tk.Entry(window)
entry_price.grid(row=4, column=1)
label_quantity = tk.Label(window, text="数量:")
label_quantity.grid(row=5, column=0)
entry_quantity = tk.Entry(window)
entry_quantity.grid(row=5, column=1)
# 创建按钮和事件处理函数
def search_book():
# 查询图书信息
book_name = entry_book_name.get()
author = entry_author.get()
publisher = entry_publisher.get()
publish_year = entry_publish_year.get()
price = entry_price.get()
quantity = entry_quantity.get()
if not any([book_name, author, publisher, publish_year, price, quantity]):
# 如果所有字段都为空,则查询所有图书信息
cursor.execute("SELECT * FROM library")
books = cursor.fetchall()
else:
# 否则根据输入的字段查询图书信息
query = "SELECT * FROM library WHERE "
conditions = []
if book_name:
conditions.append("book_name LIKE '%{}%'".format(book_name))
if author:
conditions.append("author LIKE '%{}%'".format(author))
if publisher:
conditions.append("publisher LIKE '%{}%'".format(publisher))
if publish_year:
conditions.append("publish_year = {}".format(publish_year))
if price:
conditions.append("price = {}".format(price))
if quantity:
conditions.append("quantity = {}".format(quantity))
query += " AND ".join(conditions)
cursor.execute(query)
books = cursor.fetchall()
# 显示查询结果
result_window = tk.Toplevel(window)
result_window.title("查询结果")
if not books:
tk.Label(result_window, text="没有找到符合条件的图书!").pack()
else:
tk.Label(result_window, text="查询结果:").pack()
for book in books:
tk.Label(result_window, text="书名:{}".format(book[1])).pack()
tk.Label(result_window, text="作者:{}".format(book[2])).pack()
tk.Label(result_window, text="出版社:{}".format(book[3])).pack()
tk.Label(result_window, text="出版年份:{}".format(book[4])).pack()
tk.Label(result_window, text="价格:{}".format(book[5])).pack()
tk.Label(result_window, text="数量:{}".format(book[6])).pack()
def add_book():
# 添加图书信息
book_name = entry_book_name.get()
author = entry_author.get()
publisher = entry_publisher.get()
publish_year = entry_publish_year.get()
price = entry_price.get()
quantity = entry_quantity.get()
if not all([book_name, author, publisher, publish_year, price, quantity]):
messagebox.showwarning("错误", "请填写所有字段!")
return
cursor.execute("INSERT INTO library(book_name, author, publisher, publish_year, price, quantity) VALUES (?, ?, ?, ?, ?, ?)",
(book_name, author, publisher, publish_year, price, quantity))
conn.commit()
messagebox.showinfo("成功", "添加图书信息成功!")
entry_book_name.delete(0, tk.END)
entry_author.delete(0, tk.END)
entry_publisher.delete(0, tk.END)
entry_publish_year.delete(0, tk.END)
entry_price.delete(0, tk.END)
entry_quantity.delete(0, tk.END)
def delete_book():
# 删除图书信息
book_name = entry_book_name.get()
author = entry_author.get()
if not any([book_name, author]):
messagebox.showwarning("错误", "请填写书名或作者!")
return
query = "DELETE FROM library WHERE "
conditions = []
if book_name:
conditions.append("book_name LIKE '%{}%'".format(book_name))
if author:
conditions.append("author LIKE '%{}%'".format(author))
query += " AND ".join(conditions)
cursor.execute(query)
conn.commit()
messagebox.showinfo("成功", "删除图书信息成功!")
entry_book_name.delete(0, tk.END)
entry_author.delete(0, tk.END)
def update_book():
# 修改图书信息
book_name = entry_book_name.get()
author = entry_author.get()
publisher = entry_publisher.get()
publish_year = entry_publish_year.get()
price = entry_price.get()
quantity = entry_quantity.get()
if not all([book_name, author, publisher, publish_year, price, quantity]):
messagebox.showwarning("错误", "请填写所有字段!")
return
cursor.execute("UPDATE library SET publisher = ?, publish_year = ?, price = ?, quantity = ? WHERE book_name = ? AND author = ?",
(publisher, publish_year, price, quantity, book_name, author))
conn.commit()
messagebox.showinfo("成功", "修改图书信息成功!")
entry_book_name.delete(0, tk.END)
entry_author.delete(0, tk.END)
entry_publisher.delete(0, tk.END)
entry_publish_year.delete(0, tk.END)
entry_price.delete(0, tk.END)
entry_quantity.delete(0, tk.END)
# 创建按钮
btn_search = tk.Button(window, text="查询", command=search_book)
btn_search.grid(row=6, column=0)
btn_add = tk.Button(window, text="添加", command=add_book)
btn_add.grid(row=6, column=1)
btn_delete = tk.Button(window, text="删除", command=delete_book)
btn_delete.grid(row=6, column=2)
btn_update = tk.Button(window, text="修改", command=update_book)
btn_update.grid(row=6, column=3)
# 运行界面
window.mainloop()
# 关闭数据库连接
conn.close()
```
这个程序中,我们使用了SQLite数据库来存储图书信息。在程序开始时,我们创建了一个名为“library.db”的数据库文件,并创建了一个名为“library”的表格来存储图书信息。每个图书包括书名、作者、出版社、出版年份、价格和数量等信息。程序的GUI界面使用了Tkinter库来创建,包括标签、输入框和按钮等控件。程序中的事件处理函数实现了基本的查询、插入、删除和修改功能,可以根据用户输入的条件从数据库中查询符合条件的图书信息,并可以添加、删除和修改图书信息。运行程序后,用户可以通过界面上的输入框和按钮来操作图书馆管理系统。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)