Challenge

Project 1 - English Dictionary App & Library Book Management System

1A

1B

from tkinter import *
import tkinter as tk
import sqlite3
import tkinter.ttk as ttk
import tkinter.messagebox as tkMessageBox

def database():
global con,cursor
con = sqlite3.connect('books.db')
cursor = con.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS librarytable (bookname TEXT, author TEXT, yearofpublication INTEGER, isbn INTEGER)")

def displaybook():
tree.delete(*tree.get_children())
database()
cursor.execute("SELECT * FROM librarytable ORDER BY bookname ASC")
fetch=cursor.fetchall()
count = 0

for data in fetch:
tree.insert('','end',values=(data[0],data[1],data[2],data[3]))
cursor.close()
con.close()

tk.messagebox.showinfo(message="Successfully fetched data !")

def searchbook():
title_bookname = ((bookname.get()).title())
title_author = ((author.get()).title())
title_yearofpublication = ((yearofpublication.get()).title())
title_isbn = ((isbn.get()).title())
tree.delete(*tree.get_children())
database()

cursor.execute("SELECT * FROM librarytable")
fetch =cursor.fetchall()

count = 0

data = [title_bookname,title_author,title_yearofpublication,title_isbn]

for data in fetch:
if(title_bookname == data[0]) and (title_author == data[1]) and (str(title_yearofpublication == str(data[2]))) and (str(title_isbn == str(data[3]))):
tree.insert('','end',values=(data[0],data[1],data[2],data[3]))
count+=1

elif(title_bookname == data[0]):
tree.insert('','end',values=(data[0],data[1],data[2],data[3]))
count+=1

elif(title_author == data[1]):
tree.insert('','end',values=(data[0],data[1],data[2],data[3]))
count+=1

elif(title_yearofpublication == str(data[2])):
tree.insert('','end',values=(data[0],data[1],data[2],data[3]))
count+=1

elif(str(title_isbn) == str(data[3])):
tree.insert('','end',values=(data[0],data[1],data[2],data[3]))
count+=1

if (count==0):
tk.messagebox.showinfo(message='Book not found')
cursor.close()

bookname.set("")
author.set("")
yearofpublication.set("")
isbn.set("")

con.close()

def refresh():
tree.delete(*tree.get_children())
database()
cursor.execute("SELECT * FROM librarytable ORDER BY bookname ASC")
fetch=cursor.fetchall()
count = 0

for data in fetch:
tree.insert('','end',values=(data[0],data[1],data[2],data[3]))
cursor.close()
con.close()

def addbook():

database()
title_bookname = (((bookname.get()).title()).strip())
title_author = (((author.get()).title()).strip())
title_yearofpublication = str((yearofpublication.get()).strip())
title_isbn =str ((isbn.get()).strip())

if ((title_bookname =='') or (title_author =='') or (title_yearofpublication =='') or (title_isbn) ==''):
tk.messagebox.showinfo(message='Please fill all the details in the library')

else:

cursor.execute("INSERT INTO librarytable(bookname, author, yearofpublication, isbn) VALUES(?,?,?,?)",
(title_bookname, title_author, title_yearofpublication, title_isbn))
con.commit()

bookname.set("")
author.set("")
yearofpublication.set("")
isbn.set("")

con.close()
refresh()
tk.messagebox.showinfo(message='Values inserted in the library')

def click_fetch():

select_item = tree.selection()
#print (tree.item(select_item)['values'])

for name in select_item:
name = tree.item(select_item)
record=name['values']

bookname.set(record[0])
author.set(record[1])
yearofpublication.set(record[2])
isbn.set(record[3])

def deletebook():

database()
click_fetch()
title_bookname = (((bookname.get()).title()).strip())
title_author = (((author.get()).title()).strip())
title_yearofpublication = str((yearofpublication.get()).strip())
title_isbn =str ((isbn.get()).strip())

if title_bookname==("") or title_author==("") or title_yearofpublication==("") or title_isbn==(""):
tk.messagebox.showinfo(message='Please enter values to delete!')

else:
database()
cursor.execute("SELECT * FROM librarytable")
fetch=cursor.fetchall()

for data in fetch:

cursor.execute("DELETE FROM librarytable WHERE bookname=? and author=? and yearofpublication=? and isbn=?",
(title_bookname,title_author,title_yearofpublication,title_isbn))
con.commit()

bookname.set("")
author.set("")
yearofpublication.set("")
isbn.set("")
con.close()
refresh()

tk.messagebox.showinfo(message='Book has been successfully deleted!')

def issuebook():

database()
select_item = tree.selection()

if len(select_item) > 0:
tree.delete(select_item)
tk.messagebox.showinfo(message="Selected book is issued !")
else:
tk.messagebox.showinfo(message="Please select book from library to issue !")

cursor.close()
con.close()

def Exit():

result = tkMessageBox.askquestion(message='Do you want to exit from library?', icon='warning')

if(result=='yes'):
root.destroy()

else:
tk.messagebox.showinfo(message='You will now return to the application screen of the library')

root=Tk()
root.geometry("1100x600")

root.title("A Library Application!")
root.config(background="#f0f0f0")

#=============================Frames==================================================================

border_color = Frame(root,background="blue",bd=8,relief=RAISED)

head_label = Label(border_color, width=1000,text=".....Library Management Application.....",font=('Arial',40,'bold'),bd=8)

head_label.pack(padx=5,pady=5)
border_color.pack()

LeftFrame=Frame(root,bd=8,width=350,height=520,padx=10,relief=RAISED,bg='red')
LeftFrame.pack(side=LEFT)

ButtonFrame=Frame(LeftFrame,bd=5, height =100, relief=RAISED)
ButtonFrame.pack(side=BOTTOM)

Form=Frame(LeftFrame,bd=8,width=150,height=520,padx=10)
Form.pack()

RightFrame=Frame(root,bd=8,width=300,height=520,relief=RAISED,bg='yellow')
RightFrame.pack(side=RIGHT)

#===========================================Variables===========================================

bookname = StringVar()
author = StringVar()
yearofpublication = StringVar()
isbn = StringVar()

#===========================================Label&Entry===========================================

lbl_bookname = Label(Form,text="Booktitle:",font=('Arial',13,'bold'),anchor="e", justify="right",width=10, padx=5, pady=15)
lbl_bookname.grid(row=0,column=0,sticky='w')

entry_bookname=Entry(Form,textvariable=bookname,font=('Arial',13),width=30)
entry_bookname.grid(row=0,column=1)

lbl_authorname=Label(Form,text="Author :",font=('Arial',13,'bold'),anchor="e", justify="right",width=10, padx=5, pady=15)
lbl_authorname.grid(row=1,column=0,sticky='w')

entry_authorname=Entry(Form,textvariable=author,font=('Arial',13),width=30)
entry_authorname.grid(row=1,column=1)

lbl_publishyear=Label(Form,text="Year :",font=('Arial',13,'bold'),anchor="e", justify="right",width=10, padx=5, pady=15)
lbl_publishyear.grid(row=2,column=0,sticky='w')

entry_publishyear=Entry(Form,textvariable=yearofpublication,font=('Arial',13),width=30)
entry_publishyear.grid(row=2,column=1)

lbl_ISBN=Label(Form,text="ISBN :",font=('Arial',13,'bold'),anchor="e", justify="right",width=10, padx=5, pady=15)
lbl_ISBN.grid(row=3,column=0,sticky='w')

entry_ISBN=Entry(Form,textvariable=isbn,font=('Arial',13),width=30)
entry_ISBN.grid(row=3,column=1)

lbl_button = Label(ButtonFrame)
lbl_button.grid(row=0,column=0)

#===========================================Buttons===========================================

but_view=Button(ButtonFrame,width=10,height=1,text='Display',font=('Arial',10),bd=3,relief=RAISED,command=displaybook)
but_view.grid(row=1,column=0)

but_search=Button(ButtonFrame,width=10,height=1,text='Search',font=('Arial',10),bd=3,relief=RAISED,command=searchbook)
but_search.grid(row=1,column=1)

but_add=Button(ButtonFrame,width=10,height=1,text='Add',font=('Arial',10),bd=3,relief=RAISED,command=addbook)
but_add.grid(row=1,column=2)

but_issue=Button(ButtonFrame,width=10,height=1,text='Issue',font=('Arial',10),bd=3,relief=RAISED,command=issuebook)
but_issue.grid(row=1,column=3)

but_delete=Button(ButtonFrame,width=10,height=1,text='Delete',font=('Arial',10),bd=3,relief=RAISED,command=deletebook)
but_delete.grid(row=1,column=4)

but_exit=Button(ButtonFrame,width=10,height=1,text='Exit',font=('Arial',10),bd=3,relief=RAISED,command=Exit)
but_exit.grid(row=1,column=5)

#===========================================Scrollbar & tree===========================================

col=('Book_Name','Author_Name','Publication_year','ISBN')

tree=ttk.Treeview(RightFrame,columns=col,show='headings')

scrollbary=Scrollbar(RightFrame,orient="vertical",command=tree.yview)
tree.configure(yscrollcommand=scrollbary.set)
scrollbary.pack(side=RIGHT,fill=Y)

tree.heading("Book_Name",text="Booktitle",anchor=W)
tree.heading("Author_Name",text="Author",anchor=W)
tree.heading("Publication_year",text="Year",anchor=W)
tree.heading("ISBN",text="ISBN",anchor=W)

tree.column("Book_Name",minwidth=0,width=150)
tree.column("Author_Name",minwidth=0,width=150)
tree.column("Publication_year",minwidth=0,width=60)
tree.column("ISBN",minwidth=0,width=60)

tree.pack(side=TOP,fill=BOTH)


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.