写一个pycharm图书管理系统代码,并实现弹窗
时间: 2024-03-10 09:47:39 浏览: 101
以下是一个使用Python Tkinter库实现简单图书管理系统的代码示例,其中包含弹窗的实现:
```python
from tkinter import *
from tkinter import messagebox
class Book:
def __init__(self, title, author, isbn, year):
self.title = title
self.author = author
self.isbn = isbn
self.year = year
class Library:
def __init__(self):
self.books = []
def add_book(self, book):
self.books.append(book)
def remove_book(self, book):
self.books.remove(book)
def search_book(self, title):
for book in self.books:
if book.title == title:
return book
def display_books(self):
book_list = []
for book in self.books:
book_list.append(f"{book.title} by {book.author} ({book.year})")
return "\n".join(book_list)
class GUI:
def __init__(self, library):
self.library = library
self.window = Tk()
self.window.title("图书管理系统")
self.window.geometry("400x300")
# 标题
self.title_label = Label(self.window, text="图书管理系统", font=("Arial", 20))
self.title_label.pack()
# 添加书籍
self.add_frame = Frame(self.window)
self.add_frame.pack(pady=10)
self.add_label = Label(self.add_frame, text="添加书籍", font=("Arial", 16))
self.add_label.pack(side=LEFT)
self.add_title_label = Label(self.add_frame, text="书名")
self.add_title_label.pack()
self.add_title_entry = Entry(self.add_frame)
self.add_title_entry.pack()
self.add_author_label = Label(self.add_frame, text="作者")
self.add_author_label.pack()
self.add_author_entry = Entry(self.add_frame)
self.add_author_entry.pack()
self.add_isbn_label = Label(self.add_frame, text="ISBN号")
self.add_isbn_label.pack()
self.add_isbn_entry = Entry(self.add_frame)
self.add_isbn_entry.pack()
self.add_year_label = Label(self.add_frame, text="出版年份")
self.add_year_label.pack()
self.add_year_entry = Entry(self.add_frame)
self.add_year_entry.pack()
self.add_button = Button(self.add_frame, text="添加", command=self.add_book)
self.add_button.pack()
# 删除书籍
self.remove_frame = Frame(self.window)
self.remove_frame.pack(pady=10)
self.remove_label = Label(self.remove_frame, text="删除书籍", font=("Arial", 16))
self.remove_label.pack(side=LEFT)
self.remove_title_label = Label(self.remove_frame, text="书名")
self.remove_title_label.pack()
self.remove_title_entry = Entry(self.remove_frame)
self.remove_title_entry.pack()
self.remove_button = Button(self.remove_frame, text="删除", command=self.remove_book)
self.remove_button.pack()
# 查找书籍
self.search_frame = Frame(self.window)
self.search_frame.pack(pady=10)
self.search_label = Label(self.search_frame, text="查找书籍", font=("Arial", 16))
self.search_label.pack(side=LEFT)
self.search_title_label = Label(self.search_frame, text="书名")
self.search_title_label.pack()
self.search_title_entry = Entry(self.search_frame)
self.search_title_entry.pack()
self.search_button = Button(self.search_frame, text="查找", command=self.search_book)
self.search_button.pack()
# 显示书籍
self.display_frame = Frame(self.window)
self.display_frame.pack(pady=10)
self.display_label = Label(self.display_frame, text="所有书籍", font=("Arial", 16))
self.display_label.pack(side=LEFT)
self.display_text = Text(self.display_frame, height=5, width=30)
self.display_text.pack()
self.display_button = Button(self.display_frame, text="显示", command=self.display_books)
self.display_button.pack()
def add_book(self):
title = self.add_title_entry.get()
author = self.add_author_entry.get()
isbn = self.add_isbn_entry.get()
year = self.add_year_entry.get()
if not title or not author or not isbn or not year:
messagebox.showerror("错误", "请填写所有字段")
return
book = Book(title, author, isbn, year)
self.library.add_book(book)
messagebox.showinfo("提示", "书籍添加成功")
self.add_title_entry.delete(0, END)
self.add_author_entry.delete(0, END)
self.add_isbn_entry.delete(0, END)
self.add_year_entry.delete(0, END)
def remove_book(self):
title = self.remove_title_entry.get()
if not title:
messagebox.showerror("错误", "请填写书名")
return
book = self.library.search_book(title)
if not book:
messagebox.showerror("错误", "未找到该书")
return
self.library.remove_book(book)
messagebox.showinfo("提示", "书籍删除成功")
self.remove_title_entry.delete(0, END)
def search_book(self):
title = self.search_title_entry.get()
if not title:
messagebox.showerror("错误", "请填写书名")
return
book = self.library.search_book(title)
if not book:
messagebox.showerror("错误", "未找到该书")
return
messagebox.showinfo("提示", f"{book.title} by {book.author} ({book.year})")
def display_books(self):
books = self.library.display_books()
self.display_text.delete("1.0", END)
self.display_text.insert(END, books)
def run(self):
self.window.mainloop()
# 测试
library = Library()
gui = GUI(library)
gui.run()
```
在这个示例中,我们创建了一个GUI类,其中包含添加、删除、查找和显示书籍的方法。每个方法都与一个Tkinter窗口小部件相关联,例如Button、Label和Entry。当用户单击按钮时,相关方法将被调用。如果用户未填写所有字段或未找到所需书籍,则会出现相应的消息框。在添加书籍和删除书籍方法中,如果成功执行操作,则会出现相应的消息框。在查找书籍方法中,如果找到所需书籍,则会出现相应的消息框,显示该书籍的信息。在显示所有书籍的方法中,将创建一个多行文本框,并将所有书籍的信息插入其中。最后,我们创建一个Library对象和GUI对象,并运行GUI。
阅读全文