0% found this document useful (0 votes)
44 views2 pages

Python Pomodoro Timer Code

This Python code defines a Pomodoro timer application with the following functionality: - It tracks work and break sessions in a repeating cycle to implement the Pomodoro technique. Work sessions last 25 minutes and breaks last 5 minutes, with a longer 30 minute break every 4 sessions. - The timer displays as a canvas widget showing minutes and seconds counting down for the current session. - Buttons allow the user to start and reset the timer. Completed sessions are tracked with a checkmark label. - The code handles counting down the different session lengths, updating the display, and switching between work and break states in a repeating loop.

Uploaded by

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

Python Pomodoro Timer Code

This Python code defines a Pomodoro timer application with the following functionality: - It tracks work and break sessions in a repeating cycle to implement the Pomodoro technique. Work sessions last 25 minutes and breaks last 5 minutes, with a longer 30 minute break every 4 sessions. - The timer displays as a canvas widget showing minutes and seconds counting down for the current session. - Buttons allow the user to start and reset the timer. Completed sessions are tracked with a checkmark label. - The code handles counting down the different session lengths, updating the display, and switching between work and break states in a repeating loop.

Uploaded by

adam williams
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

from tkinter import *

import math
# ---------------------------- CONSTANTS ------------------------------- #
PINK = "#e2979c"
RED = "#e7305b"
GREEN = "#9bdeac"
YELLOW = "#f7f5dd"
FONT_NAME = "Courier"
WORK_MIN = 1
SHORT_BREAK_MIN = 5
LONG_BREAK_MIN = 20
reps = 0
timer = None

# ---------------------------- TIMER RESET ------------------------------- #

def reset_timer():
window.after_cancel(timer)
[Link](timer_text, text="00:00")
title_label.config(text="Timer")
check_marks.config(text="")
global reps
reps = 0

# ---------------------------- TIMER MECHANISM ------------------------------- #

def start_timer():
global reps
reps += 1

work_sec = WORK_MIN * 60
short_break_sec = SHORT_BREAK_MIN * 60
long_break_sec = LONG_BREAK_MIN * 60

if reps % 8 == 0:
count_down(long_break_sec)
title_label.config(text="Break", fg=RED)
elif reps % 2 == 0:
count_down(short_break_sec)
title_label.config(text="Break", fg=PINK)
else:
count_down(work_sec)
title_label.config(text="Work", fg=GREEN)

# ---------------------------- COUNTDOWN MECHANISM -------------------------------


#
def count_down(count):
count_min = [Link](count / 60)
if count_min < 10:
count_min = f"0{count_min}"
count_sec = count % 60
if count_sec < 10:
count_sec = f"0{count_sec}"

[Link](timer_text, text=f"{count_min}:{count_sec}")
if count > 0:
global timer
timer = [Link](1000, count_down, count - 1)
else:
start_timer()
marks = ""
work_sessions = [Link](reps/2)
for _ in range(work_sessions):
marks += "✔"
check_marks.config(text=marks)

# ---------------------------- UI SETUP ------------------------------- #


window = Tk()
[Link]("Pomodoro")
[Link](padx=100, pady=50, bg=YELLOW)

title_label = Label(text="Timer", fg=GREEN, bg=YELLOW, font=(FONT_NAME, 50))


title_label.grid(column=1, row=0)

canvas = Canvas(width=200, height=224, bg=YELLOW, highlightthickness=0)


tomato_img = PhotoImage(file="[Link]")
canvas.create_image(100, 112, image=tomato_img)
timer_text = canvas.create_text(100, 130, text="00:00", fill="white",
font=(FONT_NAME, 35, "bold"))
[Link](column=1, row=1)

start_button = Button(text="Start", highlightthickness=0, command=start_timer)


start_button.grid(column=0, row=2)

reset_button = Button(text="Reset", highlightthickness=0, command=reset_timer)


reset_button.grid(column=2, row=2)

check_marks = Label(fg=GREEN, bg=YELLOW)


check_marks.grid(column=1, row=3)

[Link]()

Common questions

Powered by AI

The use of constants for colors and time durations enhances the script's maintainability and flexibility by centralizing these values at the start of the script. Instead of scattering values throughout the code, this method allows easy modifications and adjustments, such as changing color schemes or time settings, without digging into different parts of the script. This approach minimizes errors and simplifies updates, making the codebase more robust and adaptable to changes .

The script employs several strategies to manage transitions between working sessions and breaks. Firstly, the 'reps' variable tracks the number of sessions, dictating the transition from work to short or long breaks. The start_timer function determines the type of session based on this count and initiates the appropriate countdown. Additionally, UI changes in color and text convey phase transitions to the user, enhancing clarity during these switches. This systematic and visual approach ensures seamless transitions and clear communication of the Pomodoro phases .

The 'PhotoImage' object is used in the application's UI setup to load and display an image (in this case, 'tomato.png') on the canvas. This visual element adds an aesthetic appeal to the application, breaking the monotony of text-based interfaces and providing a themed context that can make the application more engaging and enjoyable for the user. It enhances user experience by ensuring the interface is not only functional but also visually attractive .

The countdown mechanism in the script utilizes recursion by calling the count_down function within itself using window.after(). This sets up a timer that waits for 1000 milliseconds (or 1 second) before calling count_down again with a decremented count value. This recursive call continues until the count reaches zero, at which point the start_timer function is called to initiate the next phase (break or work), ensuring continuous operation of the Pomodoro cycle without looping constructs .

The script adjusts its UI to reflect different phases of the Pomodoro cycle by changing the text and color of the title label to indicate either 'Work' (green) or 'Break' (red or pink), depending on the phase. Additionally, the timer text is updated to show the remaining time for the current phase. This dynamic UI adaptation is important for user interaction as it provides clear visual cues about the current phase, enhancing ease of use and supporting the user’s ability to manage their time effectively based on the Pomodoro technique .

The script enhances user motivation by using a visual indicator—check marks—to mark completed work sessions. After each work session, the script calculates the number of completed sessions, updating the UI with a corresponding number of check marks. This visual feedback offers a sense of accomplishment and progress, encouraging continued use of the Pomodoro timer and adherence to the work-rest cycle .

The purpose of using 'window.after_cancel(timer)' in the timer reset process is to stop any scheduled calls to the count_down function that may still be active. This ensures that when the reset_timer function is invoked, the current countdown is halted, preventing overlapping calls and ensuring that the timer display and related variables are correctly reset to their default state, ready for a new timer cycle .

The UI setup in the script contributes to functionality and aesthetics by setting up a window with a colored background (YELLOW), labels, a canvas with an image, and buttons with associated commands. The use of fonts and color schemes (via constants like GREEN, RED, PINK) makes the UI visually appealing, while elements like the timer text and check marks convey real-time information. This cohesive design ensures clarity and ease of use, which are vital for engaging the user and supporting the Pomodoro technique’s effectiveness .

The key components required to implement the Pomodoro timer using the provided Python script include the setup of constants for time intervals (WORK_MIN, SHORT_BREAK_MIN, LONG_BREAK_MIN), the functions for timer reset (reset_timer) and start mechanism (start_timer), a countdown mechanism (count_down) that recursively calls itself, and a Tkinter UI setup to create the window, label, canvas, buttons, and checkmarks. The constants are crucial in defining work and break durations, while the Tkinter setup ensures the user interface is appropriately displayed .

The 'reps' variable plays a crucial role within the timer mechanism by tracking the number of completed repetitions of the work sessions and breaks. It increments each time a new timer starts, allowing the script to determine whether to start a work session, a short break, or a long break. For every eight repetitions, a long break is triggered; for even repetitions, a short break is set, while odd repetitions initiate a work session. This logical division using the 'reps' variable facilitates the structured Pomodoro cycle, alternating work and rest periods systematically .

You might also like