Project 1 - English Dictionary App & Library Book Management System
Q1
import json
print('Welcome To The DICTIONARY Website')
while True:
print('1. ADD A WORD')
print('2. Search A Word')
print('3. Update A Word')
print('4. Exit')
choice = int(input('Enter Your Choice: '))
if choice == 1:
add()
elif choice == 2:
search()
elif choice == 3:
update()
elif choice == 4:
break
else:
print('Invalid Choice')
print('Thank You For Using The App')
def add():
try:
with open('Words.txt', 'r') as file_obj:
dict1 = json.load(file_obj)
except FileNotFoundError:
dict1 = {}
key = input('Add A New Word: ')
value = input('Write The Meaning: ')
dict1[key] = value
with open('Words.txt', 'w') as file_obj:
json.dump(dict1, file_obj, indent=4)
print('Successfully Added A Word')
def search():
search_word = input('Enter a Word To Search: ')
with open('Words.txt', 'r') as file_obj:
data = json.load(file_obj)
meaning = data.get(search_word, "Word not found.")
print(meaning)
def update():
with open('Words.txt', 'r') as file_obj:
dict1 = json.load(file_obj)
updating = input('Enter The Word: ')
meaning = input('Enter the Meaning that you want to update: ')
if updating in dict1:
dict1[updating] = meaning
with open('Words.txt', 'w') as file_obj1:
json.dump(dict1, file_obj1)
Q2
from tkinter import *
from sqlite3 import *
import tkinter.ttk as ttk
import tkinter.messagebox as tkMassgeBox
def Database():
global conn,cursor
conn=connect("Library.db")
cursor=conn.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS lbms(author_name TEXT,book_title TEXT,year INTEGER,ISBN INTEGER)")
def display_all_records():
present.delete(*present.get_children())
Database()
cursor.execute("SELECT * FROM lbms ORDER by book_title ASC")
fetch=cursor.fetchall()
for data in fetch:
present.insert('','end',values=(data[0],data[1],data[2],data[3]))
conn.close()
Ll1.config(text="Please see the books!",fg="green")
def add_new_book():
Database()
if author_name.get(1.0,"end-1c")!="" and book_title.get(1.0,"end-1c")!="" and year.get(1.0,"end-1c")!="" and ISBN.get(1.0,"end-1c")!="":
cursor.execute("INSERT INTO lbms VALUES(?,?,?,?)",(author_name.get(1.0,"end-1c"),book_title.get(1.0,"end-1c"),int(year.get(1.0,"end-1c")),int(ISBN.get(1.0,"end-1c"))))
conn.commit()
delete()
conn.close()
Ll1.config(text="Book is added successfully!",fg="green")
else:
Ll1.config(text="Please enter all the values to add the book!",fg="red")
delete()
def Search_book():
present.delete(*present.get_children())
Database()
if str(author_name.get(1.0,"end-1c"))!="":
name="'"+str(author_name.get(1.0,"end-1c")).title()+"'"
temp="SELECT * FROM lbms WHERE author_name = "+name
cursor.execute(temp)
fetch=cursor.fetchall()
for data in fetch:
present.insert('','end',values=(data[0],data[1],data[2],data[3]))
Ll1.config(text="Please see the books!",fg="green")
elif str(book_title.get(1.0,"end-1c"))!="":
name="'"+str(book_title.get(1.0,"end-1c")).title()+"'"
temp="SELECT * FROM lbms WHERE book_title = "+name
cursor.execute(temp)
fetch=cursor.fetchall()
for data in fetch:
present.insert('','end',values=(data[0],data[1],data[2],data[3]))
Ll1.config(text="Please see the books!",fg="green")
elif str(year.get(1.0,"end-1c"))!="":
temp="SELECT * FROM lbms WHERE year = "+str(year.get(1.0,"end-1c"))
cursor.execute(temp)
fetch=cursor.fetchall()
for data in fetch:
present.insert('','end',values=(data[0],data[1],data[2],data[3]))
Ll1.config(text="Please see the books!",fg="green")
elif str(ISBN.get(1.0,"end-1c"))!="":
temp="SELECT * FROM lbms WHERE ISBN = "+str(ISBN.get(1.0,"end-1c"))
cursor.execute(temp)
fetch=cursor.fetchall()
for data in fetch:
present.insert('','end',values=(data[0],data[1],data[2],data[3]))
Ll1.config(text="Please see the books!",fg="green")
else:
Ll1.config(text="Please enter at least single identity!",fg="red")
delete()
conn.close()
def issue_the_book():
Database()
selected_item=present.selection()
if len(selected_item)>0:
present.delete(selected_item)
Ll1.config(text="Selected book is issued!",fg="green")
else:
Ll1.config(text="Please select a book from library to issue!",fg="red")
delete()
conn.close()
def delete_book():
Database()
if author_name.get(1.0,"end-1c")=="" or book_title.get(1.0,"end-1c")=="" or year.get(1.0,"end-1c")=="" or ISBN.get(1.0,"end-1c")=="":
Ll1.config(text="Please enter all Entries!",fg="red")
delete()
else:
author='"'+str(author_name.get(1.0,"end-1c"))+'"'
book='"'+str(book_title.get(1.0,"end-1c"))+'"'
y=str(year.get(1.0,"end-1c"))
I=str(ISBN.get(1.0,"end-1c"))
temp="DELETE FROM lbms WHERE author_name LIKE "+author+" and book_title LIKE "+book+" and year = "+y+" and ISBN = "+I
cursor.execute(temp)
conn.commit()
Ll1.config(text="Book deleted successfully!",fg='green')
delete()
conn.close()
def delete():
author_name.delete("1.0","end")
book_title.delete("1.0","end")
year.delete("1.0","end")
ISBN.delete("1.0","end")
def Exit():
result=tkMassgeBox.askquestion("Do you want to exit? (y/n)",icon="warning")
if result=="yes":
root.destroy()
exit()
root=Tk()
root.geometry("900x400")
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)
inner_upper=Frame(Left,width=300,height=250,bd=8,relief="raise")
inner_upper.pack(side=TOP)
L1=Label(Top,text="!!! Library Book Management System !!!",font=("arial",20))
L1.pack()
L2=Label(inner_upper,text="Author Name:",font=("arial",14))
L3=Label(inner_upper,text="Book Name:",font=("arial",14))
L4=Label(inner_upper,text="Published year:",font=('arial',14))
L5=Label(inner_upper,text="ISBN:",font=('arial',14))
author_name=Text(inner_upper,height=2,width=40)
book_title=Text(inner_upper,height=2,width=40)
year=Text(inner_upper,height=2,width=40)
ISBN=Text(inner_upper,height=2,width=40)
L2.grid(row=0,column=0)
author_name.grid(row=0,column=1)
L3.grid(row=1,column=0)
book_title.grid(row=1,column=1)
L4.grid(row=2,column=0)
year.grid(row=2,column=1)
L5.grid(row=3,column=0)
ISBN.grid(row=3,column=1)
Ll1=Label(Right)
Ll1.pack(side=BOTTOM)
inner_frame=Frame(Left,width=250,height=50,bd=8,relief='raise')
inner_frame.pack(side=BOTTOM)
Bb1=Button(inner_frame,text="View Library",command=display_all_records)
Bb1.grid(row=0,column=0)
Bb2=Button(inner_frame,text="Search",command=Search_book)
Bb2.grid(row=0,column=1)
Bb3=Button(inner_frame,text="Add New Book",command=add_new_book)
Bb3.grid(row=0,column=2)
Bb4=Button(inner_frame,text="Issue the Book",command=issue_the_book)
Bb4.grid(row=0,column=3)
Bb5=Button(inner_frame,text="Delete the Book",command=delete_book)
Bb5.grid(row=0,column=4)
Bb6=Button(inner_frame,text="Exit",command=Exit)
Bb6.grid(row=0,column=5)
scrollbary=Scrollbar(Right,orient=VERTICAL)
scrollbarx=Scrollbar(Right,orient=HORIZONTAL)
present = ttk.Treeview(Right)
scrollbary.config(command=present.yview)
scrollbary.pack(side=RIGHT,fill=Y)
scrollbarx.config(command=present.xview)
scrollbarx.pack(side=BOTTOM,fill=X)
present['columns']=("Author Name","Book Name","Published year","ISBN no")
present.column('#0', width=0, stretch=NO)
present.column('Author Name', anchor=CENTER, width=100)
present.column('Book Name', anchor=CENTER, width=100)
present.column('Published year', anchor=CENTER, width=100)
present.column('ISBN no', anchor=CENTER, width=100)
present.heading('#0', text='', anchor=CENTER)
present.heading('Author Name', text='Author Name', anchor=CENTER)
present.heading('Book Name', text='Book Name', anchor=CENTER)
present.heading('Published year', text='Published year', anchor=CENTER)
present.heading('ISBN no', text='ISBN no', anchor=CENTER)
present.pack()
root.mainloop()