Project 1 - English Dictionary App & Library Book Management System
# 1.
import pickle
# Define a function to add a new word to the dictionary
def add_word(dictionary):
word = input("Enter the word: ")
meaning = input("Enter the meaning: ")
dictionary[word] = meaning
with open("words.txt", "wb") as f:
pickle.dump(dictionary, f)
print("Word added successfully.")
return dictionary
# Define a function to find the meaning of a word
def find_meaning(dictionary):
word = input("Enter the word: ")
if word in dictionary:
print(f"The meaning of '{word}' is '{dictionary[word]}'.")
else:
print(f"Sorry, '{word}' was not found in the dictionary.")
return dictionary
# Define a function to update the meaning of a word
def update_word(dictionary):
word = input("Enter the word: ")
if word in dictionary:
meaning = input("Enter the new meaning: ")
dictionary[word] = meaning
with open("words.txt", "wb") as f:
pickle.dump(dictionary, f)
print("Word updated successfully.")
else:
print(f"Sorry, '{word}' was not found in the dictionary.")
return dictionary
# Load the dictionary from the file
try:
with open("words.txt", "rb") as f:
dictionary = pickle.load(f)
except FileNotFoundError:
dictionary = {}
# Display the main menu and accept user input
while True:
print("\nMain Menu\n")
print("1. Add a new word")
print("2. Find the meaning")
print("3. Update a word")
print("4. Exit")
choice = input("\nEnter Choice: ")
if choice == "1":
dictionary = add_word(dictionary)
elif choice == "2":
dictionary = find_meaning(dictionary)
elif choice == "3":
dictionary = update_word(dictionary)
elif choice == "4":
print("Exiting program...")
break
else:
print("Invalid choice. Please try again.")
# 2.
# liabraries
from tkinter import *
import sqlite3
import tkinter.ttk as ttk
import tkinter.messagebox as tkMessagebox
root = Tk()
root.title('Library Management Application')
# Methods
def Database():
global conn, cursor
conn = sqlite3.connect('books.db')
cursor = conn.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS books (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 the fields",fd="red")
else:
Database()
cursor.execute("INSERT INTO BOOKS(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 Inserted !",fg="green")
def Read():
tree.delete(*tree.get_children)
DATABASE()
cursor.execute("SELECT * FROM books ORDER BY author ")
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="black")
def Exit():
result = tkMessageBox.askquestion('Do you want to exit ? (y/n)', icon='warning')
if result == 'yes':
root.destroy()
exit()
def Delete():
if books.db:
cursor.execute("DELETE FROM books")
conn.commit()
tkMessageBox.showinfo("Success","Book deleted from database")
else:
tkMessageBox.showerror("Error","Please select a book from list")
# Variable decalration
BOOKTITLE = StringVar()
AUTHOR = StringVar()
YEAR = 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)
Buttons=Frame(Left, width=300,height=100,bd=8,relief='raise')
Buttons.pack(side=BOTTOM)
# Labels
txt_title=Label(Top, width=900, font=('arial', 24), text = "Python: 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=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(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_display = Button(Buttons, width=10, text="Display", command=Create)
btn_display.pack(side=LEFT)
btn_create = Button(Buttons, width=10, text="Search", command=Create)
btn_create.pack(side=LEFT)
btn_add = Button(Buttons, width=10, text="Add", command=Create)
btn_add.pack(side=LEFT)
btn_issue = Button(Buttons, width=10, text="Issue", command=Create)
btn_issue.pack(side=LEFT)
btn_delete = Button(Buttons, width=10, text="Delete", command=Create)
btn_delete.pack(side=LEFT)
btn_exit = Button(Buttons, width=10, text="Exit", command=Create)
btn_exit.pack(side=LEFT)
# 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()
# INITIALIZATION
if __name__== '__main__':
root.mainloop()