python的tinker编写图书管理系统简单程序
时间: 2024-02-17 09:01:29 浏览: 128
好的,下面是一个简单的图书管理系统程序示例,使用 Python 的 Tkinter 模块进行编写:
```python
from tkinter import *
import tkinter.messagebox as messagebox
class Book:
def __init__(self, name, author, category):
self.name = name
self.author = author
self.category = category
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, name):
for book in self.books:
if book.name == name:
return book
return None
class GUI:
def __init__(self, library):
self.library = library
self.window = Tk()
self.window.title("图书管理系统")
self.window.geometry("400x300")
self.name_label = Label(self.window, text="书名:")
self.name_label.grid(row=0, column=0)
self.name_entry = Entry(self.window)
self.name_entry.grid(row=0, column=1)
self.author_label = Label(self.window, text="作者:")
self.author_label.grid(row=1, column=0)
self.author_entry = Entry(self.window)
self.author_entry.grid(row=1, column=1)
self.category_label = Label(self.window, text="分类:")
self.category_label.grid(row=2, column=0)
self.category_entry = Entry(self.window)
self.category_entry.grid(row=2, column=1)
self.add_button = Button(self.window, text="添加", command=self.add_book)
self.add_button.grid(row=3, column=0)
self.remove_button = Button(self.window, text="删除", command=self.remove_book)
self.remove_button.grid(row=3, column=1)
self.search_button = Button(self.window, text="搜索", command=self.search_book)
self.search_button.grid(row=3, column=2)
self.result_label = Label(self.window, text="")
self.result_label.grid(row=4, column=0, columnspan=3)
def add_book(self):
name = self.name_entry.get()
author = self.author_entry.get()
category = self.category_entry.get()
book = Book(name, author, category)
self.library.add_book(book)
self.result_label.config(text="添加成功")
def remove_book(self):
name = self.name_entry.get()
book = self.library.search_book(name)
if book:
self.library.remove_book(book)
self.result_label.config(text="删除成功")
else:
self.result_label.config(text="未找到该书")
def search_book(self):
name = self.name_entry.get()
book = self.library.search_book(name)
if book:
self.result_label.config(text="书名:{} 作者:{} 分类:{}".format(book.name, book.author, book.category))
else:
self.result_label.config(text="未找到该书")
def run(self):
self.window.mainloop()
library = Library()
gui = GUI(library)
gui.run()
```
这个程序实现了添加、删除和搜索图书的功能,使用了面向对象的编程方式,并使用了 Tkinter 模块进行界面设计。用户可以在界面中输入书名、作者和分类,并点击添加、删除和搜索按钮进行操作。程序会根据用户的操作,更新图书馆中的图书,并在界面中显示操作结果。
阅读全文