Project 1 - English Dictionary App & Library Book Management System
1 A.
from tkinter import*
import json
import os
window=Tk()
window.title("English Dictionary app")
window.geometry("400x400")
# creating db and adding new word
def add_new_word():
forget_frames()
frame_nw.pack()
lnw.pack()
nw.pack()
lnm.pack()
nm.pack()
bnw.pack()
def addword():
d1=dict()
w=str(n_w.get())
m=str(n_m.get())
d1[w]=m
with open("words.txt","a+") as writewords:
pass
if os.stat("words.txt").st_size == 0:
with open("words.txt","w+") as writewords:
writewords.write(json.dumps(d1))
else:
with open("words.txt","r") as writewords:
data=writewords.read()
dictionary=json.loads(data)
dictionary[w.lower()]=m
with open("words.txt","w") as writewords:
writewords.write(json.dumps(dictionary))
label1.config(text="New word is added.",fg="green")
label1.pack()
n_w.set("")
n_m.set("")
#func to fetch meaning
def find_the_meaning():
forget_frames()
frame_w.pack()
lw.pack()
w_m.pack()
lm.pack()
mt.pack()
b4.pack()
def findmeaning():
wc=str(w.get())
with open("words.txt","r") as findwords:
words=findwords.read()
dictionary=json.loads(words)
if wc.lower() not in dictionary.keys():
mt.delete("1.0",END)
label2.config(text="Sorry... The entered word is not in the dictionary.",fg="red")
label2.pack()
else:
m=dictionary[wc.lower()]
mt.delete("1.0",END)
mt.insert(END,m)
label2.config(text="meaning fetched.",fg="green")
label2.pack()
#func to update meaning
def update_a_word():
forget_frames()
frame_uw.pack()
luw.pack()
uw.pack()
lum.pack()
um.pack()
buw.pack()
def updateword():
uw=str(u_w.get())
um=str(u_m.get())
with open("words.txt","r") as updatewords:
words=updatewords.read()
dictionary=json.loads(words)
dictionary[uw.lower()]=um
with open("words.txt","w") as updatewords:
updatewords.write(json.dumps(dictionary))
u_w.set("")
u_m.set("")
label3.config(text="Word updated.",fg="green")
label3.pack()
#function to hide older frames while using new option in mainmenu
def forget_frames():
frame_nw.pack_forget()
frame_w.pack_forget()
frame_uw.pack_forget()
lnw.pack_forget()
nw.pack_forget()
lnm.pack_forget()
nm.pack_forget()
bnw.pack_forget()
label1.pack_forget()
label2.pack_forget()
label3.pack_forget()
#creating menu bar
menubar=Menu(window)
filemenu=Menu(menubar)
filemenu.add_command(label="Add a new word",command=add_new_word)
filemenu.add_command(label="Find the meaning",command=find_the_meaning)
filemenu.add_command(label="Update a word",command=update_a_word)
filemenu.add_separator()
filemenu.add_command(label="exit",command=window.destroy)
menubar.add_cascade(label="Main Menu",menu=filemenu)
#creating the frame
frame_nw=LabelFrame(window,width=400,height=400,text="Insert new word",padx=20,pady=20)
frame_w=LabelFrame(window,width=400,height=400,text="Find meaning",padx=20,pady=20)
frame_uw=LabelFrame(window,width=400,height=400,text="Update meaning",padx=20,pady=20)
# label entry button for new word
lnw=Label(frame_nw,text="Enter the new word ")
n_w=StringVar()
nw=Entry(frame_nw,textvariable=n_w)
lnm=Label(frame_nw,text="Enter the meaning ")
n_m=StringVar()
nm=Entry(frame_nw,width=30,textvariable=n_m)
bnw=Button(frame_nw,text="Add",command=addword)
# label entry button to fetch meaning
lw=Label(frame_w,text="Enter the word ")
w=StringVar()
w_m=Entry(frame_w,textvariable=w)
lm=Label(frame_w,text="Meaning ")
mt=Text(frame_w,height=4,width=30)
b4=Button(frame_w,text="Meaning",command=findmeaning)
# label entry button for updating word
luw=Label(frame_uw,text="Enter the word to update")
u_w=StringVar()
uw=Entry(frame_uw,textvariable=u_w)
lum=Label(frame_uw,text="Enter the meaning ")
u_m=StringVar()
um=Entry(frame_uw,width=30,textvariable=u_m)
buw=Button(frame_uw,text="Update",command=updateword)
label1=Label(window)
label2=Label(window)
label3=Label(window)
window.config(menu=menubar)
window.mainloop()
1 B.
from tkinter import *
import sqlite3
import tkinter.ttk as ttk
import tkinter.messagebox as tkMessageBox
root=Tk()
root.title("Library Book Management System")
#create database
def database1():
global connect,c_obj
connect=sqlite3.connect("Library_Book_Management.db")
c_obj=connect.cursor()
c_obj.execute("CREATE TABLE IF NOT EXISTS catalogue(booktitle TEXT,author TEXT,year INTEGER,isbn VARCHAR(13))")
#adds new entry to the database
def add():
if BOOKTITLE.get()=="" or AUTHOR.get()=="" or YEAR.get()=="":
txt_result.config(text="Please dont leave blank.",fg="red")
else:
if ISBN.get()=="":
ISBN.set("-")
database1()
c_obj.execute("INSERT INTO catalogue(booktitle,author,year,isbn) VALUES(?,?,?,?)",(str(BOOKTITLE.get()),str(AUTHOR.get()),str(YEAR.get()),str(ISBN.get())))
connect.commit()
BOOKTITLE.set("")
AUTHOR.set("")
YEAR.set("")
ISBN.set("")
c_obj.close()
connect.close()
txt_result.config(text="Record inserted.",fg="green")
#display all data in the database
def display():
tree.delete(*tree.get_children())
database1()
c_obj.execute("SELECT * FROM catalogue")
fetch=c_obj.fetchall()
for data in fetch:
tree.insert('',"end",values=(data[0],data[1],data[2],data[3]))
c_obj.close()
txt_result.config(text="Successfully fetched data.",fg="black")
#to search any data in the database
def search():
tree.delete(*tree.get_children())
if BOOKTITLE.get()=="" or AUTHOR.get()=="" or YEAR.get()=="":
txt_result.config(text="Please dont leave blank.",fg="red")
else:
if ISBN.get()=="":
database1()
c_obj.execute("SELECT * FROM catalogue WHERE booktitle=? AND author=? AND year=?",(str(BOOKTITLE.get()),str(AUTHOR.get()),str(YEAR.get())))
fetch=c_obj.fetchall()
for data in fetch:
tree.insert('',"end",values=(data[0],data[1],data[2],data[3]))
c_obj.close()
BOOKTITLE.set("")
AUTHOR.set("")
YEAR.set("")
ISBN.set("")
txt_result.config(text="Successfully fetched data.",fg="black")
else :
database1()
c_obj.execute("SELECT * FROM catalogue WHERE booktitle=? AND author=? AND year=? AND isbn=?",(str(BOOKTITLE.get()),str(AUTHOR.get()),str(YEAR.get()),str(ISBN.get())))
fetch=c_obj.fetchall()
for data in fetch:
tree.insert('',"end",values=(data[0],data[1],data[2],data[3]))
c_obj.close()
BOOKTITLE.set("")
AUTHOR.set("")
YEAR.set("")
ISBN.set("")
txt_result.config(text="Successfully fetched data.",fg="black")
#To search data connected to issue date
def issue():
tree.delete(*tree.get_children())
if YEAR.get()=="":
txt_result.config(text="Please dont leave YEAR blank.",fg="red")
else:
database1()
c_obj.execute("SELECT * FROM catalogue WHERE year=?",(str(YEAR.get()),))
fetch=c_obj.fetchall()
for data in fetch:
tree.insert('',"end",values=(data[0],data[1],data[2],data[3]))
c_obj.close()
BOOKTITLE.set("")
AUTHOR.set("")
YEAR.set("")
ISBN.set("")
txt_result.config(text="Successfully fetched data.",fg="black")
#deleting particular data from database
def delete():
tree.delete(*tree.get_children())
if BOOKTITLE.get()=="" or AUTHOR.get()=="" or YEAR.get()=="" or ISBN.get()=="":
txt_result.config(text="Please dont leave blank.",fg="red")
else:
database1()
c_obj.execute("DELETE FROM catalogue WHERE booktitle=? AND author=? AND year=? AND isbn=?",(str(BOOKTITLE.get()),str(AUTHOR.get()),str(YEAR.get()),str(ISBN.get())))
connect.commit()
c_obj.close()
BOOKTITLE.set("")
AUTHOR.set("")
YEAR.set("")
ISBN.set("")
txt_result.config(text="Successfully data deleted.",fg="black")
def exit():
result=tkMessageBox.askquestion("Do you want to exit? (y/n)",icon="warning")
if result=="yes":
root.destroy()
BOOKTITLE=StringVar()
AUTHOR=StringVar()
YEAR=StringVar()
ISBN=StringVar()
#frame declaration
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)
# all lable declaration
txt_title=Label(Top,width=900,font=("arial",24),text="Library Book Management app")
txt_title.pack()
txt_booktitle=Label(Forms,text="Booktitle:",font=("arial",16),bd=15)
txt_booktitle.grid(row=0,stick="e")
txt_author=Label(Forms,text="Author:",font=("arial",16),bd=15)
txt_author.grid(row=1,stick="e")
txt_year=Label(Forms,text="Year:",font=("arial",16),bd=15)
txt_year.grid(row=2,stick="e")
txt_isbn=Label(Forms,text="ISBN:",font=("arial",16),bd=15)
txt_isbn.grid(row=3,stick="e")
txt_result=Label(Buttons)
txt_result.pack(side=TOP)
#all entry
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)
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)
#all buttons
btn_create=Button(Buttons,width=10,text="ADD",command=add)
btn_create.pack(side=LEFT)
btn_read=Button(Buttons,width=10,text="DISPLAY",command=display)
btn_read.pack(side=LEFT)
btn_search=Button(Buttons,width=10,text="SEARCH",command=search)
btn_search.pack(side=LEFT)
btn_issue=Button(Buttons,width=10,text="ISSUE",command=issue)
btn_issue.pack(side=LEFT)
btn_delete=Button(Buttons,width=10,text="DELETE",command=delete)
btn_delete.pack(side=LEFT)
btn_exit=Button(Buttons,width=10,text="EXIT",command=exit)
btn_exit.pack(side=LEFT)
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)
#declaring tree
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=150)
tree.column("#1",stretch=NO,minwidth=0,width=150)
tree.column("#2",stretch=NO,minwidth=0,width=150)
tree.column("#3",stretch=NO,minwidth=0,width=150)
tree.pack()
if __name__=="__main__":
root.mainloop()