Offline Dictionary App

import tkinter as tk

from tkinter import messagebox

import sqlite3

import pyttsx3


# -----------------------------

# Database Setup

# -----------------------------

def init_db():

    conn = sqlite3.connect("dictionary.db")

    cur = conn.cursor()


    cur.execute("""

        CREATE TABLE IF NOT EXISTS words (

            word TEXT PRIMARY KEY,

            meaning TEXT

        );

    """)


    cur.execute("""

        CREATE TABLE IF NOT EXISTS favorites (

            word TEXT PRIMARY KEY

        );

    """)


    conn.commit()

    conn.close()


# -----------------------------

# Insert sample words (optional)

# -----------------------------

def insert_sample_words():

    sample_data = {

        "python": "A high-level programming language used for general-purpose programming.",

        "algorithm": "A step-by-step procedure for solving a problem or performing a task.",

        "variable": "A storage location paired with a name used to store values.",

        "database": "A structured collection of data stored electronically.",

    }


    conn = sqlite3.connect("dictionary.db")

    cur = conn.cursor()

    

    for word, meaning in sample_data.items():

        cur.execute("INSERT OR IGNORE INTO words VALUES (?, ?)", (word, meaning))


    conn.commit()

    conn.close()


# -----------------------------

# Text-to-Speech

# -----------------------------

engine = pyttsx3.init()


def speak_word(word):

    engine.say(word)

    engine.runAndWait()


# -----------------------------

# Dictionary Operations

# -----------------------------

def search_word():

    word = entry_word.get().strip().lower()

    if not word:

        messagebox.showerror("Error", "Please enter a word to search.")

        return


    conn = sqlite3.connect("dictionary.db")

    cur = conn.cursor()


    cur.execute("SELECT meaning FROM words WHERE word=?", (word,))

    result = cur.fetchone()


    if result:

        text_meaning.config(state="normal")

        text_meaning.delete(1.0, tk.END)

        text_meaning.insert(tk.END, result[0])

        text_meaning.config(state="disabled")

    else:

        messagebox.showinfo("Not Found", "Word not found in offline dictionary.")

    

    conn.close()


def add_word():

    word = entry_add_word.get().strip().lower()

    meaning = text_add_meaning.get(1.0, tk.END).strip()


    if not word or not meaning:

        messagebox.showerror("Error", "Both word and meaning are required.")

        return


    conn = sqlite3.connect("dictionary.db")

    cur = conn.cursor()


    cur.execute("INSERT OR REPLACE INTO words VALUES (?, ?)", (word, meaning))

    conn.commit()

    conn.close()


    messagebox.showinfo("Success", f"'{word}' added to dictionary.")


def add_to_favorites():

    word = entry_word.get().strip().lower()


    if not word:

        messagebox.showerror("Error", "Search a word first before adding to favorites.")

        return


    conn = sqlite3.connect("dictionary.db")

    cur = conn.cursor()

    cur.execute("INSERT OR IGNORE INTO favorites VALUES (?)", (word,))

    conn.commit()

    conn.close()


    messagebox.showinfo("Added", f"'{word}' added to favorites!")


def show_favorites():

    conn = sqlite3.connect("dictionary.db")

    cur = conn.cursor()

    cur.execute("SELECT word FROM favorites")

    favs = cur.fetchall()

    conn.close()


    fav_list = "\n".join([w[0] for w in favs]) if favs else "No favorites added yet."


    messagebox.showinfo("Favorite Words", fav_list)


# -----------------------------

# GUI Setup

# -----------------------------

root = tk.Tk()

root.title("Offline Dictionary App")

root.geometry("650x500")

root.resizable(False, False)


# Search section

tk.Label(root, text="Enter Word:", font=("Arial", 14)).pack(pady=5)

entry_word = tk.Entry(root, font=("Arial", 14), width=30)

entry_word.pack()


btn_search = tk.Button(root, text="Search", font=("Arial", 12), command=search_word)

btn_search.pack(pady=5)


btn_speak = tk.Button(root, text="🔊 Speak", font=("Arial", 12), command=lambda: speak_word(entry_word.get()))

btn_speak.pack(pady=2)


btn_fav = tk.Button(root, text="⭐ Add to Favorites", font=("Arial", 12), command=add_to_favorites)

btn_fav.pack(pady=2)


# Meaning display

tk.Label(root, text="Meaning:", font=("Arial", 14)).pack()

text_meaning = tk.Text(root, height=6, width=60, font=("Arial", 12), state="disabled")

text_meaning.pack(pady=5)


# Add new word section

tk.Label(root, text="Add New Word:", font=("Arial", 14)).pack(pady=5)

entry_add_word = tk.Entry(root, font=("Arial", 12), width=30)

entry_add_word.pack()


tk.Label(root, text="Meaning:", font=("Arial", 14)).pack()

text_add_meaning = tk.Text(root, height=4, width=60, font=("Arial", 12))

text_add_meaning.pack()


btn_add = tk.Button(root, text="Add Word to Dictionary", font=("Arial", 12), command=add_word)

btn_add.pack(pady=5)


# favorites

btn_show_fav = tk.Button(root, text="📌 Show Favorites", font=("Arial", 12), command=show_favorites)

btn_show_fav.pack(pady=5)


# Run

init_db()

insert_sample_words()

root.mainloop()


No comments: