Home » A Python program that implements a Pomodoro timer using tkinter

A Python program that implements a Pomodoro timer using tkinter

by pqzrmred71

Here’s a Python program that implements a Pomodoro timer using tkinter. The Pomodoro Technique is a time management method where you work for a set amount of time (usually 25 minutes), take a short break, and repeat the cycle.

After a few cycles, you take a longer break.

This program allows you to set a work interval, short break, and long break duration and includes Start, Reset, and Stop buttons.

Code

import tkinter as tk
import time

# Set default times for work, short break, and long break in minutes
WORK_MINUTES = 25
SHORT_BREAK_MINUTES = 5
LONG_BREAK_MINUTES = 15

# Initialize global variables
is_running = False
remaining_time = WORK_MINUTES * 60

# Function to format time in MM:SS format
def format_time(seconds):
    minutes = seconds // 60
    seconds = seconds % 60
    return f"{minutes:02}:{seconds:02}"

# Function to update the timer
def update_timer():
    global remaining_time, is_running
    if is_running and remaining_time > 0:
        minutes, seconds = divmod(remaining_time, 60)
        time_display.config(text=format_time(remaining_time))
        remaining_time -= 1
        root.after(1000, update_timer)
    elif remaining_time == 0 and is_running:
        time_display.config(text="Time's Up!")
        is_running = False

# Function to start the timer
def start_timer():
    global is_running, remaining_time
    if not is_running:
        is_running = True
        update_timer()

# Function to stop the timer
def stop_timer():
    global is_running
    is_running = False

# Function to reset the timer
def reset_timer():
    global remaining_time, is_running
    is_running = False
    remaining_time = WORK_MINUTES * 60
    time_display.config(text=format_time(remaining_time))

# Initialize the main window
root = tk.Tk()
root.title("Pomodoro Timer")
root.geometry("300x200")

# Display label for the timer
time_display = tk.Label(root, text=format_time(remaining_time), font=("Helvetica", 48))
time_display.pack(pady=20)

# Buttons to control the timer
start_button = tk.Button(root, text="Start", command=start_timer)
start_button.pack(side="left", padx=10)

stop_button = tk.Button(root, text="Stop", command=stop_timer)
stop_button.pack(side="left", padx=10)

reset_button = tk.Button(root, text="Reset", command=reset_timer)
reset_button.pack(side="left", padx=10)

# Run the main loop
root.mainloop()

Explanation

  1. Import Required Libraries:
    • tkinter: Used to create the GUI components for the application.
    • time: Used to handle time formatting and updates.
  2. Set Default Times:
    • WORK_MINUTES = 25: Default work session is set to 25 minutes.
    • SHORT_BREAK_MINUTES = 5: Default short break is set to 5 minutes.
    • LONG_BREAK_MINUTES = 15: Default long break is set to 15 minutes.
    • These can be adjusted according to user preference.
  3. Global Variables:
    • is_running: A flag that indicates whether the timer is currently running.
    • remaining_time: The remaining time in seconds. Initialized to the work duration (25 minutes converted to seconds).
  4. format_time Function:
    • Converts seconds into MM:SS format for easy readability on the display.
    • Uses f”{minutes:02}:{seconds:02}” to zero-pad single-digit minutes and seconds.
  5. update_timer Function:
    • This function is responsible for updating the time display every second.
    • Countdown Logic:
      • Checks if is_running is True and if remaining_time is greater than 0.
      • If so, it decrements remaining_time by 1 each second.
      • root.after(1000, update_timer): Recursively calls update_timer every 1000 milliseconds (1 second) to keep the countdown running.
    • When remaining_time reaches 0, it displays “Time’s Up!” and stops the timer.
  6. start_timer Function:
    • Starts the countdown if it is not already running (is_running = True).
    • Calls update_timer() to begin the countdown loop.
  7. stop_timer Function:
    • Stops the timer by setting is_running = False, halting the countdown.
  8. reset_timer Function:
    • Resets the timer by:
      • Setting is_running = False to stop the countdown.
      • Resetting remaining_time to WORK_MINUTES * 60 (the default work session duration).
      • Updating the display to show the reset time.
  9. Initialize the Main Application Window:
    • root = tk.Tk(): Creates the main application window.
    • root.title(“Pomodoro Timer”): Sets the window title.
    • root.geometry(“300×200”): Sets the window dimensions to 300×200 pixels.
  10. Display Label for the Timer:
    • time_display = tk.Label(…): Creates a label that displays the countdown timer in MM:SS format.
    • font=(“Helvetica”, 48): Sets a large font size for visibility.
  11. Start, Stop, and Reset Buttons:
    • start_button, stop_button, and reset_button are Button widgets that trigger the start_timer, stop_timer, and reset_timer functions, respectively, when clicked.
  12. Run the Main Event Loop:
    • root.mainloop() starts the main loop, keeping the window open and responsive.

How the Timer Works

  1. Starting the Timer:
    • Clicking “Start” sets is_running = True and begins decrementing remaining_time every second.
  2. Stopping the Timer:
    • Clicking “Stop” sets is_running = False, which halts the countdown.
  3. Resetting the Timer:
    • Clicking “Reset” stops the timer, resets remaining_time to the initial work duration, and updates the display to show the reset time.

Sample Usage

  1. Open the Application:
    • The timer will display 25:00 by default (or whatever you set as WORK_MINUTES).
  2. Start the Timer:
    • Clicking “Start” begins the countdown.
  3. Pause the Timer:
    • Clicking “Stop” pauses the countdown.
  4. Reset the Timer:
    • Clicking “Reset” stops and resets the timer to the initial 25 minutes (or the set work time).

Notes

  • Adjustable Intervals: You can easily adjust WORK_MINUTES, SHORT_BREAK_MINUTES, and LONG_BREAK_MINUTES to fit different Pomodoro intervals.
  • Recursive Updates: The update_timer() function uses root.after(1000, update_timer) to repeatedly call itself every second, creating a continuous countdown.

Summary

This Python program is a simple GUI-based Pomodoro timer built with tkinter. It provides basic Start, Stop, and Reset functionality to help users manage their work sessions and take breaks effectively.

This implementation is customizable and can be expanded with additional features, like automatic switching between work and break intervals.

You may also like