Library Management System using Python

In this project, we will create a Library Management System that helps manage book records, including the ability to add, update, delete, and search for books in a library. We will use Python for the backend logic and Tkinter for the graphical user interface (GUI). The project will also incorporate an SQLite database to store book records.

1. Project Setup

Modules Required:

  • tkinter (for the GUI)
  • sqlite3 (for database management)

To install necessary modules, you can use the following:

pip install tkinter

2. Project Features

  1. Add Book: Add new books with details such as Title, Author, ISBN, and Quantity.
  2. Update Book: Edit the details of an existing book.
  3. Delete Book: Remove a book record from the system.
  4. View All Books: Display all books in the library.
  5. Search for a Book: Search for a book by Title or ISBN.
  6. Issue/Return Book: Track the availability of books and manage borrowing and returning records.

3. Database Design

We’ll use SQLite to create a books table that stores book details:

  • id (INTEGER PRIMARY KEY AUTOINCREMENT)
  • title (TEXT)
  • author (TEXT)
  • isbn (TEXT)
  • quantity (INTEGER)

4. Code Structure

A. Database Connection

import sqlite3

def connect_db():
    conn = sqlite3.connect('library_management.db')
    c = conn.cursor()
    c.execute('''CREATE TABLE IF NOT EXISTS books
                 (id INTEGER PRIMARY KEY AUTOINCREMENT,
                  title TEXT,
                  author TEXT,
                  isbn TEXT,
                  quantity INTEGER)''')
    conn.commit()
    conn.close()

connect_db()

B. Add Book Function

def add_book(title, author, isbn, quantity):
    conn = sqlite3.connect('library_management.db')
    c = conn.cursor()
    c.execute("INSERT INTO books (title, author, isbn, quantity) VALUES (?, ?, ?, ?)",
              (title, author, isbn, quantity))
    conn.commit()
    conn.close()

C. View Books Function

def view_books():
    conn = sqlite3.connect('library_management.db')
    c = conn.cursor()
    c.execute("SELECT * FROM books")
    rows = c.fetchall()
    conn.close()
    return rows

D. Update Book Function

def update_book(id, title, author, isbn, quantity):
    conn = sqlite3.connect('library_management.db')
    c = conn.cursor()
    c.execute("UPDATE books SET title=?, author=?, isbn=?, quantity=? WHERE id=?",
              (title, author, isbn, quantity, id))
    conn.commit()
    conn.close()

E. Delete Book Function

def delete_book(id):
    conn = sqlite3.connect('library_management.db')
    c = conn.cursor()
    c.execute("DELETE FROM books WHERE id=?", (id,))
    conn.commit()
    conn.close()

5. GUI Design using Tkinter

Here’s a simple implementation of the GUI part using Tkinter:

from tkinter import *
from tkinter import messagebox
import sqlite3

# Main window setup
root = Tk()
root.title("Library Management System")
root.geometry("600x400")

# Function to Add Book from GUI
def add_book_gui():
    title = title_entry.get()
    author = author_entry.get()
    isbn = isbn_entry.get()
    quantity = quantity_entry.get()

    if title and author and isbn and quantity:
        add_book(title, author, isbn, int(quantity))
        messagebox.showinfo("Success", "Book added successfully!")
    else:
        messagebox.showerror("Error", "Please fill in all fields!")

# GUI Elements
Label(root, text="Title").grid(row=0, column=0, padx=20, pady=10)
title_entry = Entry(root)
title_entry.grid(row=0, column=1)

Label(root, text="Author").grid(row=1, column=0, padx=20, pady=10)
author_entry = Entry(root)
author_entry.grid(row=1, column=1)

Label(root, text="ISBN").grid(row=2, column=0, padx=20, pady=10)
isbn_entry = Entry(root)
isbn_entry.grid(row=2, column=1)

Label(root, text="Quantity").grid(row=3, column=0, padx=20, pady=10)
quantity_entry = Entry(root)
quantity_entry.grid(row=3, column=1)

Button(root, text="Add Book", command=add_book_gui).grid(row=4, column=0, columnspan=2, pady=20)

root.mainloop()

6. Final Enhancements

To make the Library Management System more comprehensive, you can add the following features:

  • Search Books by Title or ISBN: Add a search bar to find specific books by their title or ISBN.
  • Book Issuing/Returning: Add functionality to issue a book and track its availability.
  • Display Available Books: Display only the books that are currently available in the library.
  • User Interface Improvements: Enhance the user experience by organizing the interface with frames and buttons.

7. Conclusion

This is a simple Library Management System built using Python with Tkinter for the GUI and SQLite for the database. It covers essential operations like adding, viewing, updating, and deleting books, as well as providing a clean interface for managing library data.

Would you like to explore any specific feature or extend this project further?

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *