Project 1 - English Dictionary App & Library Book Management System
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
Enter Choice:
'''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.
If you want to be recognized as a professional programmer, you have to practice more and develop applications in Python which you can include in your online portfolio. To achieve this, one such comprehensive project (with two small sub-projects inside it) is added which have to be completed by every student.
1A: English Dictionary App
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.'''
import pickle
import os
def saveDictionary(tmp_dict):
try:
with open('words.txt', 'wb') as dictionary:
pickle.dump(tmp_dict, dictionary)
print("SUCCESS: writing to dictionary words.txt")
except Exception as e:
print(f"ERROR: {e}")
def getDictionary():
try:
with open('words.txt', 'rb') as dictionary:
tmp_dict = pickle.load(dictionary)
print("SUCCESS: reading from dictionary words.txt")
return tmp_dict
except Exception as e:
print(f"ERROR: {e}")
def addNewWord():
word = input("Enter a Word:")
mean = input("Enter a meaning of the word:")
dictionary = getDictionary()
dictionary[word] = mean
dictionary = saveDictionary(dictionary)
main_menu()
def findMean():
word = input("Enter the finding the meaning of the word:")
dictionary = getDictionary()
if word in dictionary:
print(dictionary[word]+"\n")
else:
print("The word is incorrect!!\nPlease enter the correct word\n")
main_menu()
def updateWord():
word = input("Enter a word:")
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"SUCCESS: Updated to [{word} : {dictionary[word]}]\n")
else:
print("ERROR: The word doesn't exist! impossible to update!\n")
main_menu()
def main_menu():
print("English Dictionary app:\n\n"+"1.Add a new word\n"+"2.Find the meaning\n"+"3.Update a word\n"+"4.Exit")
if not os.path.exists(os.path.join("words.txt")):
try:
with open('words.txt', 'wb') as dictionary:
pickle.dump({}, dictionary)
print("SUCCESS: writing to dictionary words.txt")
except Exception as e:
print(f"ERROR: {e}")
choice = int(input("\nEnter a chocie:"))
if choice == 1:
print("Add a new word")
addNewWord()
elif choice == 2:
print("Find the meaning")
findMean()
elif choice == 3:
print("Update a word")
updateWord()
elif choice == 4:
exit()
else:
print("Invalid input")
main_menu()
main_menu()
## 1B: Library Book Management System
'''In this project, a Library Book Management System will be implemented with the front end GUI developed using Tkinter
and the backend database implemented in SQLite. It is a single window tkinter GUI application which will implement
CRUD( create, read, update, delete) operations on a ‘books’ database, in SQLite.
The window will contain text widgets to accept / display book title, author, year of publication, ISBN (optional)
and a large text area/ list box to display the records which match the search criteria.'''
'and the backend database implemented in SQLite. It is a single window tkinter GUI application which will implement \nCRUD( create, read, update, delete) operations on a ‘books’ database, in SQLite. \nThe window will contain text widgets to accept / display book title, author, year of publication, ISBN (optional) \nand a large text area/ list box to display the records which match the search criteria.'
from tkinter import*
import sqlite3
import tkinter.ttk as ttk
import tkinter.messagebox as tkMessageBox
root = Tk()
root.title('Library Book Management System')
''
# method
def Database():
global con, cur
con = sqlite3.connect("books.db")
cur = con.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS book(mem_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, title TEXT, author TEXT, year TEXT, publication TEXT, isbn TEXT)")
def Add():
if TITLE.get() == "" or AUTHOR.get()=="" or YEAR.get()=="" or PUBLICATION.get()=="" or ISBN.get()=="":
txt_result.config(text = "Please enter all the field", fg = "red")
else:
Database()
cur.execute("INSERT INTO book (title, author, year, publication, isbn) VALUES(?, ?, ?, ?, ?)",(str(TITLE.get()), str(AUTHOR.get()), str(YEAR.get()), str(PUBLICATION.get()), str(ISBN.get())))
con.commit()
TITLE.set("")
AUTHOR.set("")
YEAR.set("")
PUBLICATION.set("")
ISBN.set("")
cur.close()
con.close()
txt_result.config(text = "Record Inserted!", fg = "green")
def Display():
tree.delete(*tree.get_children())
Database()
cur.execute("SELECT * FROM book")
fetch = cur.fetchall()
for data in fetch:
tree.insert('','end', values = (data[1], data[2], data[3], data[4], data[5]))
cur.close()
txt_result.config(text = "Successfully fetched data!", fg = "black")
def Search():
Database()
cur.execute("SELECT * FROM book Where title='C Programming'")
fetch = cur.fetchall()
for data in fetch:
txt_box.insert(END,data)
cur.close()
def issue():
Database()
cur.execute("SELECT title FROM book Where author='Brian Kernighan Dennis Ritchie'")
fetch = cur.fetchall()
for data in fetch:
txt_box.insert(END,data)
cur.close()
def Delete():
Database()
cur.execute("DELETE FROM book")
con.commit()
cur.close()
txt_result.config(text = "Successfully Deleted all data!", fg = "green")
def Exit():
result = tkMessageBox.askquestion("Do you want to exit? (y/n):",icon='warning')
if result == 'yes':
root.destroy()
exit()
# Variable declaration
TITLE = StringVar()
AUTHOR = StringVar()
YEAR = StringVar()
PUBLICATION = StringVar()
ISBN = StringVar()
# Frame
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)
Box = Frame(Left, width=500, height = 100, bd = 8, relief = 'raise')
Box.pack(side = BOTTOM)
Buttons = Frame(Left, width = 300 , height = 100, bd = 8, relief = 'raise')
Buttons.pack(side = BOTTOM)
txt_title1 = Label(Top,width = 900,font = ('arial', 24), text = "A Library Book Management System")
txt_title1.pack()
txt_title = Label(Forms, text = "Title:", font=('arial', 16), bd=15)
txt_title.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_publication = Label(Forms, text = "Publication:", font=('arial', 16), bd=15)
txt_publication.grid(row=3, stick="e")
txt_isdn = Label(Forms, text = "ISBN:", font=('arial', 16), bd=15)
txt_isdn.grid(row=4, stick="e")
txt_box = Entry(Box)
txt_box.pack(side=TOP)
txt_result = Label(Buttons)
txt_result.pack(side=TOP)
title = Entry(Forms, textvariable=TITLE, width=30)
title.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)
publication = Entry(Forms, textvariable=PUBLICATION, width=30)
publication.grid(row=3, column=1)
isdn = Entry(Forms, textvariable=ISBN,width=30)
isdn.grid(row=4, column=1)
# Button
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_delete = Button(Buttons, width=10, text = "Issue", command=issue)
btn_delete.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=("Title","Author","Year","Publication","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('Title', text = 'Title', anchor=W)
tree.heading('Author', text = 'Author', anchor=W)
tree.heading('Year', text = 'Year', anchor=W)
tree.heading('Publication', text = 'Publication', 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.column('#5', stretch=NO, minwidth=0, width=120)
tree.pack()
if __name__ == '__main__':
root.mainloop()