DigitalDiary Code Reviewed
DigitalDiary Code Reviewed
import sqlite3
import pandas as pd
import hashlib
import csv
import os
ctk.set_appearance_mode("dark")
ctk.set_default_color_theme("dark-blue")
app = ctk.CTk()
app.geometry("1000x700")
# Variables
sleep_hours_var = ctk.StringVar(value="7")
screen_time_var = ctk.StringVar(value="5")
exercise_var = ctk.StringVar(value="30")
return
background_image = Image.open(image_path)
background_photo = ImageTk.PhotoImage(background_image)
# Database connection
def connect_db():
cursor = conn.cursor()
cursor.execute('''
)''')
cursor.execute('''
date TEXT,
mood TEXT,
content TEXT,
photo_path TEXT
)''')
cursor.execute('''
date TEXT,
sleep_hours TEXT,
screen_time TEXT,
exercise TEXT
)''')
cursor.execute('''
theme TEXT,
font TEXT
)''')
conn.commit()
return conn
def close_db(conn):
if conn:
conn.close()
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()
user = cursor.fetchone()
if user:
load_settings()
open_dashboard()
else:
except Exception as e:
finally:
cursor.close()
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()
finally:
cursor.close()
def open_dashboard():
dashboard = ctk.CTk()
dashboard.geometry("1200x800")
btn_home.pack(pady=20)
btn_new_entry.pack(pady=20)
btn_wellbeing.pack(pady=20)
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.pack(pady=20)
lbl_calendar.pack(pady=20)
calendar.pack(pady=10)
btn_view_entry.pack(pady=10)
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.pack(pady=10)
txt_recent_entry.pack(pady=10)
txt_recent_entry.insert("1.0", get_recent_entry())
txt_recent_entry.configure(state="disabled")
lbl_new_entry.pack(pady=20)
search_text.pack(pady=10)
entry_text.pack(pady=10)
mood_label.pack(pady=10)
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.pack(pady=10)
lbl_wellbeing.pack(pady=20)
lbl_screen_time.pack(pady=10)
screen_time_entry.pack(pady=10)
lbl_sleep_hours.pack(pady=10)
sleep_hours_entry.pack(pady=10)
lbl_exercise.pack(pady=10)
exercise_entry.pack(pady=10)
btn_save_wellbeing = ctk.CTkButton(wellbeing_frame, text="💾 Save Wellbeing",
command=save_wellbeing)
btn_save_wellbeing.pack(pady=20)
lbl_settings.pack(pady=20)
lbl_theme.pack(pady=10)
theme_light_btn.pack(pady=10)
theme_dark_btn.pack(pady=10)
lbl_font.pack(pady=10)
font_arial_btn.pack(pady=10)
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()
def view_stats():
stats_window = ctk.CTk()
stats_window.geometry("800x600")
stats_window.title("Wellbeing Stats")
lbl_stats.pack(pady=20)
cursor = conn.cursor()
data = cursor.fetchone()
if not data:
return
axs[0].set_xlabel("Days")
axs[0].set_ylabel("Hours")
axs[0].legend()
axs[2].set_xlabel("Days")
axs[2].set_ylabel("Hours")
axs[2].legend()
canvas.draw()
canvas.get_tk_widget().pack(pady=20)
stats_window.mainloop()
def show_frame(frame):
frame.tkraise()
def show_entry_for_selected_date(calendar):
selected_date = calendar.get_date()
cursor = conn.cursor()
try:
entry = cursor.fetchone()
if entry:
messagebox.showinfo("Entry", entry[0])
else:
finally:
cursor.close()
# Function to attach a photo to the entry
def attach_photo(frame):
if photo_path:
photo = Image.open(photo_path)
photo.thumbnail((200, 200))
photo_display = ImageTk.PhotoImage(photo)
lbl_photo.pack(pady=10)
date = datetime.now().strftime('%Y-%m-%d')
cursor = conn.cursor()
try:
cursor.execute("INSERT INTO entries (date, mood, content) VALUES (?, ?, ?)", (date, mood,
content))
conn.commit()
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:
else:
finally:
cursor.close()
login_frame = ctk.CTkFrame(app)
login_frame.pack(pady=50)
lbl_title.pack(pady=20)
entry_username.pack(pady=10)
entry_password.pack(pady=10)
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.pack(pady=10)
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:
conn.commit()
finally:
cursor.close()
def plot_wellbeing_data():
pass
def change_theme(theme):
ctk.set_appearance_mode(theme)
save_settings(theme=theme)
def change_font(font):
app.option_add("*Font", font)
save_settings(font=font)
cursor = conn.cursor()
try:
if theme:
if font:
conn.commit()
finally:
cursor.close()
if window.winfo_exists():
else:
def load_settings():
cursor = conn.cursor()
try:
settings = cursor.fetchone()
if settings:
if theme:
ctk.set_appearance_mode(theme)
if font:
app.option_add("*Font", font)
finally:
cursor.close()
def logout(dashboard):
dashboard.destroy()
login_frame = ctk.CTkFrame(app, width=400, height=300) # Set a fixed size for the frame
lbl_title.pack(pady=20)
entry_password.pack(pady=10)
btn_login.pack(pady=10)
lbl_register.pack(pady=10)
btn_register.pack(pady=10)
app.mainloop()