Ip Project
Ip Project
Create Tables
title varchar(30),
author varchar(30),
status varchar(30));
1. books
2. issued_books
Let’s start the detailed discussion of each and every file of our library management system python
project:
1. main.py
Code:
import pymysql
Now we will connect to the server with the correct credentials associated with the MySql server installed
on our system.
Code:
Now we will design the project window and add a background image. Make sure to keep the image in
the same directory as the project is in order to avoid discrepancies.
Code:
root = Tk()
root.title("Library")
root.minsize(width=400,height=400)
root.geometry("600x500")
Explanation:
The above code sets the title of the library project window as ‘Library’. When you run the above code,
you will get a window.
Output:
Code:
same=True
n=0.25
background_image =Image.open("lib.jpg")
newImageSizeWidth = int(imageSizeWidth*n)
if same:
newImageSizeHeight = int(imageSizeHeight*n)
else:
newImageSizeHeight = int(imageSizeHeight/n)
background_image =
background_image.resize((newImageSizeWidth,newImageSizeHeight),Image.ANTIALIAS)
img = ImageTk.PhotoImage(background_image)
Canvas1 = Canvas(root)
Canvas1.create_image(300,340,image = img)
Canvas1.pack(expand=True,fill=BOTH)
Explanation:
We store our image in background_image with the help of .open() method. We fetch the image
dimensions and adjust the image size according to our window size.
Now we resize the image using .resize() method using the new dimensions.
The .PhotoImage() method is used to display images (either grayscale or true color images) in labels,
buttons, canvases, and text widgets.
We create the image on the canvas1 using .create_image() method. We use .pack() method to organize
widgets in blocks before placing them in the parent widget.
Code:
headingFrame1 = Frame(root,bg="#FFBB00",bd=5)
headingFrame1.place(relx=0.2,rely=0.1,relwidth=0.6,relheight=0.16)
Explanation:
We create a frame that will hold our Label wiz headingLabel. We increase the size and alter the font by
passing one more parameter in the Label method wiz font.
Code:
btn1.place(relx=0.28,rely=0.4, relwidth=0.45,relheight=0.1)
btn2 = Button(root,text="Delete Book",bg='black', fg='white', command=delete)
btn2.place(relx=0.28,rely=0.5, relwidth=0.45,relheight=0.1)
btn3.place(relx=0.28,rely=0.6, relwidth=0.45,relheight=0.1)
btn4.place(relx=0.28,rely=0.7, relwidth=0.45,relheight=0.1)
btn5.place(relx=0.28,rely=0.8, relwidth=0.45,relheight=0.1)
root.mainloop()
Explanation:
AddBook Details :
btn1 stores the button created on root with text = ‘AddBook Details’. As soon as someone clicks this
button, we call the function addBook defined in the AddBook.py. We call a function by specifying the
command parameter equal to the name of the function.
We place this button using the .place() method by defining the position as well as dimensions of the
button.
Similarly, we define other buttons using the Button method and keep placing them by making minor
changes in the rely parameter. You can notice that we are increasing it by 0.1 every time we define a new
button.
Output:
ad
2. AddBook.py
Code:
import pymysql
This function executes an SQL command to insert data into the table and commit the changes.
Code:
def bookRegister():
bid = bookInfo1.get()
title = bookInfo2.get()
author = bookInfo3.get()
status = bookInfo4.get()
status = status.lower()
try:
cur.execute(insertBooks)
con.commit()
except:
print(bid)
print(title)
print(author)
print(status)
root.destroy()
Explanation:
We fetch the data in the input field by .get() method. Hence after fetching each of the data fields value
we are ready to execute an SQL command to insert the data.
General Syntax:
insert into <Table Name> values (<the values of the respective columns>)
Modified :
Note : Please make sure to keep a single apostrophe (‘) before and after every variable while writing the
query. This ensures it is treated as a string.
We put this code in a try-except block in order to handle the situation effectively.
Now, we execute the insertBooks command by .execute() method associated with cur. We commit the
changes by .commit() method associated with con as discussed above.
This function connects to the MySql server and creates a window for accommodating new text fields. We
fetch details of a new book from the user and then call bookRegister() function to register the books into
the table.
Code:
def addBook():
global bookInfo1 ,bookInfo2, bookInfo3, bookInfo4, Canvas1, con, cur, bookTable, root
root = Tk()
root.title("Library")
root.minsize(width=400,height=400)
root.geometry("600x500")
mypass = "root"
mydatabase="db"
ad
cur = con.cursor()
Canvas1 = Canvas(root)
Canvas1.config(bg="#ff6e40")
Canvas1.pack(expand=True,fill=BOTH)
headingFrame1 = Frame(root,bg="#FFBB00",bd=5)
headingFrame1.place(relx=0.25,rely=0.1,relwidth=0.5,relheight=0.13)
labelFrame = Frame(root,bg='black')
labelFrame.place(relx=0.1,rely=0.4,relwidth=0.8,relheight=0.4)
# Book ID
lb1.place(relx=0.05,rely=0.2, relheight=0.08)
bookInfo1 = Entry(labelFrame)
# Title
lb2.place(relx=0.05,rely=0.35, relheight=0.08)
bookInfo2 = Entry(labelFrame)
# Book Author
lb3.place(relx=0.05,rely=0.50, relheight=0.08)
bookInfo3 = Entry(labelFrame)
bookInfo3.place(relx=0.3,rely=0.50, relwidth=0.62, relheight=0.08)
# Book Status
lb4.place(relx=0.05,rely=0.65, relheight=0.08)
bookInfo4 = Entry(labelFrame)
#Submit Button
SubmitBtn.place(relx=0.28,rely=0.9, relwidth=0.18,relheight=0.08)
quitBtn.place(relx=0.53,rely=0.9, relwidth=0.18,relheight=0.08)
root.mainloop()
Variables
Buttons
Exit
Explanation:
We declare certain variables as global in order to use them in the bookRegister() function.
We create another window and connect to the MySql server and pass on the cursor control to cur. It
means that whatever we want to write on the MySql shell, we will do it through
ad
cur. In order to commit(write changes in the table) the changes we will use con.
We draw the canvas and paint it with a good color. I have used Orange, you can use any color you like.
We create and place the headingLabel inside the headingFrame1 and give the title as “Add Books”.
The labelFrame basically creates a black box (in our case) to accommodate the input fields to fetch the
book details.
We create and place a Label on our black box which displays the text ‘Book ID:’. Just after Label is placed
we create and place the Entry box acting as our input field.
Similarly, we create and place input fields associated with labels: Title, Author, and Status.
In the end, we create a button to Finally.!! submit the details given by the user and a button to exit just
in case the user did not intend to enter any details in library management.
As an action of a click on the SubmitBtn, we call the bookRegister() function to insert the details into the
books table.
3. ViewBooks.py
ad
Code:
import pymysql
Code:
mypass = "root"
mydatabase="db"
bookTable = "books"
This function in our library project creates a window for displaying the records in the table.
Code:
def View():
root = Tk()
root.title("Library")
root.minsize(width=400,height=400)
root.geometry("600x500")
Canvas1 = Canvas(root)
Canvas1.config(bg="#12a4d9")
Canvas1.pack(expand=True,fill=BOTH)
ad
headingFrame1 = Frame(root,bg="#FFBB00",bd=5)
headingFrame1.place(relx=0.25,rely=0.1,relwidth=0.5,relheight=0.13)
labelFrame = Frame(root,bg='black')
labelFrame.place(relx=0.1,rely=0.3,relwidth=0.8,relheight=0.5)
y = 0.25
Label(labelFrame, text="%-10s%-40s%-30s%-20s"%('BID','Title','Author','Status'),
bg='black',fg='white').place(relx=0.07,rely=0.1)
Label(labelFrame, text =
"----------------------------------------------------------------------------",bg='black',fg='white').place
(relx=0.05,rely=0.2)
try:
cur.execute(getBooks)
con.commit()
for i in cur:
Label(labelFrame,text="%-10s%-30s%-30s%-20s"%(i[0],i[1],i[2],i[3]) ,bg='black',
fg='white').place(relx=0.07,rely=y)
y += 0.1
except:
quitBtn.place(relx=0.4,rely=0.9, relwidth=0.18,relheight=0.08)
root.mainloop()
Explanation:
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 title it with a Label to display ‘View
Books’.
Again we create a black box to accommodate the records returned by the getBooks query.
ad
We manually display the name of the columns associated with our books table.
We execute the query stored in getBooks using cur.execute() display each record one by one as a Label.
We place each Label You can notice that each time a record is displayed the value of y increases by ‘0.1’.
In the end, we create and place a button, quitBtn to exit from the current window of the library project.
4. DeleteBook.py
ad
Code:
import pymysql
mypass = "root"
mydatabase="db"
cur = con.cursor()
issueTable = "books_issued"
This function primarily checks if the bid (book id) exists in the book table and if it does, it executes the
necessary command to remove it.
Code:
def deleteBook():
bid = bookInfo1.get()
try:
cur.execute(deleteSql)
con.commit()
ad
cur.execute(deleteIssue)
con.commit()
except:
print(bid)
bookInfo1.delete(0, END)
root.destroy()
Explanation:
We store the SQL query to delete the record in deleteSql. After that, we execute this command using
cur.execute().
In case someone loses a book, we should delete that book from the issueTable in order to prevent
discrepancies in the future. Hence we store the SQl query to delete the same record from the IssueTable
in deleteIssue. We execute it along with the deleteSql.
This function creates a window for accommodating a text input field. We fetch details of a book from the
user and then call deleteBook() function to delete the book record from the table.
Code:
def delete():
global bookInfo1,bookInfo2,bookInfo3,bookInfo4,Canvas1,con,cur,bookTable,root
root = Tk()
root.title("Library")
root.minsize(width=400,height=400)
root.geometry("600x500")
Canvas1 = Canvas(root)
Canvas1.config(bg="#006B38")
Canvas1.pack(expand=True,fill=BOTH)
headingFrame1 = Frame(root,bg="#FFBB00",bd=5)
headingFrame1.place(relx=0.25,rely=0.1,relwidth=0.5,relheight=0.13)
labelFrame = Frame(root,bg='black')
labelFrame.place(relx=0.1,rely=0.3,relwidth=0.8,relheight=0.5)
# Book ID to Delete
bookInfo1 = Entry(labelFrame)
bookInfo1.place(relx=0.3,rely=0.5, relwidth=0.62)
#Submit Button
SubmitBtn.place(relx=0.28,rely=0.9, relwidth=0.18,relheight=0.08)
quitBtn.place(relx=0.53,rely=0.9, relwidth=0.18,relheight=0.08)
root.mainloop()
Explanation:
Firstly, we create a new window and accommodate a headingFrame followed by creating the labelFrame
to create and place a big black box.
This black box accommodates a Label and an Entry text field to take input of the Book ID.
After the black box, we create and place a Submit and Quit button associated with the name SubmitBtn
and quitBtn respectively. A click on the Submit button triggers the deleteBook() function.
Moreover, we declare some variables as global, in order to provide their access in the deleteBook()
function.
ad
5. IssueBook.py
issue book
Code:
ad
import pymysql
Code:
mypass = "root"
mydatabase="db"
cur = con.cursor()
issueTable = "books_issued"
bookTable = "books"
def issue():
global issueBtn,labelFrame,lb1,inf1,inf2,quitBtn,root,Canvas1,status
bid = inf1.get()
issueto = inf2.get()
issueBtn.destroy()
labelFrame.destroy()
lb1.destroy()
inf1.destroy()
inf2.destroy()
try:
cur.execute(extractBid)
con.commit()
ad
for i in cur:
allBid.append(i[0])
if bid in allBid:
checkAvail = "select status from "+bookTable+" where bid = '"+bid+"'"
cur.execute(checkAvail)
con.commit()
for i in cur:
check = i[0]
if check == 'avail':
status = True
else:
status = False
else:
except:
try:
cur.execute(issueSql)
con.commit()
cur.execute(updateStatus)
con.commit()
messagebox.showinfo('Success',"Book Issued Successfully")
root.destroy()
else:
allBid.clear()
root.destroy()
return
except:
print(bid)
print(issueto)
allBid.clear()
Explanation:
ad
Primarily, we fetch the desired book ID and Issuer’s name and store it into bid and issueto respectively.
After that we store all the Book ID from the books table in allBid by executing the SQL query stored in
extractBid.
We check for the existence of the desired bid in allBid. If it exists and is available, we set the status as
True.
If the book is available we update the books_issued table with the book id (bid) and Issuer’s name
(issueto) and update the books table by changing the status of the issued book to ‘issued’.
5.4. Function – issueBook()
Code:
def issueBook():
global issueBtn,labelFrame,lb1,inf1,inf2,quitBtn,root,Canvas1,status
root = Tk()
root.title("Library")
root.minsize(width=400,height=400)
root.geometry("600x500")
Canvas1 = Canvas(root)
Canvas1.config(bg="#D6ED17")
Canvas1.pack(expand=True,fill=BOTH)
headingFrame1 = Frame(root,bg="#FFBB00",bd=5)
headingFrame1.place(relx=0.25,rely=0.1,relwidth=0.5,relheight=0.13)
labelFrame = Frame(root,bg='black')
labelFrame.place(relx=0.1,rely=0.3,relwidth=0.8,relheight=0.5)
# Book ID
ad
lb1.place(relx=0.05,rely=0.2)
inf1 = Entry(labelFrame)
inf1.place(relx=0.3,rely=0.2, relwidth=0.62)
lb2.place(relx=0.05,rely=0.4)
inf2 = Entry(labelFrame)
inf2.place(relx=0.3,rely=0.4, relwidth=0.62)
#Issue Button
issueBtn.place(relx=0.28,rely=0.9, relwidth=0.18,relheight=0.08)
quitBtn.place(relx=0.53,rely=0.9, relwidth=0.18,relheight=0.08)
root.mainloop()
Explanation:
We create and place a headingFrame and two input fields for taking input of the desired books’ ID and
issuers’ name. After which we create and add two buttons named issueBtn and quitBtn to facilitate
submission of our issue request and closing the present window of library management system
respectively.
6. ReturnBook.py
return book
ad
Code:
import pymysql
Code:
mypass = "root"
mydatabase="db"
cur = con.cursor()
bookTable = "books"
Code:
def returnn():
global SubmitBtn,labelFrame,lb1,bookInfo1,quitBtn,root,Canvas1,status
bid = bookInfo1.get()
try:
cur.execute(extractBid)
con.commit()
for i in cur:
allBid.append(i[0])
if bid in allBid:
cur.execute(checkAvail)
con.commit()
for i in cur:
check = i[0]
if check == 'issued':
status = True
else:
status = False
else:
except:
print(bid in allBid)
print(status)
try:
cur.execute(issueSql)
con.commit()
cur.execute(updateStatus)
con.commit()
else:
allBid.clear()
messagebox.showinfo('Message',"Please check the book ID")
root.destroy()
return
except:
allBid.clear()
root.destroy()
Explanation:
In this library management system project, we fetch the desired book ID and store it into bid.
After that we store all the Book IDs from the books_issued table in allBid by executing the SQL query
stored in extractBid.
We check for the existence of the desired bid in allBid. Also, we check the status of the same book and if
it is ‘issued’, we set the status as True.
Then, we remove the record from books_issued table and update the books table by changing the status
of the issued book to ‘avail’.
Code:
def returnBook():
root = Tk()
root.title("Library")
root.minsize(width=400,height=400)
root.geometry("600x500")
Canvas1 = Canvas(root)
Canvas1.config(bg="#006B38")
Canvas1.pack(expand=True,fill=BOTH)
headingFrame1 = Frame(root,bg="#FFBB00",bd=5)
headingFrame1.place(relx=0.25,rely=0.1,relwidth=0.5,relheight=0.13)
labelFrame = Frame(root,bg='black')
labelFrame.place(relx=0.1,rely=0.3,relwidth=0.8,relheight=0.5)
# Book ID to Delete
bookInfo1 = Entry(labelFrame)
bookInfo1.place(relx=0.3,rely=0.5, relwidth=0.62)
#Submit Button
SubmitBtn.place(relx=0.28,rely=0.9, relwidth=0.18,relheight=0.08)
quitBtn.place(relx=0.53,rely=0.9, relwidth=0.18,relheight=0.08)
root.mainloop()
Explanation:
We create and place a headingFrame and an input field for taking input of the books’ ID. Then, we create
and add two buttons named SubmitBtn and quitBtn to facilitate submission of our return request and
closing the present window respectively.