python实现图书管理系统(带gui)
时间: 2023-07-28 16:10:17 浏览: 92
好的,下面是一个简单的Python图书管理系统的GUI实现示例:
```python
import tkinter as tk
from tkinter import messagebox
class Book:
def __init__(self, title, author, isbn):
self.title = title
self.author = author
self.isbn = isbn
class Library:
def __init__(self):
self.books = []
self.load_books()
def load_books(self):
# 加载已有书籍
pass
def add_book(self, book):
# 添加书籍
pass
def remove_book(self, book):
# 删除书籍
pass
def search_book(self, keyword):
# 搜索书籍
pass
class Application(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.library = Library()
self.create_widgets()
def create_widgets(self):
self.master.title('图书管理系统')
# 标签
self.title_label = tk.Label(self.master, text='书名')
self.title_label.grid(row=0, column=0)
self.author_label = tk.Label(self.master, text='作者')
self.author_label.grid(row=1, column=0)
self.isbn_label = tk.Label(self.master, text='ISBN')
self.isbn_label.grid(row=2, column=0)
# 输入框
self.title_entry = tk.Entry(self.master)
self.title_entry.grid(row=0, column=1)
self.author_entry = tk.Entry(self.master)
self.author_entry.grid(row=1, column=1)
self.isbn_entry = tk.Entry(self.master)
self.isbn_entry.grid(row=2, column=1)
# 按钮
self.add_button = tk.Button(self.master, text='添加', command=self.add_book)
self.add_button.grid(row=3, column=0)
self.remove_button = tk.Button(self.master, text='删除', command=self.remove_book)
self.remove_button.grid(row=3, column=1)
self.search_button = tk.Button(self.master, text='搜索', command=self.search_book)
self.search_button.grid(row=3, column=2)
# 列表框
self.book_listbox = tk.Listbox(self.master, height=10, width=60)
self.book_listbox.grid(row=4, column=0, columnspan=3)
def add_book(self):
title = self.title_entry.get()
author = self.author_entry.get()
isbn = self.isbn_entry.get()
if title and author and isbn:
book = Book(title, author, isbn)
self.library.add_book(book)
self.book_listbox.insert(tk.END, book.title)
messagebox.showinfo('添加书籍', '添加成功!')
else:
messagebox.showerror('添加书籍', '书名、作者和ISBN不能为空!')
def remove_book(self):
selection = self.book_listbox.curselection()
if selection:
title = self.book_listbox.get(selection[0])
book = self.library.search_book(title)
self.library.remove_book(book)
self.book_listbox.delete(selection[0])
messagebox.showinfo('删除书籍', '删除成功!')
else:
messagebox.showerror('删除书籍', '请选择要删除的书籍!')
def search_book(self):
keyword = self.title_entry.get()
if keyword:
books = self.library.search_book(keyword)
self.book_listbox.delete(0, tk.END)
for book in books:
self.book_listbox.insert(tk.END, book.title)
else:
messagebox.showerror('搜索书籍', '请输入要搜索的关键词!')
root = tk.Tk()
app = Application(master=root)
app.mainloop()
```
上述代码实现了一个带有GUI界面的图书管理系统。其中,Book和Library类用于表示图书和图书馆,Application类则是GUI程序的主要逻辑实现。在GUI界面中,用户可以输入书名、作者和ISBN,并通过添加、删除和搜索按钮来进行相关操作。同时,程序还会将书籍列表显示在列表框中,方便用户查看已有书籍。
阅读全文