Project 1 - English Dictionary App & Library Book Management System
'''1A: English Dictionary App '''
import json
print('Main Menu')
print('1. Add a new word')
print('2. Find the meaning')
print('3. Update the word')
print('4. Exit')
f = open('words.txt','w')
dictionary = {}
json.dump(dictionary,f)
f.close()
while True:
print('\n')
a = int(input('Please enter the serial number for selecting options :'))
if a == 1:
import json
print('Adding a word to the Dictionary')
f = open('words.txt','w')
word = input('Enter the word : ')
meaning = input('Enter the meaning : ')
dictionary[word] = meaning
json.dump(dictionary,f)
f.close()
elif a == 2:
import json
print('Searching the meaning for a word')
f = open('words.txt','r+')
dictionary = json.load(f)
meaning = input('Enter the word : ')
if meaning in dictionary:
print('The meaning is :',dictionary.get(meaning))
else:
print('the word not exists in the Dictionary')
json.dump(dictionary,f)
f.close()
elif a == 3:
import json
print('Updating the meaning of a word')
f = open('words.txt','w')
word = input('Enter the word :')
update = input('Enter the meaning : ')
dictionary[word] = update
json.dump(dictionary,f)
f.close()
elif a == 4:
print('Thank You')
break
else:
print('Main Menu')
print('1. Add a word')
print('2. Find the meaning')
print('3. Update the word')
print('4. Exit')
-----------------------------------------------------------------------------------------------------------
'''1B: Library Book Management System'''
from tkinter import *
import sqlite3
import tkinter.ttk as tkt
import tkinter.messagebox as tkmessageBox
root = Tk()
root.title('A Library Application')
root.geometry('900x500+400+300')
def Database():
global conn,cursor
conn=sqlite3.connect('n.db')
cursor=conn.cursor()
cursor.execute("""CREATE TABLE IF NOT EXISTS books (mem_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
booktitle TEXT, author TEXT, year TEXT, isbn TEXT)""")
def Display():
global conn,cursor
conn=sqlite3.connect('n.db')
cursor=conn.cursor()
cursor.execute("SELECT * FROM books ")
fetch=cursor.fetchall()
tree.delete(*tree.get_children())
for data in fetch:
tree.insert('','end',values=(data[1],data[2],data[3],data[4]))
cursor.close()
conn.close()
def search_from_table(tree,booktitle, author, year, isbn):
try:
cursor.execute("SELECT * FROM books WHERE booktitle=?", (booktitle,))
searched_titles = cursor.fetchall()
cursor.execute("SELECT * FROM books WHERE author=?", (author,))
searched_authors = cursor.fetchall()
cursor.execute("SELECT * FROM books WHERE year=?", (year,))
searched_years = cursor.fetchall()
cursor.execute("SELECT * FROM books WHERE ISBN=?", (isbn,))
searched_isbns = cursor.fetchall()
return(searched_titles + searched_authors + searched_years + searched_isbns)
except:
return(-1)
def Search():
global conn,cursor
conn=sqlite3.connect('n.db')
cursor=conn.cursor()
booktitle = BOOKTITLE.get()
author = AUTHOR.get()
year = YEAR.get()
isbn = ISBN.get()
res = search_from_table(tree,booktitle, author, year, isbn)
if res==-1:
tkmessageBox.showerror("Error!", "something went wrong!")
if res!=-1:
tree.delete(*tree.get_children())
for data in res:
tree.insert('','end',values=(data[1],data[2],data[3],data[4]))
cursor.close()
conn.close()
def Add():
if BOOKTITLE.get() =="" or AUTHOR.get() =="" or YEAR.get() =="" or ISBN.get()=="":
tkmessageBox.showerror("Error!", "Enter all promts ")
else:
global conn,cursor
conn=sqlite3.connect('n.db')
cursor=conn.cursor()
cursor.execute("INSERT INTO books(booktitle,author,year,isbn) VALUES(?,?,?,?)",(str(BOOKTITLE.get()),str(AUTHOR.get()),str(YEAR.get()),str(ISBN.get())))
conn.commit()
print('book added successfully')
cursor.close()
conn.close()
def Issue():
Database()
print("issue book")
tkmessageBox.showwarning("Undefined", "this function is not yet defined!")
cursor.close()
conn.close()
def remove(cursor, booktitle, author, year, isbn):
try:
cursor.execute('DELETE FROM books WHERE booktitle= ?',(booktitle,))
cursor.execute('DELETE FROM books WHERE author= ?',(author,))
cursor.execute('DELETE FROM books WHERE year=?',(year,))
cursor.execute('DELETE FROM books WHERE isbn=?',(isbn,))
except:
return(-1)
def Delete():
global conn,cursor
conn=sqlite3.connect('n.db')
cursor=conn.cursor()
booktitle = Booktitle.get()
author = Author.get()
year = Year.get()
isbn = ISBN.get()
code = remove(cursor, booktitle, author, year, isbn)
if code==-1:
tkmessageBox.showerror("Error!", "something went wrong!")
else:
tkmessageBox.showinfo("Deleted!")
conn.commit()
cursor.close()
conn.close()
def Exit():
result=tkmessageBox.askquestion('Do you want to exit ? (y/n)', icon='warning')
if result == 'yes':
root.destroy()
exit()
BOOKTITLE = StringVar()
AUTHOR = StringVar()
YEAR = StringVar()
ISBN = StringVar()
#=============================================================================
Top = Frame(root,width=500, height=50, bd=2, 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=LEFT)
# ========================================= label widget===================================================
txt_title=Label(Top,width=500,font=('arial',24),text=".....Library Management Application....")
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")
# ========================================= Entry widget===================================================
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)
# ========================================= Button widget===================================================
btn_Display=Button(Buttons, width=10, text="Display", command=Display)
btn_Display.pack(side=LEFT)
btn_Search=Button(Buttons, width=10, text="Search", command=Search)
btn_Search.pack(side=LEFT)
btn_Add=Button(Buttons, width=10, text="Add", command=Add)
btn_Add.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=TOP)
# ========================================= List widget===================================================
scrollbary=Scrollbar(Right,orient=VERTICAL)
scrollbarx=Scrollbar(Right,orient=HORIZONTAL)
tree=tkt.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)
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=80)
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()
# ========================================= INITIALIZATION===================================================
if __name__== '__main__':
root.mainloop()