Project 1 - English Dictionary App & Library Book Management System
1A: English Dictionary App :
def display():
print('''Main Menu \n
1. Add a word \n
2. Find the Meaning \n
3. Update a word \n
4. Exit \n
''')
import json
d = {}
display()
while True:
choice = int(input("Select from Main Menu (1,2,3,4) : "))
if choice == 1:
word = input("Enter a word : ")
meaning = input("Enter the meaning : ")
d[word] = meaning
with open("words.txt", 'w') as f:
a = json.dump(d,f)
print('Word added sucessfully')
print(' '*50)
elif choice == 2:
word = input('Enter a word to search : ')
with open("words.txt", 'r') as f1:
m = json.load(f1)
print("Meaning of the ",word," : ", m[word])
print(' '*50)
elif choice == 3:
word = input("Enter a word : ")
update = input("Enter the meaning to update : ")
with open ("words.txt", 'w') as f2:
m[word] = update
u = json.dump(m,f2)
print(' '*50)
elif choice == 4:
exit = input("Do you want to exit? (y/n) : ")
if exit == 'y' or exit == 'Y':
break
else:
print("Please select the correct option from Main Menu")
1B: Library Book Management System :
from tkinter import *
import tkinter as tk
import sqlite3
import tkinter.ttk as ttk
import tkinter.messagebox as tkmessagebox
root = Tk()
root.title = ("....Library Management Application....")
def DB():
global conn, cursor
conn = sqlite3.connect("books.db")
cursor = conn.cursor
cursor.execute("CREATE TABLE IF NOT EXISTS lib(Booktitle TEXT, Author TEXT, Year INTEGER, ISBN INTEGER)")
def Add():
if ((Booktitle.get() == '') or (Author.get() == '') or (Year.get() == '') or (ISBN.get() == '')):
tk.messagebox.showinfo('Library', 'Please enter all the fields', fg = 'red')
else:
DB()
cursor.execute("INSERT INTO lib (Booktitle, Author, Year, ISBN) VALUES(?,?,?,?)",(Booktitle.get(), Author.get(), Year.get(), ISBN.get()))
conn.commit()
Booktitle.set("")
Author.set("")
Year.set("")
ISBN.set("")
cursor.close()
conn.close()
text_result.config(text = 'New Book Added', fg = 'green')
def Search():
tree.delete(*tree.get_children())
DB()
if ISBN.get() != 0:
cursor.execute("Select * from lib WHERE ISBN = ?", (ISBN.get(),))
rows = cursor.fetchall()
for data in rows:
tree.insert('','end', values = (data[0], data[1], data[2], data[3]))
conn.close()
text_result.config(text = "Sucessfully fetched the book. ", fg = 'black')
elif Booktitle.get() != '' and Author.get() != '' and Year.get() != 0:
cursor.execute("Select * from lib WHERE Booktitle = ? AND Author = ? AND Year = ?", (Booktitle.get(), Author.get(), Year.get()))
rows = cursor.fetchall()
for data in rows:
tree.insert('','end', values = (data[0], data[1], data[2], data[3]))
conn.close()
text_result.config(text = "Sucessfully fetched the book. ", fg = 'black')
elif Booktitle.get() != '' and Author.get() != '' :
cursor.execute("Select * from lib WHERE Booktitle = ? AND Author = ? ", (Booktitle.get(), Author.get()))
rows = cursor.fetchall()
for data in rows:
tree.insert('','end', values = (data[0], data[1], data[2], data[3]))
conn.close()
text_result.config(text = "Sucessfully fetched the book. ", fg = 'black')
elif Booktitle.get() != '' and Year.get() != 0:
cursor.execute("Select * from lib WHERE Booktitle = ? AND Year = ?", (Booktitle.get(), Year.get()))
rows = cursor.fetchall()
for data in rows:
tree.insert('','end', values = (data[0], data[1], data[2], data[3]))
conn.close()
text_result.config(text = "Sucessfully fetched the book. ", fg = 'black')
elif Author.get() != '' and Year.get() != 0:
cursor.execute("Select * from lib WHERE Author = ? AND Year = ?", (Author.get(), Year.get()))
rows = cursor.fetchall()
for data in rows:
tree.insert('','end', values = (data[0], data[1], data[2], data[3]))
conn.close()
text_result.config(text = "Sucessfully fetched the book. ", fg = 'black')
elif Booktitle.get() != '' or Author.get() != '' or Year.get() != 0:
cursor.execute("Select * from lib WHERE Booktitle = ? OR Author = ? OR Year = ?", (Booktitle.get(), Author.get(), Year.get()))
rows = cursor.fetchall()
for data in rows:
tree.insert('','end', values = (data[0], data[1], data[2], data[3]))
conn.close()
text_result.config(text = "Sucessfully fetched the book. ", fg = 'black')
elif Booktitle.get() == '' and Author.get() == '' and Year.get() == 0 and ISBN.get() == 0:
text_result.config(text = "Enter atleast single identity", fg = 'red')
conn.close()
def Issue():
DB()
sel = tree.selection()
if len(sel) > 0:
tree.delete(sel)
text_result.config(text = "Successfully Issued. ", fg = "green")
else:
text_result.config(text = "Select a book from library", fg = "black")
conn.close()
def Delete():
tree.delete(*tree.get_children())
DB()
if ISBN.get() != 0:
cursor.execute("DELETE FROM lib WHERE ISBN = ?", (ISBN.get(),))
conn.commit()
text_result.config(text = "Removed Successfully.", fg = "green")
else:
text_result.config(text = "Enter ISBN to delete ", fg = "red")
conn.close()
def Display():
tree.delete(*tree.get_children())
DB()
cursor.execute("Select * FROM lib")
fetch = cursor.fetchall()
for data in fetch :
tree.insert('','end', values = (data[0], data[1], data[2], data[3]))
cursor.close()
text_result.config(text = "Sucessfully displayed the book. ", fg = 'black')
def Exit():
result = tkmessagebox.askquestion('Library', "Do you want to exit", icon = 'warning')
if result == 'yes':
root.destroy()
root = Tk()
root.title = ("....Library Management Application....")
Top = Frame(root, width = 900, height = 60, bd = 8, relief = 'raise', bg = 'BLUE')
Top.pack(side = TOP)
l = Label(Top, text = "....Library Management Application....", font = ('arial', 24))
l.pack()
Left = Frame(root, width = 400, height = 100, bd = 8, relief = 'raise', bg = 'RED')
Left.pack(side = LEFT)
Right = Frame(root, width = 300, height = 100, bd = 8, relief = 'raise', bg = 'YELLOW')
Right.pack(side = RIGHT)
Forms = Frame(Left, width = 500, height = 500)
Forms.pack(side = TOP)
Buttons = Frame(Left, width = 300, height = 60, bd = 8, relief = 'raise')
Buttons.pack(side = BOTTOM)
l1 = Label(Forms, text = 'Booktitle: ', font = ('arial', 18), bd = 15)
l1.grid(row = 0, column = 0)
l2 = Label(Forms, text = 'Author: ', font = ('arial', 18), bd = 15)
l2.grid(row = 1, column = 0)
l3 = Label(Forms, text = 'Year: ', font = ('arial', 18), bd = 15)
l3.grid(row = 2, column = 0)
l4 = Label(Forms, text = 'ISBN: ', font = ('arial', 18), bd = 15)
l4.grid(row = 3, column = 0)
text_result = Label(Buttons)
text_result.pack(side = TOP)
Booktitle = StringVar()
Author = StringVar()
Year = IntVar()
ISBN = IntVar()
e1 = Entry(Forms, textvariable = Booktitle, width = 30)
e1.grid(row = 0, column = 1)
e2 = Entry(Forms, textvariable = Author, width = 30)
e2.grid(row = 1, column = 1)
e3 = Entry(Forms, textvariable = Year, width = 30)
e3.grid(row = 2, column = 1)
e4 = Entry(Forms, textvariable = ISBN, width = 30)
e4.grid(row = 3, column = 1)
b1 = Button(Buttons, width = 10, text = 'Display', command = Display)
b1.pack(side = LEFT)
b2 = Button(Buttons, width = 10, text = 'Search', command = Search)
b2.pack(side = LEFT)
b3 = Button(Buttons, width = 10, text = 'Add', command = Add)
b3.pack(side = LEFT)
b4 = Button(Buttons, width = 10, text = 'Issue', command = Issue)
b4.pack(side = LEFT)
b5 = Button(Buttons, width = 10, text = 'Delete', command = Delete)
b5.pack(side = LEFT)
b6 = Button(Buttons, width = 10, text = 'Exit', command = Exit)
b6.pack(side = LEFT)
sby = Scrollbar(Right, orient = VERTICAL)
sbx = Scrollbar(Right, orient = HORIZONTAL)
tree = ttk.Treeview(Right, columns = ("Booktitle", "Author", "Year", "ISBN"))
sby.config(command = tree.yview)
sby.pack(side = RIGHT, fill = Y)
sbx.config(command = tree.xview)
sbx.pack(side = BOTTOM, fill = X)
tree.heading("Booktitle", text = "Booktitle", anchor = W)
tree.column('#0', stretch = NO, minwidth = 0, width = 0)
tree.heading("Author", text = "Author", anchor = W)
tree.column('#1', stretch = NO, minwidth = 0, width = 200)
tree.heading("Year", text = "Year", anchor = W)
tree.column('#2', stretch = NO, minwidth = 0, width = 150)
tree.heading("ISBN", text = "ISBN", anchor = W)
tree.column('#3', stretch = NO, minwidth = 0, width = 80)
tree.pack()
root.mainloop()