SOURCE CODE
Import tkinter as tk
From tkinter import messagebox, ttk
Import [Link] as sqlctr
From datetime import datetime, timedelta
# Connect to MySQL Database
Def create_connection():
Return [Link](host=’localhost’, user=’root’, password=’1234’,
database=’pathsala’)
# Insert a new book
Def insert_book():
Def submit_book():
Book_name = book_name_entry.get()
Quantity = int(quantity_entry.get())
Try:
Conn = create_connection()
Cursor = [Link]()
[Link](“INSERT INTO books (book_name, quantity_available) VALUES
(%s, %s)”,
(book_name, quantity))
[Link]()
[Link](“Success”, “Book added successfully”)
[Link]()
Except Exception as e:
[Link](“Error”, f”Failed to insert book: {str€}”)
# Create the insert book window
Insert_window = [Link]()
Insert_window.title(“Insert New Book”)
[Link](insert_window, text=”Book Name”).pack()
Book_name_entry = [Link](insert_window, width=80) # Increased size
Book_name_entry.pack()
[Link](insert_window, text=”Quantity Available”).pack()
Quantity_entry = [Link](insert_window, width=80) # Increased size
Quantity_entry.pack()
Submit_button = [Link](insert_window, text=”Add Book”,
command=submit_book)
Submit_button.pack()
# View all books
Def view_books():
Try:
Conn = create_connection()
Cursor = [Link]()
[Link](“SELECT * FROM books”)
Data = [Link]()
[Link]()
If data:
Books_window = [Link]()
Books_window.title(“All Books”)
# Create a Treeview table for displaying books details
Tree = [Link](books_window, columns=(“Book ID”, “Book Name”,
“Quantity Available”, “Date of Return”),
Show=”headings”, height=20)
# Define column headings
[Link](“Book ID”, text=”Book ID”)
[Link](“Book Name”, text=”Book Name”)
[Link](“Quantity Available”, text=”Quantity Available”)
[Link](“Date of Return”, text=”Date of Return”)
# Define column widths
[Link](“Book ID”, width=100)
[Link](“Book Name”, width=250)
[Link](“Quantity Available”, width=200)
[Link](“Date of Return”, width=200)
# Insert data into the table
For row in data:
[Link](“”, “end”, values=row) # Insert book data
# Add some padding between rows
Style = [Link]()
[Link](“Treeview”, rowheight=40) # Increased row height for better
spacing between rows
[Link](padx=10, pady=10)
Else:
[Link](“No Data”, “No books found in the system.”)
Except Exception as e:
[Link](“Error”, f”Failed to fetch books: {str€}”)
# Lend a book
Def lend_book():
Def lend():
Borrower_name = borrower_name_entry.get()
Contact = contact_entry.get()
Book_name = book_name_entry.get()
Lend_date = [Link]().strftime(“%Y-%m-%d %H:%M:%S”)
# Calculate the return date (for example, 7 days after the lend date)
Return_date = ([Link]() + timedelta(days=7)).strftime(“%Y-%m-%d %H:
%M:%S”)
Try:
Conn = create_connection()
Cursor = [Link]()
[Link](“SELECT quantity_available FROM books WHERE book_name =
%s”, (book_name,))
Data = [Link]()
If data and data[0] > 0:
[Link](“INSERT INTO borrower (borrowers_name, book_lent,
contact_no, lend_date, return_date) VALUES (%s, %s, %s, %s, %s)”,
(borrower_name, book_name, contact, lend_date, return_date))
[Link](“UPDATE books SET quantity_available = quantity_available
– 1 WHERE book_name = %s”, (book_name,))
[Link]()
[Link](“Success”, “Book lent successfully”)
Else:
[Link](“Out of Stock”, “Sorry, this book is currently
unavailable.”)
[Link]()
Except Exception as e:
[Link](“Error”, f”Failed to lend book: {str€}”)
# Create the lend book window
Lend_window = [Link]()
Lend_window.title(“Lend Book”)
[Link](lend_window, text=”Borrower’s Name”).pack()
Borrower_name_entry = [Link](lend_window, width=100) # Increased size
Borrower_name_entry.pack()
[Link](lend_window, text=”Contact No”).pack()
Contact_entry = [Link](lend_window, width=100) # Increased size
Contact_entry.pack()
[Link](lend_window, text=”Book Name”).pack()
Book_name_entry = [Link](lend_window, width=100) # Increased size
Book_name_entry.pack()
Lend_button = [Link](lend_window, text=”Lend Book”, command=lend)
Lend_button.pack()
# View lent book details
Def view_lent_books():
Try:
Conn = create_connection()
Cursor = [Link]()
# Query to fetch lent books and their details
[Link](“SELECT borrowers_name, book_lent, contact_no, lend_date,
return_date FROM borrower”)
Data = [Link]()
[Link]()
If data:
Lent_books_window = [Link]()
Lent_books_window.title(“Lent Books Details”)
# Create a Treeview table for displaying lent books details
Tree = [Link](lent_books_window, columns=(“Borrower Name”, “Book
Lent”, “Contact No”, “Lend Date”, “Return Date”),
Show=”headings”, height=20)
# Define column headings
[Link](“Borrower Name”, text=”Borrower Name”)
[Link](“Book Lent”, text=”Book Lent”)
[Link](“Contact No”, text=”Contact No”)
[Link](“Lend Date”, text=”Lend Date”)
[Link](“Return Date”, text=”Return Date”)
# Define column widths
[Link](“Borrower Name”, width=200)
[Link](“Book Lent”, width=250)
[Link](“Contact No”, width=150)
[Link](“Lend Date”, width=200)
[Link](“Return Date”, width=200)
# Insert data into the table
For row in data:
[Link](“”, “end”, values=row) # Insert lent book data
# Add some padding between rows
Style = [Link]()
[Link](“Treeview”, rowheight=40) # Increased row height for better
spacing between rows
[Link](padx=10, pady=10)
Else:
[Link](“No Data”, “No lent books found in the system.”)
Except Exception as e:
[Link](“Error”, f”Failed to fetch lent book details: {str€}”)
# Main Menu
Def main_menu():
Root = [Link]()
[Link](“Library Management System”)
# Set the geometry to increase the height of the main window
[Link](“800x500”) # Set the width and height of the window
# School name label with “Old English” font
School_label = [Link](root, text=”Montfort School Libarary”, font=(“Bookman Old
Style”, 30), padx=10, pady=10)
School_label.pack()
# Add Book Button
Add_book_button = [Link](root, text=”Add New Book”, font=(“Times New
Roman”, 20), padx=10, pady=10,width=120, command=insert_book) # Increased
width
Add_book_button.pack(pady=10)
# View Books Button
View_books_button = [Link](root, text=”View All Books”,font=(“Times New
Roman”, 20), padx=10, pady=10, width=120, command=view_books) # Increased
width
View_books_button.pack(pady=10)
# View Lent Books Button
View_lent_books_button = [Link](root, text=”View Lent Books”, font=(“Times
New Roman”, 20), padx=10, pady=10,width=120, command=view_lent_books) #
New button
View_lent_books_button.pack(pady=10)
# Lend Book Button
Lend_book_button = [Link](root, text=”Lend a Book”, font=(“Times New
Roman”, 20), padx=10, pady=10,width=120, command=lend_book) # Increased
width
Lend_book_button.pack(pady=10)
[Link]()
# Run the application
If __name__ == “__main__”:
Main_menu()