Challenge

Project 1 B - Library Book Management System

In this project, a Library Book Management System will be implemented with the front end GUI developed using Tkinter and the backend database implemented in SQLite. It is a single window tkinter GUI application which will implement CRUD( create, read, update, delete) operations on a ‘books’ database, in SQLite.

import sqlite3 as sql
import tkinter as tk
import tkinter.ttk as ttk
import tkinter.messagebox as mb
import tkinter.simpledialog as sd
from tkinter import *


root=Tk()
root.title('.....Library Management Application.....')


def Database():
global conn,cursor
conn=sql.connect('simple.db')
cursor=conn.cursor()
cursor.execute('CREATE TABLE IF NOT EXISTS Booklibrary(Bookname TEXT,Author TEXT,Year TEXT, ISBN TEXT)')

def addbook():
if Bookname.get()=="" or Author.get()=="" or Year.get() =="" or ISBN.get()=="":
tk.messagebox.showinfo(text='Please Enter all the fields',fd='red')
else:
Database()
cursor.execute('INSERT INTO Booklibrary(Bookname,Author,Year,ISBN)VALUES(?,?,?,?)',(str(Bookname.get()),str(Author.get()),str(Year.get()),str(ISBN.get())))
conn.commit()
Bookname.set("")
Author.set("")
Year.set("")
ISBN.set("")
cursor.close()
conn.close()
tk.messagebox.showinfo(text='Record Inserted !',fg='green')

def displaybook():
#tree.delete(*tree.get_children())
Database()
cursor.execute("SELECT * FROM Booklibrary ORDER BY Bookname ASC")
fetch=cursor.fetchall()
for data in fetch:
tree.insert('','end',values=(data[0],data[1],data[2],data[3]))
cursor.close()
conn.close
tk.messagebox.showinfo(text='Successfully fetched data !',fg='black')

def searchbook():
tree.delete(*tree.get_children())
Database()
cursor.execute('SELECT * FROM Booklibrary')
fetch=cursor.fetchall()
count=0
for data in fetch:
if Bookname.get()==data[0] and Author.get()==data[1] and Year.get() ==data[2] and ISBN.get()==data[3]:
tree.insert('','end',values=(data[0],data[1],data[2],data[3]))
count+=1
elif Bookname.get()==data[0]:
tree.insert('','end',values=(data[0],data[1],data[2],data[3]))
count+=1
elif Author.get()==data[1]:
tree.insert('','end',values=(data[0],data[1],data[2],data[3]))
count+=1
elif Year.get==data[2]:
tree.insert('','end',values=(data[0],data[1],data[2],data[3]))
count+=1
elif ISBN.get()==data[3]:
tree.insert('','end',values=(data[0],data[1],data[2],data[3]))
count+=1
if count==0:
txt_result.config(text='No Data Found !')
cursor.close()
conn.close()
Bookname.set("")
Author.set("")
Year.set("")
ISBN.set("")
tk.messagebox.showinfo(text='Successfully fetched data !')

def issuebook():
tree.delete(*tree.get_children())
Database()
cursor.execute('SELECT * FROM Booklibrary')
fetch=cursor.fetchall()
cursor.execute('Delete FROM Booklibrary')
count=0
for data in fetch:
if Bookname.get()==data[0] and Author.get()==data[1] and Year.get() ==data[2] and ISBN.get()==data[3]:
if count==0:
count+=1
txt_result.config(text='Successfully Book Issued !')
else:
cursor.execute('INSERT INTO Booklibrary(Bookname,Author,Year,ISBN)VALUES(?,?,?,?)',(str(Bookname.get()),str(Author.get()),str(Year.get()),str(ISBN.get())))
conn.commit()
else:
cursor.execute('INSERT INTO Booklibrary(Bookname,Author,Year,ISBN)VALUES(?,?,?,?)',(str(Bookname.get()),str(Author.get()),str(Year.get()),str(ISBN.get())))
conn.commit()
if count==0:
txt_result.config(text='No Book avilable of this details !')
cursor.close()
conn.close()


def deletebook():
tree.delete(*tree.get_children())
database()
cursorobj.execute("SELECT * FROM Booklibrary")
fetch=cursor.fetchall()
count=0
for data in fetch:
if Bookname.get()==data[0] and Author.get()==data[1] and Year.get() ==data[2] and ISBN.get()==data[3]:
tempb = ("'"+str(Bookname)+"'")
tempa = ("'"+str(Author)+"'")
tempp = ("'"+str(Year)+"'")
tempi = ("'"+str(ISBN)+"'")
temp1 ="DELETE FROM Booklibrary WHERE Bookname = "+tempb +"and Author= "+tempa +"and Year = "+tempp +"and ISBN= "+tempi
cursor.execute(temp1)
con.commit()
count+=1
if(count==0):
tk.messagebox.showinfo('No data with this name in the library')
else:
tk.messagebox.showinfo('Book no longer available in the library')
Bookname.set("")
Author.set("")
Year.set("")
ISBN.set("")
cursor.close()
conn.close()

def Exit():
result=mb.askquestion('Do you want to Exit ?(y/n)',icon='warning')
if result == 'yes':
root.destroy()
exit()


#variables..
BOOKNAME=StringVar()
AUTHOR=StringVar()
YEAR=StringVar()
ISBN=StringVar()

#FRAMES
Top=Frame(root,width=900,height=50,bd=8,relief='raise')
Top.pack(side=TOP)
Left=Frame(root,width=300,height=500,bd=8,relief='raise')
Left.pack(side=LEFT)
Right=Frame(root,width=600,height=500,bd=8,relief='raise')
Right.pack(side=RIGHT)


Forms=Frame(Left,width=500,height=800)
Forms.pack(side="top")

#buttons
Buttons=Frame(Left,width=300,height=100,bd=8,relief='raise')
Buttons.pack(side="bottom")

#Labels
txt_title=Label(Top,width=900,font=('arial',24),text='.....Library Management Application.....')
txt_title.pack()
txt_Bookname=Label(Forms,text='Bookname',font=('arial',16),bd=15)
txt_Bookname.grid(row=0,stick='e')
txt_Author=Label(Forms,text='Author',font=('arial',16),bd=15)
txt_Author.grid(row=1,stick='e')
txt_Year=Label(Forms,text='Year',font=('arial',16),bd=15)
txt_Year.grid(row=2,stick='e')
txt_ISBN=Label(Forms,text='ISBN',font=('arial',16),bd=15)
txt_ISBN.grid(row=3,stick='e')
txt_result=Label(Buttons)
txt_result.pack(side=TOP)

#Entry widgets
Bookname=Entry(Forms,textvariable=BOOKNAME,width=30)
Bookname.grid(row=0,column=1)
Author=Entry(Forms,textvariable=AUTHOR,width=30)
Author.grid(row=1,column=1)
Year=Entry(Forms,textvariable=YEAR,width=30)
Year.grid(row=2,column=1)
ISBN=Entry(Forms,textvariable=ISBN,width=30)
ISBN.grid(row=3,column=1)

#Button widgets
btn_display=Button(Buttons,width=10,text="Display",command=displaybook)
btn_display.pack(side="left")
btn_search=Button(Buttons,width=10,text="Search",command=searchbook)
btn_search.pack(side="left")
btn_add=Button(Buttons,width=10,text="Add",command=addbook)
btn_add.pack(side="left")
btn_issue=Button(Buttons,width=10,text="Issue",command=issuebook)
btn_issue.pack(side="left")
btn_delete=Button(Buttons,width=10,text="Delete",command=deletebook)
btn_delete.pack(side="left")
btn_exit=Button(Buttons,width=10,text="Exit",command=Exit)
btn_exit.pack(side="left")


#list widgets
scrollbary=Scrollbar(Right,orient=VERTICAL)
scrollbarx=Scrollbar(Left,orient=HORIZONTAL)

tree=ttk.Treeview(Right,columns=('Book','Author','Year','Isbn'))

scrollbary.config(command=tree.yview)
scrollbary.pack(side="right",fill=Y)
scrollbarx.config(command=tree.xview)
scrollbarx.pack(side="bottom",fill=X)
tree.heading('Book',text='Book',anchor=W)
tree.heading('Author',text='Author',anchor=W)
tree.heading('Year',text='Year',anchor=W)
tree.heading('Isbn',text='ISBN',anchor=W)
tree.column('#0',stretch=NO,minwidth=0,width=0)
tree.column('#1',stretch=NO,minwidth=0,width=80)
tree.column('#2',stretch=NO,minwidth=0,width=120)
tree.column('#3',stretch=NO,minwidth=0,width=160)
tree.pack()
#instaaliztion
root.mainloop()

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.