Project 1 - English Dictionary App & Library Book Management System
1A.This project uses the skills learnt from week 1 to week 6. The aim of this project is to create an English Dictionary app that returns definitions of English words. When the application is started, it displays a menu as follows:
Main Menu
Add a new word
Find the meaning
Update a word
Exit
import json
def main_menu():
print('Main Menu')
print('1.Add a New Word')
print('2.Find the meaning')
print('3.Update a word')
print('4.Exit')
with open('words.txt','w') as D:
dict_words = {}
json.dump(dict_words,D)
main_menu()
while True:
n = int(input('Enter your choice :'))
if n ==1:
with open('words.txt','w') as D:
word = input('Enter a word : ')
meaning = input('Enter the meaning of the word '+word+' :' )
dict_words[word] = meaning
json.dump(dict_words,D)
elif n == 2:
with open('words.txt','r+') as D:
word = input('Enter the word to find its meaning = ')
if word in dict_words:
print('The meaning of the word = ',dict_words.get(word))
else:
print('The word is not present in the dictionary')
json.dump(dict_words,D)
elif n == 3:
with open('words.txt','w') as D:
word1 = input('Enter the word to update:')
word1_meaning = input('Enter the meaning of the word = ')
dict_words[word1]=word1_meaning
json.dump(dict_words,D)
elif n == 4:
print('Exiting the Dictionary ')
break
else:
main_menu()
1B.Library Book Management System
from sqlite3 import *
import sqlite3
from tkinter import *
import tkinter.ttk as ttk
import tkinter.messagebox as tkmessagebox
#-----------------------------------------------------------------------------------------------------
#connecting to Database
#-----------------------------------------------------------------------------------------------------
def Database():
global conn,cursor
conn=sqlite3.connect('LBMS.db')
cursor=conn.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS lbms(book_title TEXT, author_name TEXT, year INTEGER, isbn INTEGER)")
#-----------------------------------------------------------------------------------------------------
#Display the records in the Database
#-----------------------------------------------------------------------------------------------------
def display_records():
tree.delete(*tree.get_children())
Database()
cursor.execute("SELECT * FROM lbms ")
fetch = cursor.fetchall()
for data in fetch:
tree.insert('',"end",values=(data[0],data[1],data[2],data[3]))
conn.close()
txt_result.config(text="These are the books available in the records", fg='red')
#-----------------------------------------------------------------------------------------------------
#Adding the details entered by the user in the Database:
#-----------------------------------------------------------------------------------------------------
def add():
Database()
if book_title.get(1.0,"end-1c")!="" and author_name.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(?,?,?,?)",(book_title.get(1.0,"end-1c").title(),author_name.get(1.0,"end-1c").title(), int(year.get(1.0,"end-1c")),int(isbn.get(1.0,"end-1c"))))
conn.commit()
delete()
conn.close()
txt_result.config(text="Book Details Inserted Successfully",fg = 'red')
else:
txt_result.config(text='Enter all the fields to add the book', fg='magenta')
delete()
#-----------------------------------------------------------------------------------------------------
#Search function call on pressing the search button:
#-----------------------------------------------------------------------------------------------------
def search():
tree.delete(*tree.get_children())
Database()
if 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:
tree.insert('','end',values=(data[0],data[1],data[2],data[3]))
txt_result.config(text="Books available in the records are:",fg='green')
elif 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:
tree.insert('','end',values=(data[0],data[1],data[2],data[3]))
txt_result.config(text="These are the books available in the records",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:
tree.insert("","end",values=(data[0],data[1],data[2],data[3]))
txt_result.config(text="These are the books available in the records",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:
tree.insert("","end",values=(data[0],data[1],data[2],data[3]))
txt_result.config(text="These are the books available in the records",fg='green')
else:
txt_result.config(text="Please enter atleast one entity to search!", fg='red')
delete()
conn.close()
#-----------------------------------------------------------------------------------------------------
#Issue book function call On pressing the Issue button
#-----------------------------------------------------------------------------------------------------
def issue_book():
Database()
selected_item=tree.selection()
if len(selected_item)>0:
tree.delete(selected_item)
txt_result.config(text="The selected book is issued",fg='green')
else:
txt_result.config(text="Select a book from the records available!",fg='red')
delete()
conn.close()
#-----------------------------------------------------------------------------------------------------
#Delete function call On pressing the Delete button
#-----------------------------------------------------------------------------------------------------
def delete_book():
Database()
if book_title.get(1.0,"end-1c")== "" and author_name.get(1.0,"end-1c")=="" and year.get(1.0,"end-1c")=="" and isbn.get(1.0,"end-1c")== "":
txt_result.config(text="Enter all fields!", fg="red")
delete()
else:
book='"'+str(book_title.get(1.0,"end-1c"))+'"'
author='"'+str(author_name.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 book_title = "+book+" and author_name = "+author+" and year = "+y+" and isbn = " +i
cursor.execute(temp)
conn.commit()
txt_result.config(text="Book deleted successfully",fg='green')
delete()
conn.close()
#-----------------------------------------------------------------------------------------------------
#Deleting the previous records
#-----------------------------------------------------------------------------------------------------
def delete():
book_title.delete("1.0","end")
author_name.delete("1.0","end")
year.delete("1.0","end")
isbn.delete("1.0","end")
#-----------------------------------------------------------------------------------------------------
#Exiting the applicaion:
#-----------------------------------------------------------------------------------------------------
def Exit():
result = tkmessagebox.askquestion('Do you want to exit the application?(Y/N)', icon='warning')
if result == 'yes':
win.destroy()
exit()
#-----------------------------------------------------------------------------------------------------
#Frame Widget
#-----------------------------------------------------------------------------------------------------
win = Tk()
win.title("......Library Management Application.....")
win.geometry("1000x500")
Top = Frame(win,width=900,height=50,bd=8,relief='raise',bg='red')
Top.pack(side=TOP)
Left = Frame(win,width=300,height=500,bd=8,relief='raise',bg='blue')
Left.pack(side=LEFT)
Right = Frame(win,width=800,height=500,bd=8,relief='raise',bg='yellow')
Right.pack(side=RIGHT)
Forms = Frame(Left,width=300,height=500,bd=8,relief='raise',bg='grey')
Forms.pack(side=TOP)
BUTTONS= Frame(Left,width=300,height=450,bd=8,relief='raise',bg='black')
BUTTONS.pack(side=BOTTOM)
txt_title = Label(Top, text= 'Library Book Management Application',width = 1000,bg='black',fg='white',font='arial')
txt_title.pack(side=TOP)
book_title = Text(Forms,height=2,width=40)
book_title.grid(row=0,column=1)
author_name = Text(Forms,height=2,width=40)
author_name.grid(row=1,column=1)
year = Text(Forms,height=2,width=40)
year.grid(row=2,column=1)
isbn = Text(Forms,height=2,width=40)
isbn.grid(row=3,column=1)
#-----------------------------------------------------------------------------------------------------
#Label Widget
#-----------------------------------------------------------------------------------------------------
title_label = Label(Forms,text = "Book title:",font=('arial',16),bg='grey')
title_label.grid(row=0,stick='e')
author_label = Label(Forms, text = "Author:",font=('arial',16),bg='grey')
author_label.grid(row=1,stick='e')
year_label = Label(Forms, text = "Year:",font=('arial',16),bg='grey')
year_label.grid(row=2,stick='e')
isbn_label = Label(Forms, text = "ISBN:",font=('arial',16),bg='grey')
isbn_label.grid(row=3,stick='e')
txt_result = Label(Right)
txt_result.pack(side=BOTTOM)
#-----------------------------------------------------------------------------------------------------
#Button Widget
#-----------------------------------------------------------------------------------------------------
display_btn = Button (BUTTONS, text='Display',command=display_records)
display_btn.pack(side=LEFT)
search_btn = Button(BUTTONS,text='Search',command=search)
search_btn.pack(side=LEFT)
add_btn = Button(BUTTONS,text='Add',command=add)
add_btn.pack(side=LEFT)
issue_btn = Button(BUTTONS,text='Issue', command=issue_book)
issue_btn.pack(side=LEFT)
delete_btn=Button(BUTTONS,text='Delete',command=delete_book)
delete_btn.pack(side=LEFT)
exit_btn = Button(BUTTONS,text='Exit',command=Exit)
exit_btn.pack(side=LEFT)
#-----------------------------------------------------------------------------------------------------
#Listbox Widget
#-----------------------------------------------------------------------------------------------------
tree = ttk.Treeview(Right)
scrollbary=Scrollbar(Right,orient=VERTICAL)
scrollbarx=Scrollbar(Right,orient=HORIZONTAL)
scrollbary.config(command=tree.yview)
scrollbary.pack(side=RIGHT, fill=Y)
scrollbarx.config(command=tree.xview)
scrollbarx.pack(side=BOTTOM, fill=X)
tree['columns']=("Book Title", "Author Name", "Publish Year", "ISBN No")
tree.column('#0', stretch=NO,width=0)
tree.column('Book Title',anchor=CENTER, width=125)
tree.column('Author Name',anchor=CENTER, width=125)
tree.column('Publish Year',anchor=CENTER, width=80)
tree.column('ISBN No',anchor=CENTER, width=50)
tree.heading('#0', text='',anchor=CENTER)
tree.heading("Book Title", text = "Book Title", anchor=CENTER)
tree.heading("Author Name", text = "Author Name", anchor=CENTER)
tree.heading("Publish Year", text = "Publish Year", anchor=CENTER)
tree.heading("ISBN No", text = "ISBN No", anchor=CENTER)
tree.pack()
win.mainloop()