python 图书借阅系统
  X5zJxoD00Cah 2023年11月19日 20 0
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()
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月19日 0

暂无评论

X5zJxoD00Cah