import tkinter as tk
from tkinter import messagebox
class Application(tk.Frame):
def __init__(self, library, master=None):
super().__init__(master)
self.library = library
self.grid()
self.create_widgets()
def create_widgets(self):
self.book_label = tk.Label(self, text="Book Title")
self.book_label.grid(row=0, column=0)
self.book_entry = tk.Entry(self)
self.book_entry.grid(row=0, column=1)
self.user_label = tk.Label(self, text="User Name")
self.user_label.grid(row=1, column=0)
self.user_entry = tk.Entry(self)
self.user_entry.grid(row=1, column=1)
self.borrow_button = tk.Button(self)
self.borrow_button["text"] = "Borrow Book"
self.borrow_button["command"] = self.borrow_book
self.borrow_button.grid(row=2, column=0)
self.return_button = tk.Button(self)
self.return_button["text"] = "Return Book"
self.return_button["command"] = self.return_book
self.return_button.grid(row=2, column=1)
def borrow_book(self):
book_title = self.book_entry.get()
user_name = self.user_entry.get()
book = self.library.find_book(book_title)
user = self.library.find_user(user_name)
if book and user and not book.is_borrowed():
user.borrow_book(book)
messagebox.showinfo("Success", f"{user_name} borrowed {book_title}")
else:
messagebox.showerror("Error", "Cannot borrow book")
def return_book(self):
book_title = self.book_entry.get()
user_name = self.user_entry.get()
book = self.library.find_book(book_title)
user = self.library.find_user(user_name)
if book and user and book.is_borrowed():
user.return_book(book)
messagebox.showinfo("Success", f"{user_name} returned {book_title}")
else:
messagebox.showerror("Error", "Cannot return book")
# 使用示例
library = Library()
book1 = Book('Book Title 1', 'Author 1', 'ISBN 1')
book2 = Book('Book Title 2', 'Author 2', 'ISBN 2')
user1 = User('User 1')
user2 = User('User 2')
library.add_book(book1)
library.add_book(book2)
library.add_user(user1)
library.add_user(user2)
app = Application(library)
app.mainloop()
标签:return,python,self,library,book,user,借阅,图书,tk
From: https://blog.51cto.com/u_16055028/8256346