Desktop Diary App in Python
About the project: This is a project for a self-contained desktop diary application written in Python using the built-in tkinter library. It will store your entries in a simple JSON file, making it easy to use and manage.
All the code is in a single file, so you can easily run it without any complex setup.
How to Run the App
- Save the Code:Save the code below into a file named `desktop_diary.py`.
- Run from Terminal: Open your terminal or command prompt, navigate to the folder where you saved the file, and run the command:
python desktop_diary.py
The application will automatically create a diary.json file in the same directory to store your entries.
You can navigate between days, write your thoughts, and save them with the click of a button.
This is a great starting point for a personal project. You could extend this by adding features like a search function, a calendar view, or password protection.
Project Level: Intermediate
You can directly copy the below snippet code with the help of green copy button, paste it and run it in any Python editor you have.
Steps: Follow these stepsStep 1: Copy below code using green 'copy' button.
Step 2: Paste the code on your chosen editor.
Step 3: Save the code with filename and .py extention.
Step 4: Run (Press F5 if using python IDLE).
# desktop_diary.py
import tkinter as tk
from tkinter import messagebox
import json
from datetime import date, timedelta
import os
class DiaryApp:
"""
A simple desktop diary application using Tkinter.
It allows users to write and save diary entries for specific dates.
Entries are stored in a JSON file named 'diary.json'.
"""
def __init__(self, root):
self.root = root
self.root.title("My Simple Desktop Diary")
self.root.geometry("600x500")
self.root.configure(bg="#f0f0f0")
# Set the current date to today's date
self.current_date = date.today()
# Load existing diary entries from a file
self.entries = self.load_entries()
# --- GUI Components ---
# Frame to hold date navigation buttons
date_frame = tk.Frame(self.root, bg="#f0f0f0")
date_frame.pack(pady=10)
# Button to go to the previous day
self.prev_button = tk.Button(
date_frame,
text="<",
font=("Arial", 12),
command=self.prev_day,
bg="#333",
fg="white",
relief=tk.FLAT
)
self.prev_button.pack(side=tk.LEFT, padx=5)
# Label to display the current date
self.date_label = tk.Label(
date_frame,
text=self.current_date.strftime("%B %d, %Y"),
font=("Arial", 14, "bold"),
bg="#f0f0f0",
fg="#333"
)
self.date_label.pack(side=tk.LEFT, padx=10)
# Button to go to the next day
self.next_button = tk.Button(
date_frame,
text=">",
font=("Arial", 12),
command=self.next_day,
bg="#333",
fg="white",
relief=tk.FLAT
)
self.next_button.pack(side=tk.LEFT, padx=5)
# Text area for writing diary entries
self.entry_text = tk.Text(
self.root,
wrap=tk.WORD,
font=("Arial", 12),
bg="white",
fg="#333",
bd=0,
padx=10,
pady=10
)
self.entry_text.pack(expand=True, fill=tk.BOTH, padx=20, pady=10)
# Button to save the current entry
self.save_button = tk.Button(
self.root,
text="Save Entry",
font=("Arial", 12, "bold"),
command=self.save_current_entry,
bg="#4caf50",
fg="white",
relief=tk.FLAT,
pady=8
)
self.save_button.pack(pady=10, padx=20, fill=tk.X)
# Initially display the entry for today's date
self.display_entry()
def load_entries(self):
"""Loads diary entries from 'diary.json'."""
if os.path.exists("diary.json"):
with open("diary.json", "r") as f:
return json.load(f)
return {}
def save_entries(self):
"""Saves all diary entries to 'diary.json'."""
with open("diary.json", "w") as f:
json.dump(self.entries, f, indent=4)
def display_entry(self):
"""Displays the diary entry for the current date."""
# Clear the text area
self.entry_text.delete(1.0, tk.END)
# Get the date in YYYY-MM-DD format as the key
date_str = self.current_date.isoformat()
# Check if an entry exists for the current date
if date_str in self.entries:
self.entry_text.insert(tk.END, self.entries[date_str])
# Update the date label
self.date_label.config(text=self.current_date.strftime("%B %d, %Y"))
def save_current_entry(self):
"""Saves the text from the text area to the current date's entry."""
content = self.entry_text.get(1.0, tk.END).strip()
date_str = self.current_date.isoformat()
if content:
self.entries[date_str] = content
elif date_str in self.entries:
# If content is empty, delete the entry for the day
del self.entries[date_str]
self.save_entries()
messagebox.showinfo("Success", "Entry saved successfully!")
def prev_day(self):
"""Moves the diary to the previous day."""
self.current_date -= timedelta(days=1)
self.display_entry()
def next_day(self):
"""Moves the diary to the next day."""
self.current_date += timedelta(days=1)
self.display_entry()
if __name__ == "__main__":
# Create the main window and start the application
root = tk.Tk()
app = DiaryApp(root)
root.mainloop()
```
← Back to Projects