0% found this document useful (0 votes)
9 views

Source Code

Uploaded by

Test Id
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Source Code

Uploaded by

Test Id
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

SOURCE CODE

Import tkinter as tk

From tkinter import messagebox, ttk


Import mysql.connector as sqlctr
From datetime import datetime, timedelta

# Connect to MySQL Database


Def create_connection():
Return sqlctr.connect(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 = conn.cursor()
Cursor.execute(“INSERT INTO books (book_name, quantity_available) VALUES
(%s, %s)”,
(book_name, quantity))
Conn.commit()
Messagebox.showinfo(“Success”, “Book added successfully”)
Conn.close()
Except Exception as e:
Messagebox.showerror(“Error”, f”Failed to insert book: {str€}”)

# Create the insert book window


Insert_window = tk.Toplevel()
Insert_window.title(“Insert New Book”)
Tk.Label(insert_window, text=”Book Name”).pack()
Book_name_entry = tk.Entry(insert_window, width=80) # Increased size
Book_name_entry.pack()

Tk.Label(insert_window, text=”Quantity Available”).pack()


Quantity_entry = tk.Entry(insert_window, width=80) # Increased size
Quantity_entry.pack()

Submit_button = tk.Button(insert_window, text=”Add Book”,


command=submit_book)
Submit_button.pack()

# View all books


Def view_books():
Try:
Conn = create_connection()
Cursor = conn.cursor()
Cursor.execute(“SELECT * FROM books”)
Data = cursor.fetchall()
Conn.close()

If data:
Books_window = tk.Toplevel()
Books_window.title(“All Books”)

# Create a Treeview table for displaying books details


Tree = ttk.Treeview(books_window, columns=(“Book ID”, “Book Name”,
“Quantity Available”, “Date of Return”),
Show=”headings”, height=20)

# Define column headings


Tree.heading(“Book ID”, text=”Book ID”)
Tree.heading(“Book Name”, text=”Book Name”)
Tree.heading(“Quantity Available”, text=”Quantity Available”)
Tree.heading(“Date of Return”, text=”Date of Return”)

# Define column widths


Tree.column(“Book ID”, width=100)
Tree.column(“Book Name”, width=250)
Tree.column(“Quantity Available”, width=200)
Tree.column(“Date of Return”, width=200)

# Insert data into the table


For row in data:
Tree.insert(“”, “end”, values=row) # Insert book data

# Add some padding between rows


Style = ttk.Style()
Style.configure(“Treeview”, rowheight=40) # Increased row height for better
spacing between rows

Tree.pack(padx=10, pady=10)
Else:
Messagebox.showinfo(“No Data”, “No books found in the system.”)
Except Exception as e:
Messagebox.showerror(“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 = datetime.now().strftime(“%Y-%m-%d %H:%M:%S”)

# Calculate the return date (for example, 7 days after the lend date)
Return_date = (datetime.now() + timedelta(days=7)).strftime(“%Y-%m-%d %H:
%M:%S”)

Try:
Conn = create_connection()
Cursor = conn.cursor()
Cursor.execute(“SELECT quantity_available FROM books WHERE book_name =
%s”, (book_name,))
Data = cursor.fetchone()

If data and data[0] > 0:


Cursor.execute(“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))
Cursor.execute(“UPDATE books SET quantity_available = quantity_available
– 1 WHERE book_name = %s”, (book_name,))
Conn.commit()
Messagebox.showinfo(“Success”, “Book lent successfully”)
Else:
Messagebox.showwarning(“Out of Stock”, “Sorry, this book is currently
unavailable.”)
Conn.close()
Except Exception as e:
Messagebox.showerror(“Error”, f”Failed to lend book: {str€}”)

# Create the lend book window


Lend_window = tk.Toplevel()
Lend_window.title(“Lend Book”)

Tk.Label(lend_window, text=”Borrower’s Name”).pack()


Borrower_name_entry = tk.Entry(lend_window, width=100) # Increased size
Borrower_name_entry.pack()

Tk.Label(lend_window, text=”Contact No”).pack()


Contact_entry = tk.Entry(lend_window, width=100) # Increased size
Contact_entry.pack()

Tk.Label(lend_window, text=”Book Name”).pack()


Book_name_entry = tk.Entry(lend_window, width=100) # Increased size
Book_name_entry.pack()

Lend_button = tk.Button(lend_window, text=”Lend Book”, command=lend)


Lend_button.pack()

# View lent book details


Def view_lent_books():
Try:
Conn = create_connection()
Cursor = conn.cursor()

# Query to fetch lent books and their details


Cursor.execute(“SELECT borrowers_name, book_lent, contact_no, lend_date,
return_date FROM borrower”)
Data = cursor.fetchall()
Conn.close()

If data:
Lent_books_window = tk.Toplevel()
Lent_books_window.title(“Lent Books Details”)

# Create a Treeview table for displaying lent books details


Tree = ttk.Treeview(lent_books_window, columns=(“Borrower Name”, “Book
Lent”, “Contact No”, “Lend Date”, “Return Date”),
Show=”headings”, height=20)

# Define column headings


Tree.heading(“Borrower Name”, text=”Borrower Name”)
Tree.heading(“Book Lent”, text=”Book Lent”)
Tree.heading(“Contact No”, text=”Contact No”)
Tree.heading(“Lend Date”, text=”Lend Date”)
Tree.heading(“Return Date”, text=”Return Date”)

# Define column widths


Tree.column(“Borrower Name”, width=200)
Tree.column(“Book Lent”, width=250)
Tree.column(“Contact No”, width=150)
Tree.column(“Lend Date”, width=200)
Tree.column(“Return Date”, width=200)

# Insert data into the table


For row in data:
Tree.insert(“”, “end”, values=row) # Insert lent book data

# Add some padding between rows


Style = ttk.Style()
Style.configure(“Treeview”, rowheight=40) # Increased row height for better
spacing between rows

Tree.pack(padx=10, pady=10)
Else:
Messagebox.showinfo(“No Data”, “No lent books found in the system.”)
Except Exception as e:
Messagebox.showerror(“Error”, f”Failed to fetch lent book details: {str€}”)

# Main Menu
Def main_menu():
Root = tk.Tk()
Root.title(“Library Management System”)
# Set the geometry to increase the height of the main window
Root.geometry(“800x500”) # Set the width and height of the window
# School name label with “Old English” font
School_label = tk.Label(root, text=”Montfort School Libarary”, font=(“Bookman Old
Style”, 30), padx=10, pady=10)
School_label.pack()

# Add Book Button


Add_book_button = tk.Button(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 = tk.Button(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 = tk.Button(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 = tk.Button(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)

Root.mainloop()

# Run the application


If __name__ == “__main__”:
Main_menu()

You might also like