用python+tkinter+mysql写一个图书管理系统
时间: 2023-12-31 11:05:13 浏览: 150
基于python语言和tkinter库语言的图书管理系统
好的,下面是一个简单的图书管理系统的实现:
首先,我们需要安装 MySQL 和 Python 的 MySQL 连接库 `mysql-connector-python`。可以通过以下命令进行安装:
```
# 安装 MySQL
sudo apt-get update
sudo apt-get install mysql-server
# 安装 mysql-connector-python 库
pip install mysql-connector-python
```
接下来,我们需要创建一个名为 `book_management` 的数据库,并创建一个名为 `books` 的数据表,用于存储图书信息。数据表中包含以下字段:`id`、`title`、`author`、`isbn`、`publisher`、`price` 和 `quantity`。
可以通过以下 SQL 语句进行创建:
```
CREATE DATABASE book_management;
USE book_management;
CREATE TABLE books (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255),
author VARCHAR(255),
isbn VARCHAR(255),
publisher VARCHAR(255),
price FLOAT,
quantity INT
);
```
接下来,我们可以开始编写 Python 代码。我们将使用 Python 的 tkinter 库来创建一个简单的 GUI,用于添加、删除、更新和查询图书信息。
以下是完整的代码:
```python
import mysql.connector
from tkinter import *
from tkinter import messagebox
# 连接 MySQL 数据库
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="yourpassword",
database="book_management"
)
# 创建游标
mycursor = mydb.cursor()
# 创建窗口
window = Tk()
window.title("图书管理系统")
window.geometry("500x300")
# 添加图书
def add_book():
# 获取输入的图书信息
title = title_entry.get()
author = author_entry.get()
isbn = isbn_entry.get()
publisher = publisher_entry.get()
price = price_entry.get()
quantity = quantity_entry.get()
# 检查输入是否为空
if not all([title, author, isbn, publisher, price, quantity]):
messagebox.showwarning("警告", "请填写完整信息!")
return
# 插入数据到数据库
sql = "INSERT INTO books (title, author, isbn, publisher, price, quantity) VALUES (%s, %s, %s, %s, %s, %s)"
val = (title, author, isbn, publisher, price, quantity)
mycursor.execute(sql, val)
mydb.commit()
messagebox.showinfo("提示", "添加成功!")
clear_entries()
# 删除图书
def delete_book():
# 获取输入的图书编号
book_id = book_id_entry.get()
# 检查输入是否为空
if not book_id:
messagebox.showwarning("警告", "请输入图书编号!")
return
# 从数据库中删除数据
sql = "DELETE FROM books WHERE id = %s"
val = (book_id,)
mycursor.execute(sql, val)
mydb.commit()
if mycursor.rowcount == 0:
messagebox.showwarning("警告", "未找到该图书!")
else:
messagebox.showinfo("提示", "删除成功!")
clear_entries()
# 更新图书
def update_book():
# 获取输入的图书信息和编号
title = title_entry.get()
author = author_entry.get()
isbn = isbn_entry.get()
publisher = publisher_entry.get()
price = price_entry.get()
quantity = quantity_entry.get()
book_id = book_id_entry.get()
# 检查输入是否为空
if not all([title, author, isbn, publisher, price, quantity, book_id]):
messagebox.showwarning("警告", "请填写完整信息!")
return
# 更新数据库中的数据
sql = "UPDATE books SET title = %s, author = %s, isbn = %s, publisher = %s, price = %s, quantity = %s WHERE id = %s"
val = (title, author, isbn, publisher, price, quantity, book_id)
mycursor.execute(sql, val)
mydb.commit()
if mycursor.rowcount == 0:
messagebox.showwarning("警告", "未找到该图书!")
else:
messagebox.showinfo("提示", "更新成功!")
clear_entries()
# 查询图书
def search_book():
# 获取输入的图书编号
book_id = book_id_entry.get()
# 检查输入是否为空
if not book_id:
messagebox.showwarning("警告", "请输入图书编号!")
return
# 从数据库中查询数据
sql = "SELECT * FROM books WHERE id = %s"
val = (book_id,)
mycursor.execute(sql, val)
result = mycursor.fetchone()
if result is None:
messagebox.showwarning("警告", "未找到该图书!")
clear_entries()
else:
title_entry.delete(0, END)
title_entry.insert(0, result[1])
author_entry.delete(0, END)
author_entry.insert(0, result[2])
isbn_entry.delete(0, END)
isbn_entry.insert(0, result[3])
publisher_entry.delete(0, END)
publisher_entry.insert(0, result[4])
price_entry.delete(0, END)
price_entry.insert(0, result[5])
quantity_entry.delete(0, END)
quantity_entry.insert(0, result[6])
# 清空输入框
def clear_entries():
book_id_entry.delete(0, END)
title_entry.delete(0, END)
author_entry.delete(0, END)
isbn_entry.delete(0, END)
publisher_entry.delete(0, END)
price_entry.delete(0, END)
quantity_entry.delete(0, END)
# 添加标签和输入框
book_id_label = Label(window, text="编号:")
book_id_label.grid(row=0, column=0, padx=5, pady=5)
book_id_entry = Entry(window)
book_id_entry.grid(row=0, column=1, padx=5, pady=5)
title_label = Label(window, text="书名:")
title_label.grid(row=1, column=0, padx=5, pady=5)
title_entry = Entry(window)
title_entry.grid(row=1, column=1, padx=5, pady=5)
author_label = Label(window, text="作者:")
author_label.grid(row=2, column=0, padx=5, pady=5)
author_entry = Entry(window)
author_entry.grid(row=2, column=1, padx=5, pady=5)
isbn_label = Label(window, text="ISBN:")
isbn_label.grid(row=3, column=0, padx=5, pady=5)
isbn_entry = Entry(window)
isbn_entry.grid(row=3, column=1, padx=5, pady=5)
publisher_label = Label(window, text="出版社:")
publisher_label.grid(row=4, column=0, padx=5, pady=5)
publisher_entry = Entry(window)
publisher_entry.grid(row=4, column=1, padx=5, pady=5)
price_label = Label(window, text="价格:")
price_label.grid(row=5, column=0, padx=5, pady=5)
price_entry = Entry(window)
price_entry.grid(row=5, column=1, padx=5, pady=5)
quantity_label = Label(window, text="数量:")
quantity_label.grid(row=6, column=0, padx=5, pady=5)
quantity_entry = Entry(window)
quantity_entry.grid(row=6, column=1, padx=5, pady=5)
# 添加按钮
add_button = Button(window, text="添加", command=add_book)
add_button.grid(row=7, column=0, padx=5, pady=5)
delete_button = Button(window, text="删除", command=delete_book)
delete_button.grid(row=7, column=1, padx=5, pady=5)
update_button = Button(window, text="更新", command=update_book)
update_button.grid(row=7, column=2, padx=5, pady=5)
search_button = Button(window, text="查询", command=search_book)
search_button.grid(row=0, column=2, padx=5, pady=5)
clear_button = Button(window, text="清空", command=clear_entries)
clear_button.grid(row=7, column=3, padx=5, pady=5)
# 主循环
window.mainloop()
```
在运行程序之前,请确保替换以下行中的 MySQL 用户名和密码:
```python
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="yourpassword",
database="book_management"
)
```
运行程序后,您应该会看到一个简单的 GUI 窗口,其中包含用于添加、删除、更新和查询图书信息的输入框和按钮。当您执行这些操作时,它们将与 MySQL 数据库进行交互,并将数据存储在 `books` 表中。
这只是一个简单的示例,您可以根据自己的需求进行修改和扩展。
阅读全文