0% found this document useful (0 votes)
13 views26 pages

Project

ENVIRONMENT
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views26 pages

Project

ENVIRONMENT
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

LIBRARY MANAGEMENT

Description of Project Files


Below are the project files you will get once you download and extract the
Library project:

• main.py – which does function call to all other python files


• AddBook.py – To add the book
• ViewBooks.py – To View the list of books in the library
• DeleteBook.py – To Delete a book from library
• IssueBook.py – To Issue a book from library
• ReturnBook.py – To Return a book to the library

Description of Tables for database connectivity


Create Tables
create database db;
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, issuedto
varchar(30));

Importing the Modules


To use the Tkinter we need to import the Tkinter module. As stated above, we
have imported each file so that we can make function calls from our main file.

Code:
from tkinter import *
from PIL import ImageTk,Image #PIL -> Pillow
import pymysql
from tkinter import messagebox
from AddBook import *
from DeleteBook import *
from ViewBooks import *
from IssueBook import *

Connecting to the MySql server


Now we will connect to the server with the correct credentials associated with
the MySql server installed on our system.

Code:
mypass = "root" #use your own password
mydatabase="db" #The database name
con = pymysql.connect (host="localhost",user="root",password=mypass,database=mydatabase)
#root is the username here
cur = con.cursor() #cur -> cursor

Designing the Window


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")

Code:

same=True
n=0.25
# Adding a background image
background_image =Image.open("lib.jpg")
[imageSizeWidth, imageSizeHeight] = background_image.size
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.config(bg="white",width = newImageSizeWidth, height = newImageSizeHeight)
Canvas1.pack(expand=True,fill=BOTH)

Setting upof head frmae

headingFrame1 = Frame(root,bg="#FFBB00",bd=5)
headingFrame1.place(relx=0.2,rely=0.1,relwidth=0.6,relheight=0.16)
headingLabel = Label(headingFrame1, text="Welcome to \n DataFlair Library", bg='black', fg='white',
font=('Courier',15))
headingLabel.place(relx=0,rely=0, relwidth=1, relheight=1)

Adding the Buttons


Code:
btn1 = Button(root,text="Add Book Details",bg='black', fg='white', command=addBook)
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 = Button(root,text="View Book List",bg='black', fg='white', command=View)
btn3.place(relx=0.28,rely=0.6, relwidth=0.45,relheight=0.1)
btn4 = Button(root,text="Issue Book to Student",bg='black', fg='white', command = issueBook)
btn4.place(relx=0.28,rely=0.7, relwidth=0.45,relheight=0.1)
btn5 = Button(root,text="Return Book",bg='black', fg='white', command = returnBook)
btn5.place(relx=0.28,rely=0.8, relwidth=0.45,relheight=0.1)
root.mainloop()

ouput:
Source Code:

from tkinter import *

from PIL import ImageTk,Image

from tkinter import messagebox

import pymysql

def bookRegister():

bid = bookInfo1.get()

title = bookInfo2.get()

author = bookInfo3.get()
status = bookInfo4.get()

status = status.lower()

insertBooks = "insert into "+bookTable+" values('"+bid+"','"+title+"','"+author+"','"+status+"')"

try:

cur.execute(insertBooks)

con.commit()

messagebox.showinfo('Success',"Book added successfully")

except:

messagebox.showinfo("Error","Can't add data into Database")

print(bid)

print(title)

print(author)

print(status)

root.destroy()

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")

# Add your own database name and password here to reflect in the code
mypass = "root"

mydatabase="db"

con = pymysql.connect(host="localhost",user="root",password=mypass,database=mydatabase)

cur = con.cursor()

# Enter Table Names here

bookTable = "books" # Book Table

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)

headingLabel = Label(headingFrame1, text="Add Books", bg='black', fg='white', font=('Courier',15))

headingLabel.place(relx=0,rely=0, relwidth=1, relheight=1)

labelFrame = Frame(root,bg='black')

labelFrame.place(relx=0.1,rely=0.4,relwidth=0.8,relheight=0.4)

# Book ID

lb1 = Label(labelFrame,text="Book ID : ", bg='black', fg='white')

lb1.place(relx=0.05,rely=0.2, relheight=0.08)

bookInfo1 = Entry(labelFrame)
bookInfo1.place(relx=0.3,rely=0.2, relwidth=0.62, relheight=0.08)

# Title

lb2 = Label(labelFrame,text="Title : ", bg='black', fg='white')

lb2.place(relx=0.05,rely=0.35, relheight=0.08)

bookInfo2 = Entry(labelFrame)

bookInfo2.place(relx=0.3,rely=0.35, relwidth=0.62, relheight=0.08)

# Book Author

lb3 = Label(labelFrame,text="Author : ", bg='black', fg='white')

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 = Label(labelFrame,text="Status(Avail/issued) : ", bg='black', fg='white')

lb4.place(relx=0.05,rely=0.65, relheight=0.08)

bookInfo4 = Entry(labelFrame)

bookInfo4.place(relx=0.3,rely=0.65, relwidth=0.62, relheight=0.08)

#Submit Button

SubmitBtn = Button(root,text="SUBMIT",bg='#d1ccc0', fg='black',command=bookRegister)

SubmitBtn.place(relx=0.28,rely=0.9, relwidth=0.18,relheight=0.08)

quitBtn = Button(root,text="Quit",bg='#f7f1e3', fg='black', command=root.destroy)

quitBtn.place(relx=0.53,rely=0.9, relwidth=0.18,relheight=0.08)
root.mainloop()
from tkinter import *

from PIL import ImageTk,Image

from tkinter import messagebox

import pymysql

# Add your own database name and password here to reflect in the code

mypass = "root"

mydatabase="db"

con = pymysql.connect(host="localhost",user="root",password=mypass,database=mydatabase)

cur = con.cursor()

# Enter Table Names here

bookTable = "books"

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)

headingFrame1 = Frame(root,bg="#FFBB00",bd=5)
headingFrame1.place(relx=0.25,rely=0.1,relwidth=0.5,relheight=0.13)

headingLabel = Label(headingFrame1, text="View Books", bg='black', fg='white', font=('Courier',15))

headingLabel.place(relx=0,rely=0, relwidth=1, relheight=1)

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)

getBooks = "select * from "+bookTable

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:

messagebox.showinfo("Failed to fetch files from database")

quitBtn = Button(root,text="Quit",bg='#f7f1e3', fg='black', command=root.destroy)

quitBtn.place(relx=0.4,rely=0.9, relwidth=0.18,relheight=0.08)

root.mainloop()
Source Code :

from tkinter import *

from PIL import ImageTk,Image

from tkinter import messagebox

import pymysql

# Add your own database name and password here to reflect in the code

mypass = "root"

mydatabase="db"

con = pymysql.connect(host="localhost",user="root",password=mypass,database=mydatabase)
cur = con.cursor()

# Enter Table Names here

issueTable = "books_issued"

bookTable = "books" #Book Table

def deleteBook():

bid = bookInfo1.get()

deleteSql = "delete from "+bookTable+" where bid = '"+bid+"'"

deleteIssue = "delete from "+issueTable+" where bid = '"+bid+"'"

try:

cur.execute(deleteSql)

con.commit()

cur.execute(deleteIssue)

con.commit()

messagebox.showinfo('Success',"Book Record Deleted Successfully")

except:

messagebox.showinfo("Please check Book ID")

print(bid)

bookInfo1.delete(0, END)

root.destroy()

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)

headingLabel = Label(headingFrame1, text="Delete Book", bg='black', fg='white', font=('Courier',15))

headingLabel.place(relx=0,rely=0, relwidth=1, relheight=1)

labelFrame = Frame(root,bg='black')

labelFrame.place(relx=0.1,rely=0.3,relwidth=0.8,relheight=0.5)

# Book ID to Delete

lb2 = Label(labelFrame,text="Book ID : ", bg='black', fg='white')

lb2.place(relx=0.05,rely=0.5)

bookInfo1 = Entry(labelFrame)

bookInfo1.place(relx=0.3,rely=0.5, relwidth=0.62)
#Submit Button

SubmitBtn = Button(root,text="SUBMIT",bg='#d1ccc0', fg='black',command=deleteBook)

SubmitBtn.place(relx=0.28,rely=0.9, relwidth=0.18,relheight=0.08)

quitBtn = Button(root,text="Quit",bg='#f7f1e3', fg='black', command=root.destroy)

quitBtn.place(relx=0.53,rely=0.9, relwidth=0.18,relheight=0.08)

root.mainloop()

Issue Book
Source code :

from tkinter import *

from PIL import ImageTk,Image

from tkinter import messagebox

import pymysql

# Add your own database name and password here to reflect in the code

mypass = "root"

mydatabase="db"
con = pymysql.connect(host="localhost",user="root",password=mypass,database=mydatabase)

cur = con.cursor()

# Enter Table Names here

issueTable = "books_issued"

bookTable = "books"

#List To store all Book IDs

allBid = []

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()

extractBid = "select bid from "+bookTable

try:

cur.execute(extractBid)

con.commit()
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:

messagebox.showinfo("Error","Book ID not present")

except:

messagebox.showinfo("Error","Can't fetch Book IDs")

issueSql = "insert into "+issueTable+" values ('"+bid+"','"+issueto+"')"

show = "select * from "+issueTable

updateStatus = "update "+bookTable+" set status = 'issued' where bid = '"+bid+"'"

try:

if bid in allBid and status == True:

cur.execute(issueSql)

con.commit()

cur.execute(updateStatus)
con.commit()

messagebox.showinfo('Success',"Book Issued Successfully")

root.destroy()

else:

allBid.clear()

messagebox.showinfo('Message',"Book Already Issued")

root.destroy()

return

except:

messagebox.showinfo("Search Error","The value entered is wrong, Try again")

print(bid)

print(issueto)

allBid.clear()

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)

headingLabel = Label(headingFrame1, text="Issue Book", bg='black', fg='white', font=('Courier',15))

headingLabel.place(relx=0,rely=0, relwidth=1, relheight=1)

labelFrame = Frame(root,bg='black')

labelFrame.place(relx=0.1,rely=0.3,relwidth=0.8,relheight=0.5)

# Book ID

lb1 = Label(labelFrame,text="Book ID : ", bg='black', fg='white')

lb1.place(relx=0.05,rely=0.2)

inf1 = Entry(labelFrame)

inf1.place(relx=0.3,rely=0.2, relwidth=0.62)

# Issued To Student name

lb2 = Label(labelFrame,text="Issued To : ", bg='black', fg='white')

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 = Button(root,text="Issue",bg='#d1ccc0', fg='black',command=issue)

issueBtn.place(relx=0.28,rely=0.9, relwidth=0.18,relheight=0.08)

quitBtn = Button(root,text="Quit",bg='#aaa69d', fg='black', command=root.destroy)


quitBtn.place(relx=0.53,rely=0.9, relwidth=0.18,relheight=0.08)

root.mainloop()

Source Code :

from tkinter import *

from PIL import ImageTk,Image

from tkinter import messagebox

import pymysql
# Add your own database name and password here to reflect in the code

mypass = "root"

mydatabase="db"

con = pymysql.connect(host="localhost",user="root",password=mypass,database=mydatabase)

cur = con.cursor()

# Enter Table Names here

issueTable = "books_issued" #Issue Table

bookTable = "books" #Book Table

allBid = [] #List To store all Book IDs

def returnn():

global SubmitBtn,labelFrame,lb1,bookInfo1,quitBtn,root,Canvas1,status

bid = bookInfo1.get()

extractBid = "select bid from "+issueTable

try:

cur.execute(extractBid)

con.commit()

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 == 'issued':

status = True

else:

status = False

else:

messagebox.showinfo("Error","Book ID not present")

except:

messagebox.showinfo("Error","Can't fetch Book IDs")

issueSql = "delete from "+issueTable+" where bid = '"+bid+"'"

print(bid in allBid)

print(status)

updateStatus = "update "+bookTable+" set status = 'avail' where bid = '"+bid+"'"

try:

if bid in allBid and status == True:

cur.execute(issueSql)

con.commit()

cur.execute(updateStatus)

con.commit()

messagebox.showinfo('Success',"Book Returned Successfully")


else:

allBid.clear()

messagebox.showinfo('Message',"Please check the book ID")

root.destroy()

return

except:

messagebox.showinfo("Search Error","The value entered is wrong, Try again")

allBid.clear()

root.destroy()

def returnBook():

global bookInfo1,SubmitBtn,quitBtn,Canvas1,con,cur,root,labelFrame, lb1

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)
headingLabel = Label(headingFrame1, text="Return Book", bg='black', fg='white', font=('Courier',15))

headingLabel.place(relx=0,rely=0, relwidth=1, relheight=1)

labelFrame = Frame(root,bg='black')

labelFrame.place(relx=0.1,rely=0.3,relwidth=0.8,relheight=0.5)

# Book ID to Delete

lb1 = Label(labelFrame,text="Book ID : ", bg='black', fg='white')

lb1.place(relx=0.05,rely=0.5)

bookInfo1 = Entry(labelFrame)

bookInfo1.place(relx=0.3,rely=0.5, relwidth=0.62)

#Submit Button

SubmitBtn = Button(root,text="Return",bg='#d1ccc0', fg='black',command=returnn)

SubmitBtn.place(relx=0.28,rely=0.9, relwidth=0.18,relheight=0.08)

quitBtn = Button(root,text="Quit",bg='#f7f1e3', fg='black', command=root.destroy)

quitBtn.place(relx=0.53,rely=0.9, relwidth=0.18,relheight=0.08)

root.mainloop()

You might also like