Project 1 - English Dictionary App & Library Book Management System
AIM : Creating an dictionary and library gui based application using the python
Objective :
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.
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.
code :
# creating an dictionary
#importing the req. files
import os
import json
#file name
fileName = "Dictionary"
#using conditional statements for the logic and conditions
if os.path.isfile(fileName):
f = open(fileName,'a')
print(' File already Exists \n') #printting to the screen
else:
f = open(fileName,'w') #writting to the file
dic = {'add':'sum'}
json.dump(dic,f)
f.close() #close the file
def add_new_word(word,meaning): #defining the functions
f1 = open("Dictionary",'r') #creating a dictionery out of text file
temp_dict = json.load(f1)
f1.close #close the file
if word in temp_dict.keys():
print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n")
print ('%s already exists if you want to update the meaning of %s use option 3' % (word,word))
print("\n $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ \n")
else:
f1_1 = open("Dictionary",'w') #writting to the file
temp_dict[word] = meaning
json.dump(temp_dict,f1_1)
f1_1.close #close the file
def find_meaning(word): #defining the functions
#creating a dictionery out of text file
# = open("Dictionary",'w')
f2 = open("Dictionary",'r')
temp_dict = json.load(f2)
f2.close
if word in temp_dict.keys():
print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ \n")
print ('meaning of %s is : %s' % (word,temp_dict[word]))
print("\n $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ \n")
else:
print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ \n")
print('meaning of %s not found choose option 3 to enter its meaning' %(word))
print("\n $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ \n")
def word_update(word): #defining the functions
#creating a dictionery out of text file
# = open("Dictionary",'w')
f3 = open("Dictionary",'r')
temp_dict = json.load(f3)
f3.close
if word in temp_dict.keys():
new_meaning = input("Enter the new meaning : ")
f3_1 = open("Dictionary",'w')
temp_dict[word] = new_meaning
json.dump(temp_dict,f3_1)
f3_1.close
else:
print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$== \n")
print ('No word such as %s found in the dictionay to add its meaning use option 1\n' % (word))
print("\n $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ \n")
while True:
print("WELCOME TO THE Dictionary APP !! \n")
print ("Enter 1 to add a new word to the Dictionary \n")
print ("Enter 2 to find the meaning of the word in the Dictionary \n")
print ("Enter 3 to update the new meaning of a word \n")
print ("Enter 4 to EXIT the Dictionary \n")
print("\n")
option = input('choose any option from the given choices for the required action ')
if option == "1":
word1 = input('Enter the word for which you want to add meaning for :')
meaning1 = input('Enter the meaning of the above word :')
add_new_word(word1,meaning1)
elif option == "2":
word2 = input('Enter the word for whcich you want to know the meaning : ')
find_meaning(word2)
elif option == "3":
word3 = input('Enter the word for which you want to update meaning for :')
word_update(word3)
elif option == "4":
print("\n \n")
print("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")
print("THANK YOU FOR USING THE Dictionary")
print("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")
print("\n \n")
break #if conditions fails terminate the program
#creating an gui based dictionary app using python and some librarys
#IMPORTTING
import sqlite3
def initial_database(): #DEFINNING THE FUNCTIONS
'''
call me, i'll initiate and give you a connection and a cursor to my database
'''
connection = sqlite3.connect("LBMS.db")
cursor = connection.cursor()
print("Database connection established successfully")
creating_table(cursor)
return(connection, cursor) #returnning the output to the function
def terminate_database(connection): #DEFINNING THE FUNCTIONS
'''
give me a database connection i'll terminate it
'''
connection.close()
print("Database connection de-established successfully")
def creating_table(cursor): #DEFINNING THE FUNCTIONS
'''
give me a cursor to database i'll create book table in it
'''
sql_command = """CREATE TABLE if not exists books (
title varchar(60) not null,
author varchar(30) not null,
year INTEGER not null,
ISBN INTEGER,
unique (title, author, year, ISBN)
);"""
cursor.execute(sql_command)
def reading_table(cursor): #DEFINNING THE FUNCTIONS
'''
give me a cursor to database, i'll read it all and send you
'''
sql_command = "SELECT * FROM books"
cursor.execute(sql_command)
data = cursor.fetchall()
return(data) #returnning the output to the function
def adding_to_table(cursor, title, author, year, isbn): #DEFINNING THE FUNCTIONS
'''
give me a cursor to database, and a book, i'll add it to the books table
'''
sql_command = f"INSERT INTO books VALUES ('{title}', '{author}', {year}, {isbn});"
try:
cursor.execute(sql_command)
return(0, "success")
except sqlite3.IntegrityError as e:
return(111, e) #returnning the output to the function
except sqlite3.OperationalError as e:
return(112, e) #returnning the output to the function
def removing_from_table(cursor, title, author, year, isbn): #DEFINNING THE FUNCTIONS
'''
give me a cursor to database, and a book, i'll remove it.
'''
try:
sql_command = f'''DELETE FROM books WHERE title="{title}";'''
cursor.execute(sql_command)
sql_command = f'''DELETE FROM books WHERE author="{author}";'''
cursor.execute(sql_command)
sql_command = f'''DELETE FROM books WHERE year="{year}";'''
cursor.execute(sql_command)
sql_command = f'''DELETE FROM books WHERE isbn="{isbn}";'''
cursor.execute(sql_command)
except:
return(-1) #returnning the output to the function
def searching_from_table(cursor, title, author, year, isbn): #DEFINNING THE FUNCTIONS
'''
give me a cursor to database, and a book, i'll search it.
'''
try:
cursor.execute("SELECT * FROM books WHERE title=?", (title,))
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) #returnning the output to the function
def saving_changes(connection):
'''
give me a connection to database i'll save the changes
'''
connection.commit()
# database_gui_interface
def listing_all_books(listArea): #DEFINNING THE FUNCTIONS
connection, cursor = initial_database()
print("listing books")
books = reading_table(cursor)
listArea.delete('0','end')
for i in range(len(books)):
listArea.insert(i,f"{books[i][0]}, {books[i][1]}, {books[i][2]}, {books[i][3]}")
terminate_database(connection)
def searching_books(listArea, title, author, year, isbn): #DEFINNING THE FUNCTIONS
connection, cursor = initial_database()
print("search book")
title = title.get()
author = author.get()
year = year.get()
isbn = isbn.get()
res = searching_from_table(cursor, title, author, year, isbn)
if res==-1:
messagebox.showerror("Error!", "something went wrong!")
if res!=-1:
listArea.delete('0','end')
for i in range(len(res)):
listArea.insert(i,res[i])
terminate_database(connection)
def adding_books(title, author, year, isbn): #DEFINNING THE FUNCTIONS
connection, cursor = initial_database()
print("adding book")
title = title.get()
author = author.get()
year = year.get()
isbn = isbn.get()
code, error = adding_to_table(cursor, title, author, year, isbn)
if code==111:
messagebox.showerror("Error!", f"Book Already Exists!\n\nerror code: {code}\nerror:{error}")
if code==112:
messagebox.showerror("Error!", f"Please, fill in the input fields appropriately.\n\ntitle: string, author: string, year: integer, year: integer.\n\nerror code: {code}\nerror:{error}")
saving_changes(connection)
terminate_database(connection)
def issue_book(title, author, year, isbn): #DEFINNING THE FUNCTIONS
connection, cursor = initial_database()
print("issue book")
messagebox.showwarning("Undefined", "this function is not yet defined!")
terminate_database(connection)
def deleting_book(title, author, year, isbn): #DEFINNING THE FUNCTIONS
connection, cursor = initial_database()
print("delte book")
title = title.get()
author = author.get()
year = year.get()
isbn = isbn.get()
code = removing_from_table(cursor, title, author, year, isbn)
if code==-1:
messagebox.showerror("Error!", "something went wrong!")
messagebox.showinfo("Deleted!", f"Deleted the book: {title}, {author}, {year}, {isbn}")
saving_changes(connection)
terminate_database(connection)
#Graphical user interface creation
#importing
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
def gui(): #DEFINNING THE FUNCTIONS
# header to the interface
root = Tk()
root.title("Library Book Management System BY DHANUNJAYA")
# the widigets in the dictionary app
title = StringVar()
author = StringVar()
year = StringVar()
isbn = StringVar()
title_label = Label(root, text="title:", padx=10)
author_label = Label(root, text="author:", padx=10)
year_label = Label(root, text="year:", padx=10)
isbn_label = Label(root, text="isbn:", padx=10)
title_entry = Entry(root, textvariable=title)
author_entry = Entry(root, textvariable=author)
year_entry = Entry(root, textvariable=year)
isbn_entry = Entry(root, textvariable=isbn)
scrollbar = Scrollbar(root, orient="vertical")
listArea = Listbox(root, bg='red', width=200, yscrollcommand=scrollbar.set)
list_all_btn = Button(root, text="List All", command=lambda:listing_all_books(listArea))
search_btn = Button(root, text="Search", command=lambda:searching_books(listArea, title, author, year, isbn))
add_btn = Button(root, text="Add", command=lambda:adding_books(title, author, year, isbn))
del_btn = Button(root, text="delete", command=lambda:deleting_book(title, author, year, isbn))
exit_btn = Button(root, text="Exit", command=root.destroy)
scrollbar.config(command=listArea.yview)
title_label.grid(row=0, column=0)
author_label.grid(row=0, column=2)
year_label.grid(row=0, column=4)
isbn_label.grid(row=0, column=6)
title_entry.grid(row=0, column=1)
author_entry.grid(row=0, column=3)
year_entry.grid(row=0, column=5)
isbn_entry.grid(row=0, column=7)
listArea.grid(row=1, column=0, columnspan=8, sticky="nsew")
scrollbar.grid(row=1, column=9, sticky="ns")
list_all_btn.grid(row=2, column=0)
search_btn.grid(row=2, column=1)
add_btn.grid(row=2, column=2)
del_btn.grid(row=2, column=3)
exit_btn.grid(row=2, column=4)
# looping
print("GUI loop INITIATING")
root.mainloop()
print("GUI loop TERMINATED")
#running the app
gui()
RESULT : The required criteria of creating an dictionary and an library based gui is created and excueted by using the python.