1A.
import pickle
def display_menu():
print("Main Menu\n")
print("1. Add a new word")
print("2. Find the meaning")
print("3. Update a word")
print("4. Exit")
def add_word():
word = input("Enter the word: ")
meaning = input("Enter the meaning: ")
word_dict[word] = meaning
save_dictionary()
def find_meaning():
word = input("Enter the word to find its meaning: ")
if word in word_dict:
print(f"The meaning of '{word}' is: {word_dict[word]}")
else:
print(f"The meaning of '{word}' is not found.")
def update_word():
word = input("Enter the word to update its meaning: ")
if word in word_dict:
meaning = input("Enter the new meaning: ")
word_dict[word] = meaning
save_dictionary()
print(f"The meaning of '{word}' has been updated.")
else:
print(f"The word '{word}' is not found.")
def save_dictionary():
with open("words.txt", "wb") as file:
pickle.dump(word_dict, file)
def load_dictionary():
try:
with open("words.txt", "rb") as file:
return pickle.load(file)
except FileNotFoundError:
return {}
word_dict = load_dictionary()
while True:
display_menu()
choice = input("Enter Choice: ")
if choice == "1":
add_word()
elif choice == "2":
find_meaning()
elif choice == "3":
update_word()
elif choice == "4":
save_dictionary()
print("Exiting the program...")
break
else:
print("Invalid choice. Please try again.")
2B.import tkinter as tk
import sqlite3
def create_database():
conn = sqlite3.connect("books.db")
cursor = conn.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS books (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, author TEXT, year INTEGER, isbn TEXT)")
conn.commit()
conn.close()
def insert_book():
title = entry_title.get()
author = entry_author.get()
year = entry_year.get()
isbn = entry_isbn.get()
conn = sqlite3.connect("books.db")
cursor = conn.cursor()
cursor.execute("INSERT INTO books (title, author, year, isbn) VALUES (?, ?, ?, ?)", (title, author, year, isbn))
conn.commit()
conn.close()
clear_entries()
display_all_books()
def search_book():
title = entry_title.get()
author = entry_author.get()
year = entry_year.get()
isbn = entry_isbn.get()
conn = sqlite3.connect("books.db")
cursor = conn.cursor()
cursor.execute("SELECT * FROM books WHERE title=? OR author=? OR year=? OR isbn=?", (title, author, year, isbn))
rows = cursor.fetchall()
conn.close()
listbox.delete(0, tk.END)
for row in rows:
listbox.insert(tk.END, row)
def update_book():
selected_book = listbox.curselection()
if selected_book:
book_id = listbox.get(selected_book)[0]
new_title = entry_title.get()
new_author = entry_author.get()
new_year = entry_year.get()
new_isbn = entry_isbn.get()
conn = sqlite3.connect("books.db")
cursor = conn.cursor()
cursor.execute("UPDATE books SET title=?, author=?, year=?, isbn=? WHERE id=?", (new_title, new_author, new_year, new_isbn, book_id))
conn.commit()
conn.close()
clear_entries()
display_all_books()
def delete_book():
selected_book = listbox.curselection()
if selected_book:
book_id = listbox.get(selected_book)[0]
conn = sqlite3.connect("books.db")
cursor = conn.cursor()
cursor.execute("DELETE FROM books WHERE id=?", (book_id,))
conn.commit()
conn.close()
clear_entries()
display_all_books()
def clear_entries():
entry_title.delete(0, tk.END)
entry_author.delete(0, tk.END)
entry_year.delete(0, tk.END)
entry_isbn.delete(0, tk.END)
def display_all_books():
conn = sqlite3.connect("books.db")
cursor = conn.cursor()
cursor.execute("SELECT * FROM books")
rows = cursor.fetchall()
conn.close()
listbox.delete(0, tk.END)
for row in rows:
listbox.insert(tk.END, row)
# Create the database and table if they don't exist
create_database()
# Create the Tkinter GUI
window = tk.Tk()
window.title("Library Book Management System")
label_title = tk.Label(window, text="Title:")
label_title.grid(row=0, column=0)
entry_title = tk.Entry(window)
entry_title.grid(row=0, column=1)
label_author = tk.Label(window, text="Author:")
label_author.grid(row=1, column=0)
entry_author = tk.Entry(window)
entry_author.grid(row=1, column=1)
label_year = tk.Label(window, text="Year:")
label_year.grid(row=2, column=0)
entry_year = tk.Entry(window)
entry_year.grid(row=2, column=1)
label_isbn = tk.Label(window, text="ISBN:")
label_isbn.grid(row=3, column=0)
entry_isbn = tk.Entry(window)
entry_isbn.grid(row=3, column=1)
button_insert = tk.Button(window, text="Insert Book", command=insert_book)
button_insert.grid(row=4, column=0)
button_search = tk.Button(window, text="Search Book", command=search_book)
button_search.grid(row=4, column=1)
button_update = tk.Button(window, text="Update Book", command=update_book)
button_update.grid(row=4, column=2)
button_delete = tk.Button(window, text="Delete Book", command=delete_book)
button_delete.grid(row=4, column=3)
listbox = tk.Listbox(window, width=70)
listbox.grid(row=5, column=0, columnspan=4)
display_all_books()
window.mainloop()