Project 1 - English Dictionary App & Library Book Management System
1A: English Dictionary App
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.
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.
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.
#1A: English Dictionary App
#function to display the main menu options and handle user input:
def display_menu():
print("Main Menu:")
print("1. Add a word and its meaning")
print("2. Search for a word")
print("3. Update the meaning of a word")
print("4. Exit")
choice = input("Enter your choice: ")
return choice
#function to add a word and its meaning to the file 'words.txt':
def add_word():
word = input("Enter a word: ")
meaning = input("Enter the meaning of the word: ")
with open('words.txt', 'a') as file:
file.write(f"{word},{meaning}\n")
print("Word and meaning added successfully!")
#function to search for a word in the file 'words.txt' and return its meaning:
def search_word():
word = input("Enter a word to search: ")
with open('words.txt', 'r') as file:
lines = file.readlines()
for line in lines:
w, m = line.strip().split(',')
if w == word:
print(f"Meaning of {word}: {m}")
return
print(f"Word '{word}' not found!")
#function to update the meaning of a word in the file 'words.txt':
def update_word():
word = input("Enter a word to update its meaning: ")
with open('words.txt', 'r') as file:
lines = file.readlines()
updated_lines = []
meaning_updated = False
for line in lines:
w, m = line.strip().split(',')
if w == word:
new_meaning = input(f"Enter the new meaning of {word}: ")
updated_lines.append(f"{word},{new_meaning}\n")
meaning_updated = True
else:
updated_lines.append(line)
if meaning_updated:
with open('words.txt', 'w') as file:
file.writelines(updated_lines)
print("Meaning updated successfully!")
else:
print(f"Word '{word}' not found!")
#main logic that displays the menu and calls the appropriate functions based on user input:
while True:
choice = display_menu()
if choice == '1':
add_word()
elif choice == '2':
search_word()
elif choice == '3':
update_word()
elif choice == '4':
print("Exiting...")
break
else:
print("Invalid choice! Please try again.")
-----------------------------------------------------------------------------------------
#1B. Library Book Management System
import tkinter
from tkinter import *
import sqlite3 as sq
import tkinter.ttk as ttk
from tkinter import messagebox
root = Tk()
root.title('A Library Application!')
root.geometry('1200x800')
#Create Database
def Database():
global conn,cursor
conn = sq.connect('books.db')
cursor = conn.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS mybooks(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,title TEXT,author TEXT,year INTEGER,isbn INTEGER)")
#Add data to Database
def Add():
if TITLE.get()=='' or AUTHOR.get()=='' or YEAR.get()=='':
txt_result.config(text="Please Enter all the fields",fg='red')
else:
Database()
cursor.execute("INSERT INTO mybooks(title,author,year,isbn) VALUES(?,?,?,?)",(TITLE.get(),AUTHOR.get(),YEAR.get(),ISBN.get()))
conn.commit()
TITLE.set('')
AUTHOR.set('')
YEAR.set('')
ISBN.set('')
cursor.close()
conn.close()
txt_result.config(text="Record Added !",fg="green")
#Search data from Database
def Search():
if TITLE.get()=='' and AUTHOR.get()=='' and YEAR.get()=='' and ISBN.get()=='':
txt_result.config(text="Please Enter any of the details to search",fg='red')
else:
tree.delete(*tree.get_children())
Database()
cursor.execute('SELECT * FROM mybooks WHERE title=? OR author=? OR year=? OR isbn=?',(TITLE.get(),AUTHOR.get(),str(YEAR.get()),str(ISBN.get())))
rows = cursor.fetchall()
for data in rows:
tree.insert("",'end',values=(data[1],data[2],data[3],data[4]))
TITLE.set('')
AUTHOR.set('')
YEAR.set('')
ISBN.set('')
cursor.close()
txt_result.config(text="Search completed and results displayed !",fg='green')
#Display data from Database
def Display():
tree.delete(*tree.get_children())
Database()
cursor.execute('SELECT * FROM mybooks')
fetch = cursor.fetchall()
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='blue')
#Closing of the interface
def Exit():
if messagebox.askokcancel("Quit", "Do you want to Exit?"):
root.destroy()
exit()
#Delete the record from database
def Delete():
if TITLE.get()=='' and AUTHOR.get()=='' and YEAR.get()=='' and ISBN.get()=='':
txt_result.config(text="Please Enter any of the details to delete",fg='red')
else:
Database()
cursor.execute('DELETE FROM mybooks WHERE title=? OR author=? OR year=? OR isbn=?',(TITLE.get(),AUTHOR.get(),YEAR.get(),str(ISBN.get())))
conn.commit()
TITLE.set('')
AUTHOR.set('')
YEAR.set('')
ISBN.set('')
cursor.close()
conn.close()
txt_result.config(text="Successfully deleted data and removed completely from the system !",fg='blue')
#Books to issue
def Issue():
if TITLE.get()=='' or AUTHOR.get()=='' or YEAR.get()=='':
txt_result.config(text="Please Enter all the fields",fg='red')
else:
TITLE.set('')
AUTHOR.set('')
YEAR.set('')
ISBN.set('')
txt_result.config(text="Successfully Issued the Book...!",fg="black")
TITLE = StringVar()
AUTHOR = StringVar()
YEAR = StringVar()
ISBN = StringVar()
#Frame
Top = Frame(root,width=1200,height=100,bg='blue', bd=8, relief='raise')
Top.pack(side=TOP)
Left = Frame(root, width=1000, height=800,bg='red', bd=8, relief='raise')
Left.pack(side=LEFT)
Right = Frame(root,width=300,height=800,bg='yellow', bd=8,relief='raise')
Right.pack(side=RIGHT)
Forms = Frame(Left,width=1000,height=800)
Forms.pack(side=TOP)
Buttons = Frame(Left,width=1000,height=100,bd=8,relief='raise')
Buttons.pack(side=BOTTOM)
#Label widget
main_title =Label(Top,width=1000,font=('arial',24),text=".....Library Management Application.....")
main_title.pack()
txt_title = Label(Forms, text="Booktitle:",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_isbn = Label(Forms, text="ISBN:",font=('arial',16),bd=15)
txt_isbn.grid(row=3, stick="e")
txt_result = Label(Buttons)
txt_result.pack()
#Entry widget
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)
isbn = Entry(Forms,textvariable=ISBN, width=30)
isbn.grid(row=3,column=1)
#Buttons 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=LEFT)
#List Widet
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=200)
tree.column('#2', stretch=NO, minwidth=0, width=100)
tree.column('#3', stretch=NO, minwidth=0, width=50)
tree.column('#4', stretch=NO, minwidth=0, width=100)
tree.pack()
# Starting the main loop
window.mainloop()