Python Library Management System [project with source code]

FREE Online Courses: Transform Your Career – Enroll for Free!

In this article, we are going to build an advanced python project Library Management System using MySQL database and Tkinter for GUI (Graphical User interface).

Tkinter is Python’s standard GUI interface and it is easy to learn. Even if you don’t know anything about tkinter you can just learn it along with this project, we have used some basic widgets like Button, Label, and Entry, etc.

Let’s look at the packages required for this project.

Project Prerequisites

To install the required libraries for python library management system, you can use pip installer from the cmd/Terminal:

To install tkinter:

pip install tkinter

To install mysql connector:

pip install mysql-connector-python

Download Library Management System Python Code

Please download the source code of python library management system: Library Management System Python Code

Database Creation Steps:

In the library management project we will use MySQL database, please create the database using following commands:

create database db;
 
use db;
 
create table books(bid varchar(10) primary key,
  title varchar(50) Not null,
  author varchar(50) Not null,
  available varchar(5) Default ‘YES’,	
  );
 
create table issue(bid varchar(10) primary key,
 studentName varchar(50) Not null,
 foreign key(bid) references books(bid)
 );
 
desc books;
desc issue;

Output

books table

issue table

We have created two tables for library management project:

1. Books:

  • Bid: Book id, primary key
  • Title: Book title
  • Author: Book’s Author
  • Available: Determines book availability

2. Issue:

  • Bid: Book id, foreign+ primary key
  • studentName: Student’s name

Create Library Management System Python Files

1. Home.py

from tkinter import *
import mysql.connector
 
from add import *
from delete import *
from issue import * 
from Return import *
from view import *
 
db = mysql.connector.connect(host ="localhost",user = "root",password = 'Your password',database='db')
cursor = db.cursor()
 
window=Tk()
window.title("ProjectGurukul Library Management System")
 
greet = Label(window, font = ('arial', 30, 'bold'), text = "Welcome to ProjectGurukul!")
greet.grid(row = 0,columnspan = 3)
 
addbtn=Button(window,text="Add Books",command=addBooks,bg="DodgerBlue2",fg="white",font = ('arial', 20, 'bold'))
addbtn.grid(row=3,columnspan=3)
 
deletebtn=Button(window,text="Delete Books",command=deleteBooks,bg="DodgerBlue2",fg="white",font = ('arial', 20, 'bold'))
deletebtn.grid(row=5,columnspan=3)
 
issuebtn=Button(window,text="Issue Books",command=issueBooks,bg="DodgerBlue2",fg="white",font = ('arial', 20, 'bold'))
issuebtn.grid(row=7,columnspan=3)
 
returnbtn=Button(window,text="Return Books",command=returnBooks,bg="DodgerBlue2",fg="white",font = ('arial', 20, 'bold'))
returnbtn.grid(row=9,columnspan=3)
 
viewbtn=Button(window,text="View Books",command=viewBooks,bg="DodgerBlue2",fg="white",font = ('arial', 20, 'bold'))
viewbtn.grid(row=11,columnspan=3)
 
greet = Label(window, font = ('arial', 15, 'bold'), text = "Thank you")
greet.grid(row = 13,columnspan = 3)
 
window.mainloop()

Explanation

  • We have imported all the python modules in this file so that it can function as per user requirement.
  • We have also established a connection with mysql database using mysql connector. Make sure you provide your own mysql username and password while connecting with mysql in every file.
  • It displays all the functionalities and enables the user to perform any task according to the requirement. We have used some basic tkinter widgets like Button (to call different functions) and Label (to display the test on the window)

library management project home

2. add.py

from tkinter import *
from tkinter import messagebox
import mysql.connector
 
def add_db():
 
    global id
    global title
    global author
 
    bid=id.get()
    btitle=title.get()
    bauthor=author.get()
    
    db = mysql.connector.connect(host ="localhost",user = "root",password = 'your password',database='db')
    cursor = db.cursor()
    
    print(bid,end='--')
    print(btitle,end='--')
    print(bauthor,end='--')
    print("add")
 
    sqlquery= "insert into books values('" + bid +"','"+btitle+"','"+bauthor+"','YES');"
    print(sqlquery)
 
    try:
        cursor.execute(sqlquery)
        db.commit()
        messagebox.showinfo('Success',"Book added Successfully")
    except:
        messagebox.showinfo("Error","Cannot add given book data into Database")
    
    window.destroy()
 
def addBooks():
 
    global id
    global title
    global author
 
    window=Tk()
    window.title('ProjectGurukul Library Management System')
 
    greet = Label(window, font = ('arial', 30, 'bold'), text = "Add Books")
    greet.grid(row = 0,columnspan = 3)
 
    #----------id-------------------
 
    L = Label(window, font = ('arial', 15, 'bold'), text = "Enter Book id: ")
    L.grid(row = 2, column = 1)
 
    L = Label(window, font = ('arial', 15, 'bold'), text = "   ")
    L.grid(row = 2, column = 2)
 
    id=Entry(window,width=5,font =('arial', 15, 'bold'))
    id.grid(row=2,column=3)
 
    #----------title-------------------
 
    L = Label(window, font = ('arial', 15, 'bold'), text = "Enter Title: ")
    L.grid(row = 4, column = 1)
 
    L = Label(window, font = ('arial', 15, 'bold'), text = "   ")
    L.grid(row = 4, column = 2)
 
    title=Entry(window,width=5,font =('arial', 15, 'bold'))
    title.grid(row=4,column=3)
 
    #----------author-------------------
 
    L = Label(window, font = ('arial', 15, 'bold'), text = "Enter Author: ")
    L.grid(row = 6, column = 1)
 
    L = Label(window, font = ('arial', 15, 'bold'), text = "   ")
    L.grid(row = 6, column = 2)
 
    author=Entry(window,width=5,font =('arial', 15, 'bold'))
    author.grid(row=6,column=3)
    
    submitbtn=Button(window,text="Submit",command=add_db,bg="DodgerBlue2",fg="white",font = ('arial', 15, 'bold'))
    submitbtn.grid(row=8,columnspan=3)
        
    print("add")
    pass

Explanation:

  • Home.py will call addBooks() function when the user wants to add a book in the library management system.
  • AddBooks function asks the user to enter the book information (id, title, and author) and it also offers a submit button.
  • Submit button calls add_db() function which adds the provided information in the database.
  • Cursor executes the query and then we commit(save) the changes in the database.

Add Books Page:

library add books

3. delete.py

from tkinter import *
from tkinter import messagebox
import mysql.connector
 
def delete_db():
 
    global id
 
    bid=id.get()
    
    db = mysql.connector.connect(host ="localhost",user = "root",password = 'your password',database='db')
    cursor = db.cursor()
    
    print(bid,end='--')
    print("delete")
 
    sqlquery= "delete from books where bid='"+bid+"';"
    print(sqlquery)
 
    try:
        cursor.execute(sqlquery)
        db.commit()
        messagebox.showinfo('Success',"Book deleted Successfully")
    except:
        messagebox.showinfo("Error","Book with given id does not exist")
    
    window.destroy()
 
def deleteBooks():
 
    global id
 
    window=Tk()
    window.title('ProjectGurukul Library Management System')
 
    greet = Label(window, font = ('arial', 30, 'bold'), text = "Delete Books")
    greet.grid(row = 0,columnspan = 3)
 
    #----------id-------------------
 
    L = Label(window, font = ('arial', 15, 'bold'), text = "Enter Book id: ")
    L.grid(row = 2, column = 1)
 
    L = Label(window, font = ('arial', 15, 'bold'), text = "   ")
    L.grid(row = 2, column = 2)
 
    id=Entry(window,width=5,font =('arial', 15, 'bold'))
    id.grid(row=2,column=3)
 
    submitbtn=Button(window,text="Submit",command=delete_db,bg="DodgerBlue2",fg="white",font = ('arial', 15, 'bold'))
    submitbtn.grid(row=8,columnspan=3)
        
    print("delete")
    pass

Explanation:

All the commands/logic in this file are very similar to add.py, the only difference is it deletes the required book from the library management project.

Book Deletion Page:

delete books page

4. view.py

from tkinter import *
from tkinter import messagebox
import mysql.connector
 
def viewBooks():
 
    global id
 
    window=Tk()
    window.title('ProjectGurukul Library Management System')
 
    greet = Label(window, font = ('arial', 30, 'bold'), text = "View Books")
    greet.grid(row = 0,columnspan = 3)
 
    db = mysql.connector.connect(host ="localhost",user = "root",password = 'your password',database='db')
    cursor = db.cursor()
 
    sqlquery= "select * from books ;"
    print(sqlquery)
 
    try:
        cursor.execute(sqlquery)
        # db.commit()
        L = Label(window, font = ('arial', 20), text = "%-10s%-20s%-20s%-20s"%('BID','Title','Author','Available'))
        L.grid(row = 1,columnspan = 4)
 
        L = Label(window, font = ('arial', 20), text = "----------------------------------------------------------------")
        L.grid(row = 2,columnspan = 4)
 
        x=4
        for i in cursor:
            L = Label(window, font = ('arial', 15), text = "%-10s%-20s%-20s%-20s"%(i[0],i[1],i[2],i[3]))
            L.grid(row = x,columnspan = 4)
            x+=1   
 
    except:
        messagebox.showinfo("Error","Cannot Fetch data.")
    
    print("view")
    pass

Explanation:

This file contains a function that displays all the information of the books present in the database along with the availability status in python library management system.

View Books Page:

view library books

5. issue.py

from tkinter import *
from tkinter import messagebox
import mysql.connector
 
def issue_db():
 
    global id
    global StudentName
 
    bid=id.get()
    bStudentName=StudentName.get()
 
    db = mysql.connector.connect(host ="localhost",user = "root",password = 'your password',database='db')
    cursor = db.cursor()
    
    print(bid,end='--')
    print(bStudentName,end='--')
    print("issue")
 
    try:
        checkavailability=" select * from books where available='YES';"
        print(checkavailability)
        cursor.execute(checkavailability)
 
        flag=0
 
        for i in cursor:
            print(i[0])
            if(i[0]==bid):
                flag=1
                break;
        
        if flag==1:     
            updatequery="update books set available='NO' where bid='"+bid +"';"
            print(updatequery)
            cursor.execute(updatequery)
            db.commit()
 
            sqlquery= "insert into issue values('" + bid +"','" +bStudentName+"' );"
            print(sqlquery)
 
            cursor.execute(sqlquery)
            db.commit()
 
            messagebox.showinfo('Success',"Book issued Successfully")
        else:
            messagebox.showinfo("Error","Required Book is not available")
    except:
        messagebox.showinfo("Error","Cannot issue given book ")
    
def issueBooks():
 
    global id
    global StudentName
 
    window=Tk()
    window.title('ProjectGurukul Library Management System')
 
    greet = Label(window, font = ('arial', 30, 'bold'), text = "Issue Books")
    greet.grid(row = 0,columnspan = 3)
 
    #----------id-------------------
 
    L = Label(window, font = ('arial', 15, 'bold'), text = "Enter Book id: ")
    L.grid(row = 2, column = 1)
 
    L = Label(window, font = ('arial', 15, 'bold'), text = "   ")
    L.grid(row = 2, column = 2)
 
    id=Entry(window,width=5,font =('arial', 15, 'bold'))
    id.grid(row=2,column=3)
 
    #----------StudentName-------------------
 
    L = Label(window, font = ('arial', 15, 'bold'), text = "Enter StudentName: ")
    L.grid(row = 4, column = 1)
 
    L = Label(window, font = ('arial', 15, 'bold'), text = "   ")
    L.grid(row = 4, column = 2)
 
    StudentName=Entry(window,width=5,font =('arial', 15, 'bold'))
    StudentName.grid(row=4,column=3)
    
    submitbtn=Button(window,text="Submit",command=issue_db,bg="DodgerBlue2",fg="white",font = ('arial', 15, 'bold'))
    submitbtn.grid(row=8,columnspan=3)
        
    print("issue")
    pass

Explanation:

  • Home.py will call issueBooks() function when the user wants to issue a book from the library.
  • issueBooks function asks the user to enter his name and the book id and it also offers a submit button.
  • Submit button calls issue_db() function which actually issues the book and updates the database.
  • First it checks if the asked book is available or not. If not it displays ‘Required Book is not available’.

#IMG

  • If the asked book is available, then it issues the book to the student and enters the details in the ‘issue’ table. Also, it updates the availability status of the book to ‘No’ in the books table.
  • Cursor executes the query and then we commit (save) the changes in the database.

Issue Books Page:

issue library book

6. return.py

from tkinter import *
from tkinter import messagebox
import mysql.connector
 
def return_db():
 
    global id
 
    bid=id.get()
 
    db = mysql.connector.connect(host ="localhost",user = "root",password = 'your password',database='db')
    cursor = db.cursor()
    
    print(bid,end='--')
    print("return")
 
    try:
        checkavailability=" select * from books where available='NO';"
        print(checkavailability)
        cursor.execute(checkavailability)
 
        flag=0
 
        for i in cursor:
            print(i[0])
            if(i[0]==bid):
                flag=1
                break;
        
        if flag==1:     
            updatequery="update books set available='YES' where bid='"+bid +"';"
            print(updatequery)
            cursor.execute(updatequery)
            db.commit()
 
            sqlquery= "delete from issue where bid='" + bid +"';"
            print(sqlquery)
 
            cursor.execute(sqlquery)
            db.commit()
 
            messagebox.showinfo('Success',"Book returned Successfully")
        else:
            messagebox.showinfo("Error","Invalid Book id")
    except:
        messagebox.showinfo("Error","Cannot return given book ")
    
def returnBooks():
 
    global id
 
    window=Tk()
    window.title('ProjectGurukul Library Management System')
 
    greet = Label(window, font = ('arial', 30, 'bold'), text = "Return Books")
    greet.grid(row = 0,columnspan = 3)
 
    L = Label(window, font = ('arial', 15, 'bold'), text = "Enter Book id: ")
    L.grid(row = 2, column = 1)
 
    L = Label(window, font = ('arial', 15, 'bold'), text = "   ")
    L.grid(row = 2, column = 2)
 
    id=Entry(window,width=5,font =('arial', 15, 'bold'))
    id.grid(row=2,column=3)
 
    
    submitbtn=Button(window,text="Submit",command=return_db,bg="DodgerBlue2",fg="white",font = ('arial', 15, 'bold'))
    submitbtn.grid(row=8,columnspan=3)
        
    print("return")
    pass

Explanation:

All the commands in this file are very similar to issue.py, the only difference is it deletes the book entry from the issue table and updates the availability status to ‘YES’ in the books table.

Return Books Page

book return

Summary

We have finally completed the Library Management System project. Here are all the files we have created with a short description of the task they perform.

Functionalities and the file names:

  • add.py: to add Books
  • delete.py: to delete Books
  • View.py: to view Books along with their availability status
  • Issue.py: to issue Books
  • Return.py: to return Books
  • Home.py: Home page displaying all the functionalities.

Happy Coding !!!

We work very hard to provide you quality material
Could you take 15 seconds and share your happy experience on Google | Facebook

3 Responses

  1. SUBHASRI K says:

    Good

  2. nidar says:

    it is definitely gonna to be helpfull

  3. Udaya Pamarthi says:

    Where is fine module sir?

Leave a Reply

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