0% found this document useful (0 votes)
20 views

DigitalDiary Code Reviewed

Uploaded by

Ai Learn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

DigitalDiary Code Reviewed

Uploaded by

Ai Learn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

import customtkinter as ctk

import sqlite3

from tkinter import messagebox, filedialog

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

import matplotlib.pyplot as plt

import pandas as pd

from tkcalendar import Calendar

from PIL import Image, ImageTk

from datetime import datetime

import hashlib

import csv

import os

# Set the appearance mode and color theme

ctk.set_appearance_mode("dark")

ctk.set_default_color_theme("dark-blue")

# Create the main application window

app = ctk.CTk()

app.geometry("1000x700")

app.title("Login to Digital Diary")

# Variables

mood_var = ctk.StringVar(value="Select Mood")

sleep_hours_var = ctk.StringVar(value="7")

screen_time_var = ctk.StringVar(value="5")

exercise_var = ctk.StringVar(value="30")

daily_quote = "Stay positive, work hard, make it happen!"

def set_background(app, image_path):


if not os.path.exists(image_path):

messagebox.showerror("Error", f"Image not found: {image_path}")

return

background_image = Image.open(image_path)

background_image = background_image.resize((1000, 700), Image.Resampling.LANCZOS) #


Replace ANTIALIAS with Resampling.LANCZOS

background_photo = ImageTk.PhotoImage(background_image)

background_label = ctk.CTkLabel(app, image=background_photo, text="")

background_label.image = background_photo # Keep a reference to avoid garbage collection

background_label.place(x=0, y=0, relwidth=1, relheight=1)

# Database connection

def connect_db():

conn = sqlite3.connect('new_diary.db', timeout=10)

conn.execute('PRAGMA journal_mode=WAL;') # Enable WAL mode for concurrency

cursor = conn.cursor()

cursor.execute('''

CREATE TABLE IF NOT EXISTS users (

id INTEGER PRIMARY KEY AUTOINCREMENT,

username TEXT NOT NULL,

password TEXT NOT NULL

)''')

cursor.execute('''

CREATE TABLE IF NOT EXISTS entries (

id INTEGER PRIMARY KEY AUTOINCREMENT,

date TEXT,

mood TEXT,

content TEXT,
photo_path TEXT

)''')

cursor.execute('''

CREATE TABLE IF NOT EXISTS wellbeing (

id INTEGER PRIMARY KEY AUTOINCREMENT,

date TEXT,

sleep_hours TEXT,

screen_time TEXT,

exercise TEXT

)''')

cursor.execute('''

CREATE TABLE IF NOT EXISTS settings (

id INTEGER PRIMARY KEY AUTOINCREMENT,

theme TEXT,

font TEXT

)''')

conn.commit()

return conn

conn = connect_db() # Global connection to be reused

# Function to close the database connection

def close_db(conn):

if conn:

conn.close()

# Function to encrypt password

def encrypt_password(password):

return hashlib.sha256(password.encode()).hexdigest()
# Function to handle login

def login():

try:

username = entry_username.get()

password = encrypt_password(entry_password.get())

cursor = conn.cursor()

cursor.execute("SELECT * FROM users WHERE username=? AND password=?", (username,


password))

user = cursor.fetchone()

if user:

load_settings()

messagebox.showinfo("Login Successful", "Welcome!")

open_dashboard()

else:

messagebox.showerror("Login Failed", "Invalid username or password")

except Exception as e:

messagebox.showerror("Error", f"An error occurred: {e}")

finally:

cursor.close()

# Function to register a new user

def register():

username = entry_username.get()

password = encrypt_password(entry_password.get())

cursor = conn.cursor()

try:

cursor.execute("INSERT INTO users (username, password) VALUES (?, ?)", (username, password))
conn.commit()

messagebox.showinfo("Registration Successful", "User Registered Successfully!")

finally:

cursor.close()

# Function to open the dashboard

def open_dashboard():

dashboard = ctk.CTk()

dashboard.geometry("1200x800")

dashboard.title("Enhanced Digital Diary Dashboard")

# Create a menu frame

menu_frame = ctk.CTkFrame(dashboard, width=220, height=800)

menu_frame.pack(side="left", fill="y", padx=10, pady=10)

# Add menu buttons

btn_home = ctk.CTkButton(menu_frame, text="🏠 Home", corner_radius=8, command=lambda:


show_frame(home_frame))

btn_home.pack(pady=20)

btn_new_entry = ctk.CTkButton(menu_frame, text="📝 New Entry", corner_radius=8,


command=lambda: show_frame(new_entry_frame))

btn_new_entry.pack(pady=20)

btn_wellbeing = ctk.CTkButton(menu_frame, text="💪 Wellbeing", corner_radius=8,


command=lambda: show_frame(wellbeing_frame))

btn_wellbeing.pack(pady=20)

btn_stats = ctk.CTkButton(menu_frame, text="📊 View Stats", corner_radius=8,


command=view_stats)

btn_stats.pack(pady=20)
btn_settings = ctk.CTkButton(menu_frame, text="⚙️Settings", corner_radius=8,
command=lambda: show_frame(settings_frame))

btn_settings.pack(pady=20)

btn_logout = ctk.CTkButton(menu_frame, text="🚪 Logout", corner_radius=8, command=lambda:


logout(dashboard))

btn_logout.pack(pady=20)

# Add calendar to the menu for quick access

lbl_calendar = ctk.CTkLabel(menu_frame, text="📅 Calendar", font=("Arial", 18))

lbl_calendar.pack(pady=20)

calendar = Calendar(menu_frame, selectmode='day', year=2024, month=9, day=1)

calendar.pack(pady=10)

btn_view_entry = ctk.CTkButton(menu_frame, text="Show Entry", command=lambda:


show_entry_for_selected_date(calendar))

btn_view_entry.pack(pady=10)

# Frames for each section

home_frame = ctk.CTkFrame(dashboard, width=980, height=800)

new_entry_frame = ctk.CTkFrame(dashboard, width=980, height=800)

wellbeing_frame = ctk.CTkFrame(dashboard, width=980, height=800)

settings_frame = ctk.CTkFrame(dashboard, width=980, height=800)

for frame in (home_frame, new_entry_frame, wellbeing_frame, settings_frame):

frame.place(x=220, y=0, relwidth=0.8, relheight=1)

# Home frame content

lbl_home = ctk.CTkLabel(home_frame, text="Welcome to your Enhanced Digital Diary!",


font=("Arial", 24))

lbl_home.pack(pady=20)
lbl_quote = ctk.CTkLabel(home_frame, text=daily_quote, font=("Arial", 16))

lbl_quote.pack(pady=20)

lbl_recent_entry = ctk.CTkLabel(home_frame, text="Recent Entry:", font=("Arial", 18))

lbl_recent_entry.pack(pady=10)

txt_recent_entry = ctk.CTkTextbox(home_frame, width=700, height=300)

txt_recent_entry.pack(pady=10)

txt_recent_entry.insert("1.0", get_recent_entry())

txt_recent_entry.configure(state="disabled")

# New Entry frame content

lbl_new_entry = ctk.CTkLabel(new_entry_frame, text="New Diary Entry", font=("Arial", 24))

lbl_new_entry.pack(pady=20)

search_text = ctk.CTkEntry(new_entry_frame, width=500, corner_radius=10,


placeholder_text="Search for keywords...")

search_text.pack(pady=10)

entry_text = ctk.CTkTextbox(new_entry_frame, width=700, height=300, corner_radius=10)

entry_text.pack(pady=10)

# Replace mood emoji buttons with a mood dropdown (Combobox)

mood_label = ctk.CTkLabel(new_entry_frame, text="Select Mood", font=("Arial", 16))

mood_label.pack(pady=10)

mood_dropdown = ctk.CTkComboBox(new_entry_frame, values=["😊 Happy", "😢 Sad", "😠


Angry", "😌 Relaxed", "😴 Tired"], variable=mood_var)

mood_dropdown.pack(pady=10)
btn_attach_photo = ctk.CTkButton(new_entry_frame, text="🖼 Attach Photo", command=lambda:
attach_photo(new_entry_frame))

btn_attach_photo.pack(pady=10)

btn_save_entry = ctk.CTkButton(new_entry_frame, text="💾 Save Entry", command=lambda:


save_entry(entry_text, mood_var.get()))

btn_save_entry.pack(pady=10)

# Wellbeing frame content

lbl_wellbeing = ctk.CTkLabel(wellbeing_frame, text="Wellbeing Tracker", font=("Arial", 24))

lbl_wellbeing.pack(pady=20)

lbl_screen_time = ctk.CTkLabel(wellbeing_frame, text="Track Your Screen Time (in hours)",


font=("Arial", 18))

lbl_screen_time.pack(pady=10)

screen_time_entry = ctk.CTkEntry(wellbeing_frame, textvariable=screen_time_var)

screen_time_entry.pack(pady=10)

lbl_sleep_hours = ctk.CTkLabel(wellbeing_frame, text="Track Your Sleep Hours (in hours)",


font=("Arial", 18))

lbl_sleep_hours.pack(pady=10)

sleep_hours_entry = ctk.CTkEntry(wellbeing_frame, textvariable=sleep_hours_var)

sleep_hours_entry.pack(pady=10)

lbl_exercise = ctk.CTkLabel(wellbeing_frame, text="Track Your Exercise Time (in minutes)",


font=("Arial", 18))

lbl_exercise.pack(pady=10)

exercise_entry = ctk.CTkEntry(wellbeing_frame, textvariable=exercise_var)

exercise_entry.pack(pady=10)
btn_save_wellbeing = ctk.CTkButton(wellbeing_frame, text="💾 Save Wellbeing",
command=save_wellbeing)

btn_save_wellbeing.pack(pady=20)

# Settings frame content

lbl_settings = ctk.CTkLabel(settings_frame, text="Customize Your Dashboard", font=("Arial", 24))

lbl_settings.pack(pady=20)

lbl_theme = ctk.CTkLabel(settings_frame, text="Select Theme", font=("Arial", 18))

lbl_theme.pack(pady=10)

# Add theme buttons with a change of button format

theme_light_btn = ctk.CTkButton(settings_frame, text="🌞 Light Theme", command=lambda:


change_theme("light"))

theme_light_btn.pack(pady=10)

theme_dark_btn = ctk.CTkButton(settings_frame, text="🌚 Dark Theme", command=lambda:


change_theme("dark"))

theme_dark_btn.pack(pady=10)

lbl_font = ctk.CTkLabel(settings_frame, text="Select Font", font=("Arial", 18))

lbl_font.pack(pady=10)

# Add font selection buttons with a change of button format

font_arial_btn = ctk.CTkButton(settings_frame, text="Arial", command=lambda:


change_font("Arial", dashboard))

font_arial_btn.pack(pady=10)

font_roboto_btn = ctk.CTkButton(settings_frame, text="Roboto", command=lambda:


change_font("Roboto", dashboard))

font_roboto_btn.pack(pady=10)
font_courier_btn = ctk.CTkButton(settings_frame, text="Courier", command=lambda:
change_font("Courier", dashboard))

font_courier_btn.pack(pady=10)

show_frame(home_frame)

dashboard.mainloop()

# Function to display wellbeing stats

def view_stats():

stats_window = ctk.CTk()

stats_window.geometry("800x600")

stats_window.title("Wellbeing Stats")

lbl_stats = ctk.CTkLabel(stats_window, text="Wellbeing Stats", font=("Arial", 18))

lbl_stats.pack(pady=20)

# Fetch the latest diary entry for the pie chart

cursor = conn.cursor()

cursor.execute("SELECT screen_time, exercise, sleep_hours FROM wellbeing ORDER BY date DESC


LIMIT 1")

data = cursor.fetchone()

if not data:

messagebox.showerror("Error", "No data to display!")

return

screen_time, exercise_time, sleep_hours = data

# Pie chart data for time invested in each activity

labels = ['Screen Time', 'Exercise Time', 'Sleep Hours']


times = [float(screen_time), float(exercise_time), float(sleep_hours)]

fig, axs = plt.subplots(1, 3, figsize=(15, 5))

# Bar chart and Line graph for last 7 days

cursor.execute("SELECT screen_time, exercise, sleep_hours FROM wellbeing ORDER BY date DESC


LIMIT 7")

all_data = cursor.fetchall()[::-1] # Reverse to get chronological order

screen_times = [float(row[0]) for row in all_data]

exercise_times = [float(row[1]) for row in all_data]

sleep_hours_list = [float(row[2]) for row in all_data]

days = range(1, len(all_data) + 1)

# Stacked bar chart

axs[0].bar(days, screen_times, color='blue', label="Screen Time")

axs[0].bar(days, exercise_times, color='green', label="Exercise Time", bottom=screen_times)

axs[0].bar(days, sleep_hours_list, color='orange', label="Sleep Hours",

bottom=[i + j for i, j in zip(screen_times, exercise_times)])

axs[0].set_title("Last 7 Days Time Spent on Activities")

axs[0].set_xlabel("Days")

axs[0].set_ylabel("Hours")

axs[0].legend()

# Pie chart for today's time investment

axs[1].pie(times, labels=labels, autopct='%1.1f%%', startangle=90)

axs[1].set_title("Today's Time Investment")

# Line graph for time trends over last 7 days

axs[2].plot(days, screen_times, marker='o', color='blue', label="Screen Time")

axs[2].plot(days, exercise_times, marker='o', color='green', label="Exercise Time")


axs[2].plot(days, sleep_hours_list, marker='o', color='orange', label="Sleep Hours")

axs[2].set_title("Trend of Time Spent Over Last 7 Days")

axs[2].set_xlabel("Days")

axs[2].set_ylabel("Hours")

axs[2].legend()

# Show the graphs

canvas = FigureCanvasTkAgg(fig, master=stats_window)

canvas.draw()

canvas.get_tk_widget().pack(pady=20)

stats_window.mainloop()

# Function to show the selected frame

def show_frame(frame):

frame.tkraise()

# Function to show entry for the selected date

def show_entry_for_selected_date(calendar):

selected_date = calendar.get_date()

cursor = conn.cursor()

try:

cursor.execute("SELECT content FROM entries WHERE date=?", (selected_date,))

entry = cursor.fetchone()

if entry:

messagebox.showinfo("Entry", entry[0])

else:

messagebox.showinfo("Entry", "No entry found for the selected date.")

finally:

cursor.close()
# Function to attach a photo to the entry

def attach_photo(frame):

photo_path = filedialog.askopenfilename(filetypes=[("Image files", "*.jpg;*.jpeg;*.png")])

if photo_path:

photo = Image.open(photo_path)

photo.thumbnail((200, 200))

photo_display = ImageTk.PhotoImage(photo)

lbl_photo = ctk.CTkLabel(frame, image=photo_display)

lbl_photo.photo = photo_display # Keep a reference

lbl_photo.pack(pady=10)

# Function to save the diary entry

def save_entry(entry_text, mood):

date = datetime.now().strftime('%Y-%m-%d')

content = entry_text.get("1.0", "end").strip()

cursor = conn.cursor()

try:

cursor.execute("INSERT INTO entries (date, mood, content) VALUES (?, ?, ?)", (date, mood,
content))

conn.commit()

messagebox.showinfo("Success", "Entry saved successfully!")

finally:

cursor.close()

# Function to get the most recent entry with date, time, and mood

def get_recent_entry():

cursor = conn.cursor()

try:
cursor.execute("SELECT date, mood, content FROM entries ORDER BY id DESC LIMIT 1")

entry = cursor.fetchone()

if entry:

date, mood, content = entry

return f"Date: {date}\nMood: {mood}\n\n{content}"

else:

return "No entries yet."

finally:

cursor.close()

# Create login frame and widgets

login_frame = ctk.CTkFrame(app)

login_frame.pack(pady=50)

# Set login background image

set_background(app, "C:/Users/Anjali/Downloads/download (6).jpeg") # Replace with your image


path

lbl_title = ctk.CTkLabel(login_frame, text="Digital Diary", font=("Arial", 24))

lbl_title.pack(pady=20)

entry_username = ctk.CTkEntry(login_frame, width=300, corner_radius=10,


placeholder_text="Username")

entry_username.pack(pady=10)

entry_password = ctk.CTkEntry(login_frame, width=300, corner_radius=10,


placeholder_text="Password", show="*")

entry_password.pack(pady=10)

btn_login = ctk.CTkButton(login_frame, text="Login", corner_radius=10, command=login)

btn_login.pack(pady=10)
lbl_register = ctk.CTkLabel(login_frame, text="New user? Register below.", font=("Arial", 14))

lbl_register.pack(pady=10)

btn_register = ctk.CTkButton(login_frame, text="Register", corner_radius=10, command=register)

btn_register.pack(pady=10)

# Function to save wellbeing data

def save_wellbeing():

date = datetime.now().strftime('%Y-%m-%d')

sleep_hours = sleep_hours_var.get()

screen_time = screen_time_var.get()

exercise = exercise_var.get()

cursor = conn.cursor()

try:

cursor.execute("INSERT INTO wellbeing (date, sleep_hours, screen_time, exercise) VALUES


(?, ?, ?, ?)", (date, sleep_hours, screen_time, exercise))

conn.commit()

messagebox.showinfo("Success", "Wellbeing data saved successfully!")

plot_wellbeing_data() # Call to plot data after saving

finally:

cursor.close()

def plot_wellbeing_data():

# Code for plotting the wellbeing data

pass

# Function to apply and save theme changes

def change_theme(theme):

ctk.set_appearance_mode(theme)
save_settings(theme=theme)

# Function to apply and save font changes

def change_font(font):

app.option_add("*Font", font)

save_settings(font=font)

# Function to save the theme and font settings in the database

def save_settings(theme=None, font=None):

cursor = conn.cursor()

try:

if theme:

cursor.execute("UPDATE settings SET theme=?", (theme,))

if font:

cursor.execute("UPDATE settings SET font=?", (font,))

conn.commit()

finally:

cursor.close()

# Function to change font for the dashboard window

def change_font(font, window):

print("Attempting to change font...")

if window.winfo_exists():

print(f"Window exists, changing font to {font}")

window.option_add("*Font", font) # Apply font change to the window

save_settings(font=font) # Save settings to the database

else:

print("Window doesn't exist. Cannot change font.")

messagebox.showerror("Error", "Cannot change font, the window is closed.")


# Function to load saved settings from the database

def load_settings():

cursor = conn.cursor()

try:

cursor.execute("SELECT theme, font FROM settings")

settings = cursor.fetchone()

if settings:

theme, font = settings

if theme:

ctk.set_appearance_mode(theme)

if font:

app.option_add("*Font", font)

finally:

cursor.close()

# Function to log out and close the dashboard

def logout(dashboard):

dashboard.destroy()

# Create login frame and widgets

login_frame = ctk.CTkFrame(app, width=400, height=300) # Set a fixed size for the frame

login_frame.pack_propagate(False) # Prevent the frame from resizing to fit content

login_frame.place(relx=0.5, rely=0.4, anchor="center") # Center the frame, adjusting `rely` for


vertical alignment

lbl_title = ctk.CTkLabel(login_frame, text="Digital Diary", font=("Arial", 24))

lbl_title.pack(pady=20)

entry_username = ctk.CTkEntry(login_frame, width=300, corner_radius=10,


placeholder_text="Username")
entry_username.pack(pady=10)

entry_password = ctk.CTkEntry(login_frame, width=300, corner_radius=10,


placeholder_text="Password", show="*")

entry_password.pack(pady=10)

btn_login = ctk.CTkButton(login_frame, text="Login", corner_radius=10, command=login)

btn_login.pack(pady=10)

lbl_register = ctk.CTkLabel(login_frame, text="New user? Register below.", font=("Arial", 14))

lbl_register.pack(pady=10)

btn_register = ctk.CTkButton(login_frame, text="Register", corner_radius=10, command=register)

btn_register.pack(pady=10)

# Start the application

app.mainloop()

You might also like