Project 1 - English Dictionary App & Library Book Management System
# Project 1-A
import json
# English Dictionary App
# Load the dictionary from the words.txt file, if it exists
try:
with open("words.txt", "r") as file:
dictionary = json.load(file)
except FileNotFoundError:
dictionary = {}
# Display the main menu
def display_menu():
print("Main Menu")
print("1. Add a new word")
print("2. Find the meaning")
print("3. Update a word")
print("4. Exit")
# Add a new word to the dictionary
def add_word():
word = input("Enter the word: ")
definition = input("Enter the definition: ")
dictionary[word] = definition
with open("words.txt", "w") as file:
json.dump(dictionary, file)
print("Word added successfully.")
# Find the meaning of a word in the dictionary
def find_word():
word = input("Enter the word to search: ")
if word in dictionary:
print("Definition:", dictionary[word])
else:
print("Word not found.")
# Update the definition of an existing word in the dictionary
def update_word():
word = input("Enter the word to update: ")
if word in dictionary:
definition = input("Enter the new definition: ")
dictionary[word] = definition
with open("words.txt", "w") as file:
json.dump(dictionary, file)
print("Word updated successfully.")
else:
print("Word not found.")
# Loop until the user chooses to exit
while True:
display_menu()
choice = input("Enter your choice: ")
if choice == "1":
add_word()
elif choice == "2":
find_word()
elif choice == "3":
update_word()
elif choice == "4":
print("Goodbye!")
break
else:
print("Invalid choice. Please try again.")
# Project 1-B
import tkinter as tk
import sqlite3
# Create a connection to the database
conn = sqlite3.connect("books.db")
c = conn.cursor()
# Create the books table if it does not exist
c.execute(
"""
CREATE TABLE IF NOT EXISTS books (
id INTEGER PRIMARY KEY,
title TEXT,
author TEXT,
year INTEGER,
isbn TEXT
)
"""
)
conn.commit()
# Define the GUI
class BookManager(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.master.title("Library Book Management System")
self.create_widgets()
def create_widgets(self):
# Create the title label and entry
tk.Label(self.master, text="Title:").grid(row=0, column=0)
self.title_entry = tk.Entry(self.master)
self.title_entry.grid(row=0, column=1)
# Create the author label and entry
tk.Label(self.master, text="Author:").grid(row=1, column=0)
self.author_entry = tk.Entry(self.master)
self.author_entry.grid(row=1, column=1)
# Create the year label and entry
tk.Label(self.master, text="Year:").grid(row=2, column=0)
self.year_entry = tk.Entry(self.master)
self.year_entry.grid(row=2, column=1)
# Create the ISBN label and entry
tk.Label(self.master, text="ISBN:").grid(row=3, column=0)
self.isbn_entry = tk.Entry(self.master)
self.isbn_entry.grid(row=3, column=1)
# Create the buttons
self.add_button = tk.Button(self.master, text="Add Book", command=self.add_book)
self.add_button.grid(row=4, column=0)
self.update_button = tk.Button(self.master, text="Update Book", command=self.update_book)
self.update_button.grid(row=4, column=1)
self.delete_button = tk.Button(self.master, text="Delete Book", command=self.delete_book)
self.delete_button.grid(row=4, column=2)
self.search_button = tk.Button(self.master, text="Search Books", command=self.search_books)
self.search_button.grid(row=5, column=0)
self.view_all_button = tk.Button(self.master, text="View All Books", command=self.view_all_books)
self.view_all_button.grid(row=5, column=1)
self.quit_button = tk.Button(self.master, text="Quit", command=self.master.quit)
self.quit_button.grid(row=5, column=2)
# Create the listbox
self.listbox = tk.Listbox(self.master, height=10, width=60)
self.listbox.grid(row=6, column=0, columnspan=3)
def add_book(self):
# Get the values from the entries
title = self.title_entry.get()
author = self.author_entry.get()
year = self.year_entry.get()
isbn = self.isbn_entry.get()
# Insert the values into the database
c.execute("INSERT INTO books (title, author, year, isbn) VALUES (?, ?, ?, ?)", (title, author, year, isbn))
conn.commit()
# Clear the entries
self.title_entry.delete(0, tk.END)
self.author_entry.delete(0, tk.END)
self.year_entry.delete(0, tk.END)
self.isbn_entry.delete(0, tk.END)
# Update the listbox
self.view_all_books()
def update_book(self):
# Get the selected book from the listbox
try:
selected_book = self.listbox.get(self.listbox.curselection())
except tk.TclError:
return
# Get the values from the entries
title = self.title_entry.get()
author = self.author_entry.get()
year = self.year_entry.get()
isbn = self.isbn_entry.get()
# Update the values in the database
c.execute(
"UPDATE books SET title=?, author=?, year=?, isbn=? WHERE id=?",
(title, author, year, isbn, selected_book.split(":")[0])
)
conn.commit()
# Clear the entries
self.title_entry.delete(0, tk.END)
self.author_entry.delete(0, tk.END)
self.year_entry.delete(0, tk.END)
self.isbn_entry.delete(0, tk.END)
# Update the listbox
self.view_all_books()
def delete_book(self):
# Get the selected book from the listbox
try:
selected_book = self.listbox.get(self.listbox.curselection())
except tk.TclError:
return
# Delete the book from the database
c.execute("DELETE FROM books WHERE id=?", (selected_book.split(":")[0],))
conn.commit()
# Clear the entries
self.title_entry.delete(0, tk.END)
self.author_entry.delete(0, tk.END)
self.year_entry.delete(0, tk.END)
self.isbn_entry.delete(0, tk.END)
# Update the listbox
self.view_all_books()
def search_books(self):
# Get the values from the entries
title = self.title_entry.get()
author = self.author_entry.get()
year = self.year_entry.get()
isbn = self.isbn_entry.get()
# Build the query
query = "SELECT * FROM books WHERE 1=1"
if title:
query += " AND title LIKE ?"
if author:
query += " AND author LIKE ?"
if year:
query += " AND year=?"
if isbn:
query += " AND isbn=?"
query += " ORDER BY title"
# Execute the query
c.execute(query, (f"%{title}%", f"%{author}%", year, isbn))
books = c.fetchall()
# Clear the listbox
self.listbox.delete(0, tk.END)
# Add the books to the listbox
for book in books:
self.listbox.insert(tk.END, f"{book[0]}: {book[1]} by {book[2]} ({book[3]})")
def view_all_books(self):
# Get all the books from the database
c.execute("SELECT * FROM books ORDER BY title")
books = c.fetchall()
# Clear the listbox
self.listbox.delete(0, tk.END)
# Add the books to the listbox
for book in books:
self.listbox.insert(tk.END, f"{book[0]}: {book[1]} by {book[2]} ({book[3]})")
# Create the GUI window
root = tk.Tk()
# Create the BookManager widget
book_manager = BookManager(root)
# Run the GUI loop
book_manager.mainloop()
# Close the database connection
conn.close()