Project 1 - English Dictionary App & Library Book Management System

from tkinter import * # for gui
import sqlite3 # for sql commands
import tkinter.ttk as ttk
import tkinter.messagebox as tkMessageBox # for messagebox
root=Tk()
root.title('LIBRARY MANAGEMENT SYSTEM')
# Methods
def Database(): # function to access database
global conn,cursor
conn=sqlite3.connect('LIBRARY.db')
cursor=conn.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS book(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, Booktitle TEXT, Author TEXT,Year INTEGER,ISBN INTEGER)")
def Create():
if BOOKTITLE.get()=="" and AUTHOR.get()=="" and YEAR.get()=="" and ISBN.get()=="":
txt_result.config(text='Please enter all the fields',fg="red")
else:
Database()
cursor.execute("INSERT INTO book(Booktitle, Author, Year,ISBN) VALUES(?,?,?,?)",(str(BOOKTITLE.get()),str(AUTHOR.get()),str(YEAR.get()),str(ISBN.get())))
conn.commit()
BOOKTITLE.set("")
AUTHOR.set("")
YEAR.set("")
ISBN.set("")
cursor.close()
conn.close()
txt_result.config(text='Record Inserted',fg='green')
def Read():
tree.delete(*tree.get_children())
Database()
cursor.execute("SELECT * FROM book ORDER BY Booktitle ASC")
fetch=cursor.fetchall()
for data in fetch:
tree.insert("",'end',values=(data[1],data[2],data[3],data[4]))
cursor.close()
txt_result.config(text="Successfully fetched data !",fg='green')
def delete():
tree.delete(*tree.get_children())
Database()
if BOOKTITLE.get()=="" or AUTHOR.get()==""or YEAR.get()=="" or ISBN.get()=="":
txt_result.config(text='deleting',fg="red")
else:
cursor.execute(f"DELETE FROM book WHERE Booktitle= '{BOOKTITLE.get()}' ")
txt_result.config(text='deleted',fg="red")
conn.commit()
BOOKTITLE.set("")
AUTHOR.set("")
YEAR.set("")
ISBN.set("")
cursor.close()
conn.close()
def issue():
Database()
tree.delete(*tree.get_children())
Database()
if BOOKTITLE.get()=="" or AUTHOR.get()==""or YEAR.get()=="" or ISBN.get()=="":
txt_result.config(text='cannot issue the book',fg="red")
BOOKTITLE.set("")
AUTHOR.set("")
YEAR.set("")
ISBN.set("")
cursor.close()
conn.close()
else:
txt_result.config(text="your book is issued please collect ",fg="green")
BOOKTITLE.set("")
AUTHOR.set("")
YEAR.set("")
ISBN.set("")
cursor.close()
conn.close()
def search():
tree.delete(*tree.get_children())
Database()
if BOOKTITLE.get()=="" or AUTHOR.get()==""or YEAR.get()=="" or ISBN.get()=="":
txt_result.config(text='Book not found with the provided ',fg="red")
BOOKTITLE.set("")
AUTHOR.set("")
YEAR.set("")
ISBN.set("")
cursor.close()
conn.close()
else:
tree.delete(*tree.get_children())
Database()
cursor.execute(f"SELECT * FROM book WHERE book.Booktitle=={BOOKTITLE.get()}")
fetch=cursor.fetchall()
for data in fetch:
tree.insert("",'end',values=(data[1],data[2],data[3],data[4]))
cursor.close()
txt_result.config(text="Successfully SEARCHED data !",fg='green')
BOOKTITLE.set("")
AUTHOR.set("")
YEAR.set("")
ISBN.set("")
cursor.close()
conn.close()
def Exit():
result=tkMessageBox.askquestion('Do you want to exit? (y/n)',icon='warning')
if result=='yes':
root.destroy()
exit()
# variable declaration
BOOKTITLE=StringVar()
AUTHOR=StringVar()
YEAR=StringVar()
ISBN=StringVar()
# Frame
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=300,height=450)
Forms.pack(side=TOP)
Buttons=Frame(Left,width=300,height=100,bd=8,relief='raise')
Buttons.pack(side=BOTTOM)
# Labels
txt_title=Label(Top,width=900,text='LIBRARY Application')
txt_title.pack()
txt_firstname=Label(Forms,text='Bookname:',font=('arial',16),bd=15)
txt_firstname.grid(row=0,stick="e")
txt_lastname=Label(Forms,text='Author: ',font=('arial',16),bd=15)
txt_lastname.grid(row=1,stick="e")
txt_gender=Label(Forms,text='Year: ',font=('arial',16),bd=15)
txt_gender.grid(row=2,stick="e")
txt_address=Label(Forms,text='ISBN',font=('arial',16),bd=15)
txt_address.grid(row=3,stick="e")
txt_result=Label(Buttons)
txt_result.pack(side=TOP)
# Entry Widget
booktitle=Entry(Forms,textvariable=BOOKTITLE,width=30)
booktitle.grid(row=0,column=1)
author=Entry(Forms,textvariable=AUTHOR,width=30)
author.grid(row=1,column=1)
# RadioGroup.grid(row=2,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_create=Button(Buttons,width=6,text='Add',command=Create)
btn_create.pack(side=LEFT)
btn_read=Button(Buttons,width=6,text='Read',command=Read)
btn_read.pack(side=LEFT)
btn_update=Button(Buttons,width=6,text='Issue',command=issue)
btn_update.pack(side=LEFT)
btn_search=Button(Buttons,width=6,text='Search',command=search)
btn_search.pack(side=LEFT)
btn_delete=Button(Buttons,width=6,text='Delete',command=delete)
btn_delete.pack(side=LEFT)
btn_exit=Button(Buttons,width=6,text='Exit',command=Exit)
btn_exit.pack(side=LEFT)
# List Widget
scrollbary=Scrollbar(Right,orient=VERTICAL)
scrollbarx=Scrollbar(Right,orient=HORIZONTAL)
tree=ttk.Treeview(Right,columns=("Booktitle","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('Booktitle',text="Booktitle",anchor=W)
tree.heading('Author',text="Author",anchor=W)
tree.heading('Year',text="Year",anchor=W)
tree.heading('isbn',text="isbn",anchor=W)
# tree.heading('Username',text="Username",anchor=W)
# tree.heading('Password',text="Password",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=80)
tree.column('#4',stretch=NO,minwidth=0,width=150)
# tree.column('#5',stretch=NO,minwidth=0,width=120)
# tree.column('#6',stretch=NO,minwidth=0,width=120)
tree.pack()
# Initialisation
if __name__=='__main__':
root.mainloop()