Project 1 - English Dictionary App & Library Book Management System
1A ENGLISH DICTIONARY APP
In [1]:
import pickle
import os
def saveDictionary(tmp_dict):
'''
This function recives a python dictionary,
and saves it to a serialized file words.txt
'''
try:
with open('words.txt', 'wb') as dictionary:
pickle.dump(tmp_dict, dictionary)
print("DONE: writing to dictionary words.txt")
except Exception as e:
print(f"ERROR: {e}")
def getDictionary():
'''
This function returns a python dictionary,
after de-serializing the file words.txt
'''
try:
with open('words.txt', 'rb') as dictionary:
tmp_dict = pickle.load(dictionary)
print("DONE: reading from dictionary words.txt")
return tmp_dict
except Exception as e:
print(f"ERROR: {e}")
def addNewWord():
'''
This function provides functionalities for n==1,
that is, it creates a new word in the dictionary
'''
print("_"*15)
print("NEW WORD ADDER:")
print("_"*15)
print("Enter a Word to add")
word = input(">>")
print(f"Enter a meaning of {word} to add")
meaning = input(">>")
dictionary = getDictionary()
dictionary[word] = meaning
saveDictionary(dictionary)
main_menu()
def findMeaning():
'''
This function provides functionalities for n==2,
that is, it finds a word in the dictionary
'''
print("_"*15)
print("WORD FINDER:")
print("_"*15)
print("Enter a Word to find")
word = input(">>")
dictionary = getDictionary()
if word in dictionary:
print(f"Found Entry: [{word} : {dictionary[word]}]")
else:
print("ERROR: The word doesn't exist!")
findMeaning()
main_menu()
def updateWord():
'''
This function provides functionalities for n==3,
that is, it updates a word in the dictionary
'''
print("_"*15)
print("WORD UPDATER:")
print("_"*15)
print("Enter an old Word to edit")
word = input(">>")
dictionary = getDictionary()
if word in dictionary:
print(f"Enter a New Meaning to edit [{word} : {dictionary[word]}] to:")
new_meaning = input(">>")
dictionary[word] = new_meaning
saveDictionary(dictionary)
print(f"DONE: Updated to [{word} : {dictionary[word]}]")
else:
print("ERROR: The word doesn't exist! impossible to update!")
updateWord()
main_menu()
def main_menu():
'''
This function prints Main Menu:
______________
Main Menu
______________
1. Add a new word
2. Find the meaning
3. Update a word
4. Exit
______________
'''
if not os.path.exists(os.path.join("words.txt")):
try:
with open('words.txt', 'wb') as dictionary:
pickle.dump({}, dictionary)
print("DONE: writing to dictionary words.txt")
except Exception as e:
print(f"ERROR: {e}")
print("_"*15)
print("Main Menu")
print("_"*15)
print("1. Add a new word")
print("2. Find the meaning")
print("3. Update a word")
print("4. Exit")
print("_"*15)
print('select the operation by entering operation number "1,2,3,4" given above')
n = int(input(">>"))
if n==1:
addNewWord()
elif n==2:
findMeaning()
elif n==3:
updateWord()
elif n==4:
exit()
else:
print("ERROR: Please Enter a digit according to Menu.")
main_menu()
main_menu()
DONE: writing to dictionary words.txt _______________ Main Menu _______________ 1. Add a new word 2. Find the meaning 3. Update a word 4. Exit _______________ select the operation by entering operation number "1,2,3,4" given above >>1 _______________ NEW WORD ADDER: _______________ Enter a Word to add >>Vishal Enter a meaning of Vishal to add >>Very Big DONE: reading from dictionary words.txt DONE: writing to dictionary words.txt _______________ Main Menu _______________ 1. Add a new word 2. Find the meaning 3. Update a word 4. Exit _______________ select the operation by entering operation number "1,2,3,4" given above >>3 _______________ WORD UPDATER: _______________ Enter an old Word to edit >>Vishal DONE: reading from dictionary words.txt Enter a New Meaning to edit [Vishal : Very Big] to: >>Gigantic DONE: writing to dictionary words.txt DONE: Updated to [Vishal : Gigantic] _______________ Main Menu _______________ 1. Add a new word 2. Find the meaning 3. Update a word 4. Exit _______________ select the operation by entering operation number "1,2,3,4" given above >>4
In [ ]:
1B Library Book Management System
In [1]:
# libraries
from tkinter import *
import sqlite3
import tkinter.ttk as ttk # (gives new look )
import tkinter.messagebox as tkMessageBox
In [2]:
# creat window
root = Tk()
root.title('A Library Application!')
Out[2]:
''
In [3]:
# Methods
def Database():
global conn, cursor
conn = sqlite3.connect('simple.db')
cursor = conn.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS member (mem_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, Booktitle TEXT, Author TEXT, Year TEXT, Isbn TEXT)")
def Create():
if BOOKTITLE.get == "" or AUTHOR.get() == "" or YEAR.get() == "" or ISBN.get() == "" :
txt_result.config(text="Please enter all hte fields",fd="red")
else:
Database()
cursor.execute("INSERT INTO member(Booktitle,Author,Year,Isbn) VALUES (?,?,?,?)",(str(BOOKTITLE.get()),str(AUTHOR.get()),str(YEAR.get()),str(ISBN.get())))
conn.commit()
BOOKTITLE.set("")
AUTHOR.set("")
YEAR.set("")
ISBN.set("")
cursor.close()
conn.close()
txt_result.config(text="Record Insert !",fg="green")
def Read():
tree.delete(*tree.get_children()) # tree is a data structure
Database()
cursor.execute("SELECT * FROM member ORDER BY Booktitle ASC")
fetch = cursor.fetchall() # fetch
for data in fetch:
tree.insert('','end',values= (data[1],data[2],data[3],data[4]))
cursor.close()
txt_result.config(text="Successfully fetched data !",fg="black")
def Exit():
result = tkMessageBox.askquestion('Do you want to exit ? (y/n)',icon='warning')
if result == 'yes':
root.destroy()
exit()
def Search():
txt_result.config(text="Function Doesn't Define right now !",fg="black")
In [4]:
# Variable decalration
BOOKTITLE = StringVar()
AUTHOR = StringVar()
YEAR = StringVar()
ISBN = StringVar()
In [5]:
# Frame
Top = Frame(root, width = 900, height = 50, bd=8, relief='raise',bg= "blue")
Top.pack(side=TOP)
Left = Frame(root, width = 300, height = 500, bd=8, relief='raise',bg= "red")
Left.pack(side=LEFT)
Right = Frame(root, width = 600, height = 500, bd=8, relief='raise',bg= "yellow")
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)
# LABEL WIDGET
txt_title= Label(Top,width=900, 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=3, stick="e")
txt_Isbn = Label(Forms, text="ISBN:", font=('arial',16),bd=15)
txt_Isbn.grid(row=4, stick="e")
txt_result = Label(Buttons)
txt_result.pack(side=TOP)
# 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=3, column=1)
Isbn = Entry(Forms, textvariable=ISBN, width=30)
Isbn.grid(row=4, column=1)
# BUTTONS WIDGET
btn_read = Button(Buttons, width=10, text='Display', command=Read)
btn_read.pack(side=LEFT)
btn_read = Button(Buttons, width=10, text='Search', command=Search)
btn_read.pack(side=LEFT)
btn_create = Button(Buttons, width=10, text='Add', command=Create)
btn_create.pack(side=LEFT)
btn_update = Button(Buttons, width=10, text='Issue', state=DISABLED)
btn_update.pack(side=LEFT)
btn_delete = Button(Buttons, width=10, text='Delete', state=DISABLED)
btn_delete.pack(side=LEFT)
btn_exit = Button(Buttons, width=10, text='Exit', command=Exit)
btn_exit.pack(side=LEFT)
In [6]:
# LIST WIDGET
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)
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()
In [7]:
# INITIALIZATION
if __name__ == '__main__':
root.mainloop()