In this project, we will create a Personal Diary Application using Python, allowing users to write, save, and view their daily diary entries. We’ll use Tkinter for the graphical user interface (GUI) and SQLite to store the diary entries.
1. Project Setup
Modules Required:
- tkinter: For creating the graphical user interface.
- sqlite3: To store the diary entries in a local database.
Install the necessary modules:
pip install tkinter
2. Project Features
- Add Diary Entry: Users can write a new diary entry with a title and body.
- View Diary Entries: Users can view previously saved diary entries.
- Edit/Delete Entries: Users can edit or delete their past entries.
- Search Entries: Users can search for a specific entry by title.
- Database Integration: Use SQLite to store and manage diary entries.
3. Database Design
We will create a single table in SQLite to store diary entries:
- Diary Table:
- id: (INTEGER PRIMARY KEY AUTOINCREMENT)
- title: (TEXT)
- entry: (TEXT)
- date: (TEXT)
4. Code Structure
We will divide the project into four main sections:
- Creating the GUI: The interface for the user to interact with the application.
- Handling Diary Entry Logic: Adding, viewing, updating, and deleting diary entries.
- Database Connection: Creating the database and storing/retrieving entries.
- Search Functionality: Allowing users to search their diary entries by title.
5. Creating the Database
Let’s first define the database connection and table creation:
import sqlite3
from datetime import datetime
# Connect to SQLite database and create tables
def connect_db():
conn = sqlite3.connect('diary.db')
c = conn.cursor()
# Create Diary Table
c.execute('''CREATE TABLE IF NOT EXISTS diary
(id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT,
entry TEXT,
date TEXT)''')
conn.commit()
conn.close()
connect_db()
6. Diary Entry Functions
A. Add New Entry
def add_entry(title, entry):
conn = sqlite3.connect('diary.db')
c = conn.cursor()
# Get current date
date = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# Insert the new entry into the diary table
c.execute("INSERT INTO diary (title, entry, date) VALUES (?, ?, ?)", (title, entry, date))
conn.commit()
conn.close()
B. View All Entries
def view_entries():
conn = sqlite3.connect('diary.db')
c = conn.cursor()
# Select all entries from the diary table
c.execute("SELECT * FROM diary")
entries = c.fetchall()
conn.close()
return entries
C. Update an Entry
def update_entry(entry_id, new_title, new_entry):
conn = sqlite3.connect('diary.db')
c = conn.cursor()
# Update the title and body of the entry
c.execute("UPDATE diary SET title = ?, entry = ? WHERE id = ?", (new_title, new_entry, entry_id))
conn.commit()
conn.close()
D. Delete an Entry
def delete_entry(entry_id):
conn = sqlite3.connect('diary.db')
c = conn.cursor()
# Delete the entry from the diary table
c.execute("DELETE FROM diary WHERE id = ?", (entry_id,))
conn.commit()
conn.close()
7. Building the GUI with Tkinter
We will create a graphical interface using Tkinter for users to add, view, and manage their diary entries.
A. Main Window
from tkinter import *
from tkinter import messagebox
# Function to add a new diary entry
def save_diary_entry():
title = title_entry.get()
entry = diary_text.get("1.0", END)
if title and entry:
add_entry(title, entry)
messagebox.showinfo("Success", "Diary entry saved!")
title_entry.delete(0, END)
diary_text.delete("1.0", END)
else:
messagebox.showwarning("Error", "Title and entry cannot be empty!")
# Function to display all diary entries
def display_entries():
entries_window = Toplevel(root)
entries_window.title("View Diary Entries")
entries = view_entries()
for entry in entries:
Label(entries_window, text=f"Title: {entry[1]}").pack()
Label(entries_window, text=f"Date: {entry[3]}").pack()
Label(entries_window, text=f"Entry:\n{entry[2]}\n").pack(pady=10)
# Main GUI window
root = Tk()
root.title("Personal Diary")
root.geometry("400x400")
# Labels and text fields for entry title and body
Label(root, text="Diary Title:", font=("Helvetica", 12)).pack(pady=10)
title_entry = Entry(root, width=40)
title_entry.pack(pady=5)
Label(root, text="Diary Entry:", font=("Helvetica", 12)).pack(pady=10)
diary_text = Text(root, height=10, width=40)
diary_text.pack(pady=5)
# Buttons to save the entry and view all entries
Button(root, text="Save Entry", command=save_diary_entry).pack(pady=10)
Button(root, text="View All Entries", command=display_entries).pack(pady=5)
# Run the GUI loop
root.mainloop()
8. Explanation of Code
A. Saving a New Entry
- The
save_diary_entry()
function retrieves the title and body of the diary entry from the GUI and saves it to the database using theadd_entry()
function. It also provides feedback to the user via a message box.
B. Displaying All Entries
- The
display_entries()
function opens a new window that lists all diary entries stored in the database. It calls theview_entries()
function to fetch and display the data.
9. Enhancements and Additional Features
Here are some ideas to extend the functionality of the Personal Diary Application:
- Search Functionality: Allow users to search for specific entries by title.
- Password Protection: Add password protection to secure diary entries.
- Backup and Restore: Implement a feature to back up and restore diary entries from a file.
- Date-Based Search: Enable users to search for diary entries by specific dates.
- Tagging: Allow users to tag entries with specific labels or topics for easy categorization.
10. Conclusion
The Personal Diary Application allows users to easily store and manage their diary entries. It covers core Python concepts like GUI development with Tkinter, working with SQLite databases, and handling user input. The project can be further enhanced with more advanced features like search, tagging, and password protection.
Would you like to add any specific functionality or make adjustments to this project?