Project 1 - English Dictionary App & Library Book Management System
Aim : If item 1 is selected, it should prompt for a word and then accept its meaning. This pair of word and its meaning should be stored in a file called ‘words.txt’. When item 2 is selected, it should prompt for a word, search that word in the file ‘words.txt’ and return the meaning if it is found. If the meaning is not found, it should display an appropriate message. Similarly, when choice 3 is entered, it should accept a word and update its meaning. In all the above three cases, the prompt should return to the main menu. The words and their meanings should be stored as a dictionary in words.txt, using the concept of serialization. If choice 4 is selected, a graceful exit should be performed.
1A: English Dictionary App.
1B: Library Book Management System.
The Program is written as follows:
1A) import sys
import fileinput
print("##################################### WELCOME TO MY DICTIONARY #############################")
def Add_New_Word():
f = open('word.txt', 'a+')
word = input("Enter your word : ")
meaning = input(" Enter the Meaning of the word : ")
a = word + " : " + meaning
f.write("\n")
f.write(a)
print("data write sucsessfully!")
f.close()
def Find_Meaning():
readFile = open('word.txt', 'r')
print(readFile)
word = input("Enter word to find the meaning : ")
for i in readFile:
if word in i:
print(f'This word present in the dictionary \t " {i} " : ' )
break
readFile.close()
import sys
import fileinput
def Update_Word():
word = input("Enter text to be replaced:")
replacement_Word = input("Enter replacement text")
f=open('word.txt','a+')
for l in fileinput.input(files="word.txt"):
l = l.replace(word, replacement_Word)
sys.stdout.write(l)
print('Word has successfully updateed')
f.close()
def Exit():
sys.exit()
chiceStr='''
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Main Menu <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
P2ress 1 --> For Add a new word
Press 2 --> For Find the meaning
Press 3 --> For Update a word
Press 4 --> For Exit
'''
def NEW():
while True:
def mainFuc():
print(chiceStr)
choice=int(input("Enter Your Choice 1 to 4 \n"))
if choice==1:
Add_New_Word()
elif choice==2:
Find_Meaning()
elif choice==3:
Update_Word()
elif choice==4:
Exit()
else:
print("Your Choice is wrong ")
mainFuc()
if __name__=="__main__":
NEW()
1B)
from tkinter import *
import tkinter as tk
import sqlite3
import tkinter.ttk as ttk
import tkinter.messagebox as tkMassgeBox
fullframe=Tk()
fullframe.title('Library Managment System')
def database():
global conn,cursorobj
conn=sqlite3.connect('books.db')
cursorobj=conn.cursor()
cursorobj.execute("CREATE TABLE IF NOT EXISTS librarytable(booktitle TEXT,author TEXT,year INTEGER,isbn INTEGER)")
def displaybook():
tree.delete(*tree.get_children())
database()
cursorobj.execute("SELECT * FROM librarytable ORDER BY booktitle ASC")
fetch=cursorobj.fetchall()
for data in fetch:
tree.insert('','end',values=(data[0],data[1],data[2],data[3]))
cursorobj.close()
conn.close()
tk.messagebox.showinfo('Liabrary','Data fetched')
#print(fetch)
def searchbook():
titleauthor=((author.get()).title())
titlebookname=((booktitle.get()).title())
titleisbn=((isbn.get()).title())
tree.delete(*tree.get_children())
database()
cursorobj.execute("SELECT * FROM librarytable")
fetch=cursorobj.fetchall()
count=0
for data in fetch:
if(titlebookname==data[0]) and (titleauthor==data[1]) and (str(year.get())==str(data[2])) and (str(titleisbn)==str(data[3])):
tree.insert('','end',values=(data[0],data[1],data[2],data[3]))
count+=1
elif(titlebookname==data[0]):
tree.insert('','end',values=(data[0],data[1],data[2],data[3]))
count+=1
elif(titleauthor==data[1]):
tree.insert('','end',values=(data[0],data[1],data[2],data[3]))
count+=1
elif(str(publishedyear.get())==str(data[2])):
tree.insert('','end',values=(data[0],data[1],data[2],data[3]))
count+=1
elif(str(titleisbn)==str(data[3])):
tree.insert('','end',values=(data[0],data[1],data[2],data[3]))
count+=1
if(count==0):
tk.messagebox.showinfo('Library','No book like this in library')
cursorobj.close()
conn.close()
author.set("")
booktitle.set("")
year.set("")
isbn.set("")
def addbook():
database()
titlebookname=((booktitle.get()).title())
titleauthor=((author.get()).title())
titlepublishedyear=str(year.get())
titleisbn=str(isbn.get())
#ursorobj.execute("INSERT INTO librarytable VALUES(?,?,?,?)",(titlebookname,titleauthor,titlepublishedyear,titleisbn))
#print(titlebookname)
if ((titlebookname=='') or (titleauthor=='') or (titlepublishedyear=='') or (titleisbn)==''):
tk.messagebox.showinfo('Library','Please fill all the details')
else:
database()
cursorobj.execute("INSERT INTO librarytable(booktitle,author,year,isbn) VALUES(?,?,?,?)",(titlebookname,titleauthor,titlepublishedyear,titleisbn))
conn.commit()
author.set("")
booktitle.set("")
year.set("")
isbn.set("")
cursorobj.close()
conn.close()
tk.messagebox.showinfo('Library','Values inserted')
def deletebook():
titleauthor=((author.get()).title())
titlebookname=((booktitle.get()).title())
titleisbn=((isbn.get()).title())
titlepublishedyear=(year.get().title())
tree.delete(*tree.get_children())
database()
cursorobj.execute("SELECT * FROM librarytable")
fetch=cursorobj.fetchall()
count=0
for data in fetch:
if ((titlebookname==data[0])and (titleauthor==data[1])and (str(titlepublishedyear)==str(data[2])) and (str(titleisbn)==str(data[3]))):
tempa=("'"+str(titlebookname)+"'")
tempb=("'"+str(titleauthor)+"'")
tempp=("'"+str(titlepublishedyear)+"'")
tempi=("'"+str(titleisbn)+"'")
temp1="DELETE FROM librarytable WHERE booktitle= "+tempa +"and author= "+tempb +"and year= "+tempp +"and isbn= "+tempi
cursorobj.execute(temp1)
conn.commit()
count+=1
if(count==0):
tk.messagebox.showinfo('Library','No data with this name')
else:
tk.messagebox.showinfo('Library','Book deleted')
author.set("")
booktitle.set("")
year.set("")
isbn.set("")
conn.commit()
cursorobj.close()
conn.close()
def issuebook():
titleauthor=((author.get()).title())
titlebookname=((booktitle.get()).title())
titleisbn=((isbn.get()).title())
titlepublishedyear=(year.get().title())
tree.delete(*tree.get_children())
database()
cursorobj.execute("SELECT * FROM librarytable")
fetch=cursorobj.fetchall()
cursorobj.execute("DELETE FROM librarytable")
count=0
for data in fetch:
if(titlebookname==data[0])and (titleauthor==data[1])and (titlepublishedyear==str(data[2])) and (titleisbn==str(data[3])):
if(count==0):
count+=1
tk.messagebox.showinfo('Library','Book issued')
else:
cursorobj.execute("INSERT INTO librarytable(booktitle,author,year,isbn) VALUES(?,?,?,?)",(data[0],data[1],data[2],data[3]))
conn.commit()
else:
cursorobj.execute("INSERT INTO librarytable(booktitle,author,year,isbn) VALUES(?,?,?,?)",(data[0],data[1],data[2],data[3]))
conn.commit()
if(count==0):
tk.messagebox.showinfo('Library','No book with this detail in library')
author.set("")
booktitle.set("")
publishedyear.set("")
isbn.set("")
conn.commit()
cursorobj.close()
conn.close()
def Exit():
result=tkMassgeBox.askquestion('Library','Do you want to exit?',icon='warning')
if(result=='yes'):
fullframe.destroy()
else:
tk.messagebox.showinfo('Library','You will now return to the application screen')
#=============================Frames==================================================================
TitleFrame=Frame(fullframe,width=1350,padx=20,bd=20,relief=RIDGE,bg='grey85')
TitleFrame.pack(side=TOP)
lblTitle=Label(TitleFrame,width=45,font=('Lucida Sans',40),text='...Library Managment Application...',padx=12, fg='#000')
lblTitle.grid()
LeftFrame=Frame(fullframe,bd=20,width=930,height=520,padx=20,relief=RIDGE,bg='red')
LeftFrame.pack(side=LEFT)
ButtonFrame=Frame(LeftFrame,bd=20,padx=20,relief=RIDGE,bg='grey85')
ButtonFrame.pack(side=BOTTOM)
Form=Frame(LeftFrame,bd=20,width=620,height=520,padx=20,bg='grey85')
Form.pack()
RightFrame=Frame(fullframe,bd=20,width=620,height=520,padx=20,relief=RIDGE,bg='yellow')
RightFrame.pack(side=RIGHT)
#===========================================Variables===========================================
author=StringVar()
booktitle=StringVar()
year=StringVar()
isbn=StringVar()
#===========================================Label&Entry===========================================
lbl_authorname=Label(Form,text="Booktitle:",font=('Rockwell',18,'bold'),fg='#f00')
lbl_authorname.grid(row=0,column=0,sticky='w')
entry_authorname=Entry(Form,textvariable=booktitle,width=35)
entry_authorname.grid(row=0,column=1)
lbl_bookname=Label(Form,text="Author:",font=('Rockwell',18,'bold'), fg='#f00')
lbl_bookname.grid(row=1,column=0,sticky='w')
entry_bookname=Entry(Form,textvariable=author,width=35)
entry_bookname.grid(row=1,column=1)
lbl_publishyear=Label(Form,text="Year:",font=('Rockwell',18,'bold'),fg='#f00')
lbl_publishyear.grid(row=2,column=0,sticky='w')
entry_publishyear=Entry(Form,textvariable=year,width=35)
entry_publishyear.grid(row=2,column=1)
lbl_ISBN=Label(Form,text="ISBN:",font=('Rockwell',18,'bold'), fg='#f00')
lbl_ISBN.grid(row=3,column=0,sticky='w')
entry_ISBN=Entry(Form,textvariable=isbn,width=35)
entry_ISBN.grid(row=3,column=1)
#===========================================Buttons===========================================
but_view=Button(ButtonFrame,text="Display",width=9,font=('Rockwell',15),command=displaybook,bg='grey85', fg='#000')
but_view.grid(row=1,column=0)
but_search=Button(ButtonFrame,text="Search",width=9,font=('Rockwell',15),command=searchbook,bg='grey85', fg='#000')
but_search.grid(row=1,column=1)
but_add=Button(ButtonFrame,text="Add",width=9,font=('Rockwell',15),command=addbook,bg='grey85', fg='#000')
but_add.grid(row=1,column=2)
but_issue=Button(ButtonFrame,text="Issue",width=9,font=('Rockwell',15),command=issuebook,bg='grey85', fg='#000')
but_issue.grid(row=1,column=3)
but_delete=Button(ButtonFrame,text="Delete",width=9,font=('Rockwell',15),command=deletebook,bg='grey85', fg='#000')
but_delete.grid(row=1,column=4)
but_exit=Button(ButtonFrame,text="Exit",width=9,font=('Rockwell',15),command=Exit,bg='grey85', fg='#000')
but_exit.grid(row=1,column=5)
#===========================================Scrollbar===========================================
scrollbary=Scrollbar(RightFrame)
scrollbarx=Scrollbar(RightFrame)
col=('Book_Name','Author_Name','Published_year','ISBN')
tree=ttk.Treeview(RightFrame,columns=col,show='headings')
scrollbary=Scrollbar(RightFrame)
scrollbary.pack(side=RIGHT,fill=Y)
scrollbarx=Scrollbar(RightFrame)
scrollbarx.pack(side=BOTTOM,fill=X)
tree.heading("Book_Name",text="Booktitle",anchor=W)
tree.heading("Author_Name",text="Author",anchor=W)
tree.heading("Published_year",text="Year",anchor=W)
tree.heading("ISBN",text="ISBN",anchor=W)
tree.column("Author_Name",minwidth=0,width=250)
tree.column("Book_Name",minwidth=0,width=250)
tree.column("Published_year",minwidth=0,width=300)
tree.column("ISBN",minwidth=0,width=250)
tree.pack(side=TOP,fill=BOTH)
fullframe.configure(bg='grey85')
fullframe.mainloop().