Challenge

Project 1 - English Dictionary App & Library Book Management System

import json
from difflib import get_close_matches

data = json.load(open("dictionary.json"))

def meaning(w):

#data = json.load(open("dictionary.json"))

w = w.lower()
if w in data:
return data[w]
elif len(get_close_matches(w, data.keys())) > 0:
return data[get_close_matches(w, data.keys())[0]]
else:
return "The word doesn't exist."

def add_word():

class_list=dict()
data=input("Enter the value:")
temp=data.split(':')
class_list[temp[0]]=(temp[1])
f=open('dictionary.json','a')
student=(class_list)
json.dump(class_list,f)
f.close()

from tkinter import *
import tkinter.ttk as ttk
import tkinter.messagebox as tkmessagebox
import sqlite3
root=Tk()

def Databasetable():
global conn,cursor
conn=sqlite3.connect("Library.db")
cursor=conn.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS libarary(booktitle TEXT,author TEXT,year INTEGER,ISBN INTEGER)")

def add_new_book():
Databasetable()
if booktitle.get()!="" and author.get()!="" and year.get()!="" and ISBN.get()!="":
cursor.execute("INSERT INTO libarary VALUES(?,?,?,?)",(booktitle.get(),author.get(),int(year.get()),int(ISBN.get())))
conn.commit()

conn.close()
text_result.config(text="Book is added successfully!",fg="green")
else:
text_result.config(text="Please enter all the values to add the book!",fg="red")

def Display():
tree.delete(*tree.get_children())
Databasetable()
cursor.execute("SELECT * FROM libarary ORDER by booktitle ASC")
fetch=cursor.fetchall()
for data in fetch:
tree.insert('','end',values=(data[0],data[1],data[2],data[3]))
cursor.close()
text_result.config(text="successfully fetched",fg='black')

def delete_book():
Databasetable()
#if booktitle.get()!="" and author.get()!="" and year.get()!="" and ISBN.get()!="":
if BOOKTITLE.get()=="" or AUTHOR.get()=="" or YEAR.get()=="" or ISBN.get()=="":
text_result.config(text="Please enter all Entries!",fg="red")
delete()
else:
book='"'+str(BOOKTITLE.get())+'"'
author='"'+str(AUTHOR.get())+'"'
y=str(YEAR.get())
I=str(ISBN.get())
temp="DELETE FROM libarary WHERE booktitle LIKE "+book+" and author LIKE "+author+" and year = "+y+" and ISBN = "+I
cursor.execute(temp)
conn.commit()
text_result.config(text="Book deleted successfully!",fg='green')
delete()
conn.close()

def delete():

booktitle.delete(0,END)
author.delete(0,END)
year.delete(0,END)
ISBN.delete(0,END)


def issue_the_book():
Databasetable()
selected_item=tree.selection()
if len(selected_item)>0:
tree.delete(selected_item)
text_result.config(text="Selected book is issued!",fg="green")
else:
text_result.config(text="Please select a book from library to issue!",fg="red")
delete()
conn.close()

def Search_book():
tree.delete(*tree.get_children())
Databasetable()
if str(booktitle.get())!="":
name="'"+str(booktitle.get()).title()+"'"
temp="SELECT * FROM libarary WHERE booktitle = "+name
cursor.execute(temp)
fetch=cursor.fetchall()
for data in fetch:
tree.insert('','end',values=(data[0],data[1],data[2],data[3]))
text_result.config(text="Please see the books!",fg="green")
elif str(author.get())!="":
name="'"+str(author.get()).title()+"'"
temp="SELECT * FROM libarary WHERE author = "+name
cursor.execute(temp)
fetch=cursor.fetchall()
for data in fetch:
tree.insert('','end',values=(data[0],data[1],data[2],data[3]))
text_result.config(text="Please see the books!",fg="green")

elif str(year.get())!="":
temp="SELECT * FROM libarary WHERE year = "+str(year.get())
cursor.execute(temp)
fetch=cursor.fetchall()
for data in fetch:
tree.insert('','end',values=(data[0],data[1],data[2],data[3]))
text_result.config(text="Please see the books!",fg="green")
elif str(ISBN.get())!="":
temp="SELECT * FROM libarary WHERE ISBN = "+str(ISBN.get())
cursor.execute(temp)
fetch=cursor.fetchall()
for data in fetch:
tree.insert('','end',values=(data[0],data[1],data[2],data[3]))
text_result.config(text="Please see the books!",fg="green")
else:
text_result.config(text="Please enter at least single identity!",fg="red")
delete()
conn.close()


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

frame1=Frame(root,width=1000,height=100,bd=8,bg='red',relief='raise')
frame1.pack(side=TOP)
#frame2=Frame(root,width=300,height=800,bd=15,bg='red',relief='raise')
#frame2.pack(side=LEFT)
top=Label(frame1,width=900,text='Libarary Managment System')
top.pack()

Left=Frame(root,width=800,height=400,bd=8,relief='raise',bg='red')
Left.pack(side=LEFT)
frame2=Frame(Left,width=300,height=550)
frame2.pack(side=TOP)


Buttons=Frame(Left,width=300,height=100,bd=8,relief='raise')
Buttons.pack(side=BOTTOM)

Right=Frame(root,width=1000,height=500,bd=8,relief='raise')
Right.pack(side=RIGHT)
text_result=Label(Buttons)
text_result.pack(side=TOP)


booktitle=Label(frame2,text='Booktitle: ',bd=15)
booktitle.grid(row=0,column=1)
author=Label(frame2,text='Author: ',bd=15)
author.grid(row=1,column=1)
year=Label(frame2,text='Year: ',bd=15)
year.grid(row=2,column=1)
ISBN=Label(frame2,text='ISBN: ',bd=15)
ISBN.grid(row=3,column=1)


BOOKTITLE=StringVar()
AUTHOR=StringVar()
YEAR=IntVar()
ISBN=IntVar()


booktitle=Entry(frame2,textvariable=BOOKTITLE,width=30)
booktitle.grid(row=0,column=2)
author=Entry(frame2,textvariable=AUTHOR,width=30)
author.grid(row=1,column=2)
year=Entry(frame2,textvariable=YEAR,width=30)
year.grid(row=2,column=2)
ISBN=Entry(frame2,textvariable=ISBN,width=30)
ISBN.grid(row=3,column=2)

btn_create=Button(Buttons,width=10,text="Display",command=Display)
btn_create.pack(side=LEFT)
btn_read=Button(Buttons,width=10,text="Search",command=Search_book)
btn_read.pack(side=LEFT)
btn_update=Button(Buttons,width=10,text="Add",command=add_new_book)
btn_update.pack(side=LEFT)
btn_delete=Button(Buttons,width=10,text="Issue",command=issue_the_book)
btn_delete.pack(side=LEFT)
btn_delete=Button(Buttons,width=10,text="Delete",command=delete_book)
btn_delete.pack(side=LEFT)
btn_exit=Button(Buttons,width=10,text="Exit",command=Exit)
btn_exit.pack(side=LEFT)
cols=('Booktitle','Author','Year','ISBN')
tree=ttk.Treeview(Right,columns=cols)
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.column('#0',stretch=NO,minwidth=0,width=0)
tree.column('#1',stretch=NO,minwidth=0,width=180)
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.pack()

if __name__=='__main__':
root.mainloop()


def update(key_value,value):
with open('dictionary.json','r+') as f:
data=json.load(f)
data[key_value]=[value]

f=open('dictionary.json','w+')
json.dump(data,f)
f.close()

#while
while True:
print('1.Find the meaning')
print('2.Add new word')
print('3.Update a word')
print('4.Exit')

choice=int(input('Enter your choice'))
if choice==1:
value = input("Enter word: ")
result = meaning(value)
print(result)
break

elif choice==2:
add_word()
break

elif choice==3:
key_value=input('Enter word')
value=input('Enter meaning')
result=update(key_value,value)
break
elif choice==4:
print('Exit')
break
else:
print("Sorry! you enter wrong choice")

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.

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.