A study timer application developed using Python and Pygame .The program features a customizable countdown timer, start and reset controls, multiple modes ,daily study time tracing, and real time digital clock ,all presented in a clean and intuitive user interface Study with Mini Tree.py import pygame import sys import time from datetime import datetime
pygame.init()
WIDTH, HEIGHT = 600, 400 screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("study with me")
BG_COLOR = (200, 240, 200) # pastel green BUTTON_COLOR = (255, 255, 255) TEXT_COLOR = (0, 0, 0) BUTTON_BORDER = (180, 220, 180)
font = pygame.font.Font(None, 48) small_font = pygame.font.Font(None, 30)
MODES = { "Study": 25 * 60, "Break": 5 * 60, "Relax": 15 * 60 } mode_list = list(MODES.keys()) mode_index = 0 mode = mode_list[mode_index]
timer_length = MODES[mode] start_time = None timer_running = False
start_timestamp = None
input_box = pygame.Rect(WIDTH // 2 - 60, HEIGHT // 2 - 80, 120, 50) start_button = pygame.Rect(WIDTH // 2 - 60, HEIGHT // 2 + 50, 120, 50) reset_button = pygame.Rect(WIDTH - 140, HEIGHT - 60, 100, 40) mode_button = pygame.Rect(30, HEIGHT // 2 - 25, 120, 50)
input_active = False input_text = str(timer_length // 60)
clock = pygame.time.Clock() running = True
study_log = {}
def draw_button(rect, text): pygame.draw.rect(screen, BUTTON_COLOR, rect, border_radius=15) pygame.draw.rect(screen, BUTTON_BORDER, rect, 3, border_radius=15) text_surf = small_font.render(text, True, TEXT_COLOR) screen.blit(text_surf, (rect.x + (rect.width - text_surf.get_width()) // 2, rect.y + (rect.height - text_surf.get_height()) // 2))
def get_today_date(): return datetime.now().strftime("%Y-%m-%d")
def add_study_time(seconds): today = get_today_date() if today not in study_log: study_log[today] = 0 study_log[today] += seconds
def get_study_time_str(): today = get_today_date() total_seconds = study_log.get(today, 0) m, s = divmod(total_seconds, 60) h, m = divmod(m, 60) return f"{h}h {m}m {s}s"
while running: clock.tick(60) mouse_pos = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
if start_button.collidepoint(mouse_pos):
try:
mins = int(input_text)
if mins > 0:
timer_length = mins * 60
start_time = time.time()
timer_running = True
start_timestamp = time.time()
except:
pass
elif reset_button.collidepoint(mouse_pos):
if timer_running and start_time:
elapsed = int(time.time() - start_time)
add_study_time(elapsed)
timer_running = False
start_time = None
elif mode_button.collidepoint(mouse_pos):
if timer_running and start_time:
elapsed = int(time.time() - start_time)
add_study_time(elapsed)
mode_index = (mode_index + 1) % len(mode_list)
mode = mode_list[mode_index]
timer_length = MODES[mode]
input_text = str(timer_length // 60)
timer_running = False
start_time = None
elif input_box.collidepoint(mouse_pos):
input_active = True
else:
input_active = False
if event.type == pygame.KEYDOWN and input_active:
if event.key == pygame.K_BACKSPACE:
input_text = input_text[:-1]
elif event.unicode.isdigit():
input_text += event.unicode
elif event.key == pygame.K_RETURN:
input_active = False
screen.fill(BG_COLOR)
if timer_running and start_time:
elapsed = int(time.time() - start_time)
remaining = max(0, timer_length - elapsed)
minutes = remaining // 60
seconds = remaining % 60
timer_text = f"{minutes:02}:{seconds:02}"
if remaining == 0:
timer_running = False
add_study_time(timer_length)
start_time = None
else:
timer_text = f"{timer_length // 60:02}:00"
timer_surf = font.render(timer_text, True, TEXT_COLOR)
screen.blit(timer_surf, (WIDTH // 2 - timer_surf.get_width() // 2, 50))
draw_button(start_button, "Start" if not timer_running else "Running")
draw_button(reset_button, "Reset")
draw_button(mode_button, f"Mode: {mode}")
color = (255, 255, 255) if input_active else (230, 230, 230)
pygame.draw.rect(screen, color, input_box, border_radius=15)
pygame.draw.rect(screen, BUTTON_BORDER, input_box, 2, border_radius=15)
input_surf = font.render(input_text, True, TEXT_COLOR)
screen.blit(input_surf, (input_box.x + 10, input_box.y + 10))
input_label = small_font.render("Set Minutes:", True, TEXT_COLOR)
screen.blit(input_label, (input_box.x, input_box.y - 30))
# Show daily study time
study_time_text = small_font.render("Today Study: " + get_study_time_str(), True, TEXT_COLOR)
screen.blit(study_time_text, (30, 20))
# Digital clock
local_time = time.strftime("%H:%M:%S")
clock_surf = small_font.render(local_time, True, TEXT_COLOR)
screen.blit(clock_surf, (WIDTH // 2 - clock_surf.get_width() // 2, HEIGHT - 40))
pygame.display.flip()
pygame.quit() sys.exit() ##How to Run 1.install Python 3 2.Install Pygame;'pip install pygame' 3.Run the code