Library Management System Using Python(GUI + Database)

Hello all, Library Management system is a simple yet fantastic project which keeps track of each book present in a library as well as the books which are issued, returned by the reader.  It is an essential piece of software required by schools and colleges to maintain and library record of students.

📌 Why you should make this project ~ The peoples who want to enhance their python skills then this sort of project are very nice to try and build and the students in college can present this project in their DBMS system. 


Library Management System using Python Project

We are going to make a complete library Management system project using python and GUI using a popular module named Tkinter which is so interactive in nature and for the database, we will use MySQL for sake of simplicity.

👉 If you do not have MySQL installed then, please download it from here

What is Tkinter?

Python offers a huge st of libraries to design a GUI interface and one such amazing utility is Tkinter. Tkinter is an open-source Python module used for building GUI applications. Developing desktop-based applications using python Tkinter is not a complex task, you can simply create a top-level window and apply your designs. It contains a huge set of widgets like buttons, check buttons, canvas, frame, labels, and many more.

we have already created 2 projects based on Tkinter so if you want to practice or understand in-depth please explore this tab.

Library Management System in Python

We have to understand the introduction of our project, now let's jump into its implementation part. The final project will look something like this with each CRUD operation running on the system.

Perform All CRUD Operation Python Database Project with GUI Support

Project Prerequisites

you can simply install the following modules by simply running the pip command in your command prompt.
  • Tkinter:     pip install Tkinter
  • pillow:       pip install pillow
  • pymysql:  pip install pymysql

To work with pymysql, you need MySQL installed. Instead of pyMySQL, you can also use MySQL.connector library you like that, all the methods are same.

Description of Project files:

Below are the project files you need to make to complete the project. you can also download the complete source code from the link provided at end of the article.

  • main.py: file which does function calling to all other functions and it is our home page of the project.
  • AddBook.py: a program that performs the task of adding new books to the library.
  • ViewBooks.py: To view the complete list of books in a library
  • DeleteBook.py: To Delete a book from the library.
  • IssueBook.py: To Issue a book from the library.
  • ReturnBook.py: To Return a book to a library.

We will implement each and every function step by step and understand which performs what task. so please be calm and enjoy the project. let's dive in.

Create The Database and Tables

First, in your MySql create a new database and create the following tables which will be used to perform database operations. One by one run the below requires creating two new tables in our database.

create database mydb;      

use mydb;
                                                
create table books(bid varchar(20) primary key, title varchar(30), author varchar(30), status varchar(30)); 

create table books_issued(bid varchar(20) primary key, issueto varchar(30));

Implement the Library Management, Coding

let's start the detailed discussion of each file and function one by one.

1) main.py (Home Page)

Import Modules

we have installed a 3 module, now to work with them, import them first.


Connect to MySQL server

code:

Designing a Window

Now, we will design a project window add a background image to it and then, add buttons one by one as we have seen in the image.

root = Tk()
root.title("Library")
root.minsize(width=400, height=400)
root.geometry("600x500") 

Explanation: Running the above code will create an empty top-level Tkinter widget with the title library. And we have set the minimum height and width of the window and geometry.


Add a Background Image to the window

💻 code: 

Explanation: We load the image using .open() method and store it in background_image variable. we get the dimension of the image and with the help of resizing function, we resize the image according to the size of the window.
here we have used one dummy variable to increase or decrease the height of an image. If you are using different window geometry then, by increasing or decreasing then, you can fix the image dimension at your window.

  • .PhotoImage():- the method id used to display the image(either grayscale or true-color images) in labels, canvas, buttons, or text widgets.
  • canvas: canvas is used to add structured graphics to the python application. here we have created an image on canvas using the create_image() method and passing width, height, and image to it as a parameter. and configure it with the new width and height of an image.  we use the .pack() method to organize widgets in a block, before placing them into the main parent widget(root).

Setting the Heading Frame

💻 code:


we have added a frame to the parent widget which contains a label of heading. Label() widget is used to add labels and we have added some designing parameters to it like background color, font color, and font. the place() method is used to add a particular widget to the main window at a particular height and width with flexible margin space in x and y-direction.

Adding a Buttons

we have added all the buttons to the main window. the functionality of this button will come from corresponding files which we have seen in the project file description above and Now, we need to build each functionality.
we placed these buttons using the .place() method by defining position as well as dimensions.

2) AddBook.py

Implement a function to add a new book to the library

Add a new Books to Library

2.1. Import Modules

Import all the necessary modules. 

from tkinter import *
from PIL import Image, ImageTk
from tkinter import messagebox
import pymysql

2.2. Function BookRegister()

The function is used to commit the changes to the database by running an SQL query and gives a successful message if all the things entered are correct.

Explanation:

First, we fetch the data with help of getting () method from the form. after getting the data we are ready to execute the SQL command and we execute this under the try-except block to handle the run time exception efficiently. If entered details are right and the connection is oriented then, your changes will be committed fine to a database.

we have saved our query in insertBooks and executed it using the .execute() method which is associated with cur(cursor object to database connection).

2.3. Function AddBook()

This function connects to a database(MySQL) and creates a window containing a text field where the user can enter book details and buttons to submit and quit for adding a new book. after clicking the submit button it will call the bookRegister() function, which will fetch the entered data and commit the changes.



Explanation:

We have used a different variable to store each item and one is for connection and cursor object.

  • bookInfo1: It contains book Id
  • bookInfo2: It contains the title of a book.
  • bookInfo3: contains the book Author.
  • bookInfo4: contains the status of the book(available, issued, not available).
  • con: MySQL console connection object
  • cur: Cursor object to the console
we have also included the 2 buttons to submit or quit as:
  • Submit: to submit the changes
  • Exit: to quit the window.

We declare certain variables global to use in the bookRegister() function as well. We create another window for AddBooks same as main.py only designing is different, the structure is the same. we pass the cursor control over the shell(cur). It means whatever we want to perform on MySQL shell, will do it through cur. To commit the changes, we will use the connection object(con).

  • 📌 we draw a canvas and design it with a background color. we have added a headingLabel inside the headingFrame and given it a title as "Add Books".
  • 📌 we create the labelFrame, which basically creates the black box to display the input files to enter book details.
  • 📌 In this LabeFrame, we create labels like book Id, title, author, and place them using the place() method with appropriate dimensions. In corresponding to each Label we provide an input text field using Entry().
  • 📌 Finally, at end of the window, we add a button to submit the details to add book and exit in case a user does not intend to add the details. 


3) ViewBooks.py

View Books from Library

3.1) Import the necessary modules

Import all the libraries, same as we have imported in the previous file AddBook.py. 

3.2) Connect to MySQL Server

write the same code to connect to the MySQL server as we have written in the main.py file.

3.3) Function - View()

The view function in our library creates a new window to showcase the complete record of books in table books.


Explanation:

  • 👉 First, we create a new window to display the list of books and their status.
  • 👉 just like we did in the previous file, we create a headingFrame and a title label to it as "View Books" and same as previous we again create a black box to display the list of books by executing a simple query under the getBooks command.
  • 👉 we manually display the name of columns associated with our book table. we execute the query using the .execute() method and we run a for loop to display one by one row with the y-axis difference of 0.1. 
  • 👉 To handle any exception, we place the execution code in the try-except block.
  • 👉 In the end, we also add a quit button to the view books window to close after having a look at the list of books.


4) DeleteBooks.py

Delete book from Library


Now we have to implement a functionality to delete any book from the table.

4.1. Import all the necessary modules

Import all the same libraries as we have imported in previous files.

4.2. Connect to MySQL database

Make a connection with the database using the same code as we have used in the previous file to orient the connection with the MySQL server.

4.3. Function - DeleteBook()

The function primary checks the bid in our books table, and if it exists then it successfully executes the query to delete that particular record.


Explanation:
we store the SQL query in the delete SQL command and issue SQL command for respective table books and books issued. Note that you have declared both the table names before or after the connection.
we execute the SQL command in try-except block to handle any run time exception. In case some has lost the books we should delete that book from the database and along with deleting it with the books table, we will also delete it from the books issued table.

4.4 Function - delete()

The function creates a window to take a book Id input. we fetch the details of the book from the user and then call the deleteBook() function to commit the changes and delete a Book from the record.

Explanation:
First, create a new window. add a headingFrame and window title label to it. After that same as the previous one create a black box labelFrame and place a label along with the Entry() command to take book Id as input.
We add the submit and quit buttons. whenever the submit button gets clicked it triggers the deleteBook() function, and it fetches the data to commit the changes.

5) IssueBook.py

Issue Book


5.1. Import Modules
Import all the necessary modules, same as we have imported in previous files.

5.2. Connect to MySQL server

Now, orient the connection with the MySQL database using the same code as we have used in previous files.

5.3. Function - issue()

Code:

Explanation:
First, we fetch the book Id and Issuer's name in bid and issueto variables respectively.
After, that we retrieve all the book IDs from the books table by executing a simple SQL select query and storing them in the allbid named list.
we check for the desired book id in allbid. 
If it is present and the status is available as true then, we update the books table as the status changed to "issued" and the book gets successfully issued to a user by displaying a success message to the screen.

5.4. Function - issueBook()

Code:

Explanation:
Same as all other files, in a second function we create a new window, add a heading Frame with a label containing a particular function title. Then, it contains a black box where we add labels and buttons with input text fields to take book Id and name to issue the book.
On clicking the submit button it will call the issue() function which will fetch the data and check if everything is right then commit the changes.

6) ReturnBook.py

Return Book

6.1. Import Modules
Import all the necessary modules, same as we have imported in previous files.

6.2. Connect to MySQL server

Make the connection with the MySQL database using the same code as we have used in previous files using the connection and cursor object.

6.3. Function - Return()

Function to make changes in tables when the book is returned. Note that the name of a function should be from the capital or put the extra n at the end because the return is a keyword.

Code-


Explanation:

The function is very similar to issue() function we designed for issueBook() function.

In this function, we fetch the desired book Id and store it in a bid which book is to be returned. Then, we retrieve all the bids from the books table by executing the extracted command. After that we check the existence of bid in allbid, also we check the status of a particular book is "issued", and if we get status as True then the book is returned successfully by changing the status as "available" and showing a success message to a user.


6.4. Function - ReturnBook()

Create the window for taking the Book Id as input and submit button to call the Return function.

💻 Code-

Explanation:

we create and place a heading frame with the title label of the window. After that, we take the book Id as input. Then, we create two buttons to submit or quite a task to make your submission successful.


🔰 Download The Complete Project Source code: Library Management using Python

Summary

Hooray! We have successfully designed a complete Library Management system using python with a decent UI which is working fine. The project can look a little bit hazy during implementation due to the large codebase, but it's straightforward to understand and implement, and I hope that you are also capable of catching the concepts used very easily.

👉 In this project, we have divided the various task into different files in a particular folder. In real-world scenarios, the same practice is followed to make things easy to understand and build. And constructing any package or library follows the same process.

👉 Apart from this, you can extend this project to an advanced level by implementing a history tab, which will keep the track of all the daily library activities, how many books and which books are been issued by whom, and when. Moreover, you can also integrate a login system to authenticate a user before making any changes to a database.

👉 If you are having any queries then, please post them out in the comment section below 👇, I will very much happy to help you out and it can help someone to learn something new.

Keep learning, keep exploring, keep smiling😊
Thank you!..

6 Comments

If you have any doubt or suggestions then, please let me know.

  1. Amazing project.
    Thank you for complete explanation.

    ReplyDelete
  2. Very nice for beginners
    Thanks for in-depth explanation

    ReplyDelete
  3. How to install this project after compete programming

    ReplyDelete
  4. I have a question that if there are a lot of books in our database then how we can see all of them as in mine the window cuts off when i try to see more

    Please Help

    ReplyDelete
Previous Post Next Post