Build Library Management Software Using Tkinter From Scratch

Or maybe you’re just a reader trying to keep track of what you’re reading and what else the library has to offer.

Well, being a developer you have the advantage of being able to create almost anything.

We’ll be creating a Library Management Software today just using a server-side database and Tkinter, which can do all sorts of things.

So let’s get started and build this thing!What Is Tkinter?To put it simply, Tkinter is a library in Python which helps us build the easiest and fastest GUI (Graphical User Interface) applications out there.

It’s the most commonly used method because of how easy it is to use.

To learn more about Tkinter go to the Python website, although I would recommend checking out this tutorial as I think it’s much easier to understand than the official one.

How to UseTo use any module, the first and foremost rule is always to import them.

In our case, let’s just call it tk for a shorthand notation.

Your command should be import tkinter as tk .

This module always has two things which are absolutely necessary.

To add the main window object from our module, which will actually open the window where we’ll display our content on runtime.

To do this we have an object called Tk() which is initialized on any custom window.

window = tk.

Tk() where window is our main window object.

To enclose all our widgets inside this window, we want it to run continuously in a loop.

Tkinter provides a method called mainloop(), which is placed at the very end of our file so that the window always runs on a loop untill the user manually closes it.

To demonstrate how these works see the example above.

Pillow or PILOne more thing we’ll be needing is Pillow; a free library for Python that adds support for opening, manipulating, and saving many different image file formats.

Don’t worry, we’ll just use it to add a small image in our homepage.

Once Pillow is installed in your system you’re ready to import it and get started.

You’ll also need an image file, which will be placed in your home screen, so feel free to choose your own image or you could work with mine.

Pillow has lots of applications, but what we’ll be using is the Image module.

To import Pillow in your file, type from PIL import Image, and then an object where you’ll be storing the image.

img = Image.

open("path or image").

Installing All the Packages Into Your Python EnvironmentBefore implementing and using all the modules into our source code, there’s always a very important step that needs to be followed every time a new package is added, i.

e to install it in the Python environment.

This process is very simple — all you need is a terminal of your choice.

Additionally, you also have to make sure you have pip available.

You can check this by typing pip –versionpip versionIf you’ve installed Python from source, with an installer from python.

org, or via Homebrew you should already have pip.

If you’re on Linux and installed using your OS package manager, you may have to install pip separately.

Installing the packagesTo install the packages in your Python environment, type the following in any terminal:pip install tkinterpip install pillowpip install pymysqlBut if you’re using any other distribution for Python (I use Anaconda3), then the steps to install the packages are a little different.

First, open up Anaconda Prompt, then type in the following:conda install -c anaconda tkinter #Once prompted press yconda install -c anaconda pillow #Once prompted press yconda install -c anaconda pymysql #Once prompted press yHow Should Our Final Product LookIn this project, the functionalities we’ll create are simple but branched, so it’s better if you keep a note of what sections we’re about to build :Overview of the final projectWe’ll create a simple home page, which will give the users the choice of whether they want to login as an employee (with all the powers that authorities have), or as a student (with fewer options).

We’ll create a registration page for first time users, and also a login page for existing users.

(For employees as well as students).

Once logged in, the user will get certain menu choices.

If logged in as an employee they have all the authority to add a book, search for books, delete a book or even issue any book to the students.

But if they’re logged in as a student they only get two options; to either view all the books or to search for a particular one.

Last but not the least, we’ll use a server-side database purely using MySQL, so that all the records we create can be properly saved.

Check out the full source code here:S-ayanide/Library-ManagerA library management software where you can view all the book present in your library, issue books to the student, and do a…github.

comHome PageThis will be the main dashboard whenever anyone opens our application.

It gives the user two choices; to either log in as an employee or as a student.

First, let’s add our background image.

Feel free to choose your own image but in this case, I’ll choose my image from the source files.

Make an image object using the image module of Pillow like we did before background_image = Image.


Now your image is stored in this object, but to display it in the window we need ImageTk, which is another module of the Pillow package.

You can’t just display an image without a background.

To do that you have to create a Canvas Widget.

They are rectangular boxes intended for drawing pictures or other complex layouts.

You don’t want the canvas to stick around with a fixed height and width, if the user ever increases the size of the window.

To implement this idea we extracted the ImageWidth and ImageHeight from the image object and every time the window size increases we multiply the canvas dimensions by a factor “n”, which shouldn’t be less than 0.

25 or greater than 5 (the image becomes too small or too large).

To produce the double overlap on the heading section, we’ve used two frames and overlapped them with different colors.

And lastly, two simple buttons connected to custom functions, which will take the user to the registration and login page upon button click (which we haven't yet created).

NOTE: We are using place() instead of pack() as it gives better visualization as well as better placements of the components.

Registration and LoginUpon user click, we’ll have separate windows for employee and student registration and login.

(With different attributes).

To create this functionality, we’ll add three functions — employee registration EmpRegister(), student registration studentRegister(), and login Login().

However, before implementing this, we need to clear out the previous screen or otherwise all the new labels and headers will be overlapped, and we won’t be getting the output we’re looking for.

To clear out all those previous components that we don’t need, we set them as global in the function which we’re creating.

For example, if your parent function has a label frame, and the parent is calling the child function, you should make the components global in the child function and then destroy them there.

In this window, we’ve destroyed all the components that we don’t need, and added a new canvas to give it that ‘light beach shade’ in the background.

In the middle, we’ve created a label frame which is turquoise in color and added all the labels we need with the entry boxes inside it.

We create two simple buttons, namely submit and quit.

These buttons will come in handy during the whole project.

Whatever function we create, we can have a callback to them using command = <functionName>, and thus, after we create the registration or login function, we’ll just link them to the submit button itself.

Once the user enters their credentials we call a getter function, which, as the name suggests, gets all the values entered and stores them in their appropriate variables.

Later we add these details into our MySQL database.

After getting all the values and performing the necessary database handling, we clear the input field.

It’s not considered good practice if the user has to delete what they’ve typed.

Rather, by implementing the delete(0,END), we make sure that our software auto clears the input field value after the user clicks on the submit button.

All the values entered by the user are validated, and if all the conditions are satisfied a message box is displayed, which says, “You are successfully logged in”, and transferred to the menu.

Otherwise you’ll be shown what the errors/validations are that didn’t match the criteria.

S-ayanide/Library-ManagerCheck out the functions mentioned above for more information…github.

comCreating MenusNow that you have successfully logged in and are able to fetch the data from the registered users, let’s make a menu screen (two actually; one for employees and one for students).

Applying the same concept of declaring components global, we delete all the assets/component widgets that we don’t need in this screen, and then we create a fresh one.

All the screens from now onwards are fairly simple.

All there is to it is a heading frame and lots of black buttons in the middle, with functionalities like adding a book, deleting a book, etc.

You might know by now that, to implement these functionalities, we need to create a new function which we’ll call through our buttons.

So instead of cramping up a single file, let’s actually divide them and create new files, which we’ll import into the main project from now on (which also means all of these will open in a new windows since we have Tk() inside them).

Similarly, another menu function will be created for the students with only two buttons — to view all books and to search for specific ones only — so that’ll be much shorter in size.

In the command section of each button, you can see certain functions attached to them.

They are nothing but the functions that I told you to keep separate, and which will be imported later.

Let’s talk about these simple functions one by one.

Add booksA window where employees can add books into the library with certain information regarding the book.

(Book ID, name, etc)Add BooksAddBooks()This function creates a new canvas, which provides the window with a blue background color when it opens.

Also, there is a black label frame at the center, which contains labels and entries inside it, with information like book ID, title, subject, author and status of the book (Avail/Issued).

Once clicked on, submit bookRegister() is called.

BookRegister()This gets all the values entered by the user, creates a connection to the database and inserts all the values there.

Viola, your book is added.

Adding Books into the DatabaseDelete bookA window where employees can delete their books from the library.

Delete Bookdelete()This function creates a new canvas, which provides the window with the grey background color when it opens.

There is a black label frame at the center, which contains a label and entry which asks for the book ID.

Upon pressing “submit”, it sets a callback to the deleteBook() function.

deleteBook()This gets the value from the input field and deletes the book from the table.

Deletes book data from the databaseView all booksThis window shows us a tabular form of all the books present in our library.

Whenever we add any new book that shows up here, any changes we make in the table will be reflected here.

View BooksView()Has a canvas which provides the light yellow shade at the background.

Also has a heading frame as we had previously.

And a label frame where the table is displayed.

This function keeps track of the y-coordinate, and after each data is displayed it shifts the coordinate downwards by 0.

2, giving us a table like structure.

View all the books in the library in a tabular formatSearch bookSearch BookIt is exactly similar to delete book.

The only difference is that it globalizes the frames and deletes them after searching as it displays the result in the same frame.

Search()It fetches the data (subject) from the table, and returns all the data that matches.

For example, if the user searches for Python, it’ll display all the books with Subject = Python.

Upon clicking the button, it destroys the previous label and entry to display the search result there only.

Searches books of given subjectIssue bookThis window gives the option to the employee to issue a book to the student, provided that he enters the correct book ID and the correct roll number of the student, as well as the correct employee ID.

Issue BooksIssueBook()Creates a purple canvas with a heading frame, as well as a label frame with three entry boxes.

Upon clicking Issue it creates a callback to issue(), which does most of the work here.

Issue()Gets the values entered by the user, and checks whether the book ID, employee ID and the student roll number entered by the user, is present in the database or not.

If, and only if all three of them are present, and the book has a status of ‘avail’ (which mean is available), only then is the book issued to the student.

Upon successfully issuing the book, the status of the book changes from ‘avail’ to ‘issued’.

Issues books to Students and also updates book statusThat’s all for this software.

In the student part, you have the same button (View all books and search for one), so just set a callback to the same functions that you just created.

Thank you for reading this.

I really appreciate all the effort.

If you’re facing any issues creating the software, try comparing them with my source code, or comment below and I’ll try to answer them.

.. More details

Leave a Reply