Skip to content

Latest commit

 

History

History
 
 

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

README.md

CountDown Timer

Description

A feature-rich GUI-based countdown timer application with start, pause, resume, reset, and close functionality. The timer displays time in HH:MM:SS format and changes colors as time runs out, providing visual feedback to the user.

Features

  • Clean and modern dark-themed GUI
  • Input time in seconds
  • Large, easy-to-read countdown display (HH:MM:SS format)
  • Start button to begin countdown
  • Pause/Resume button to pause and resume timer
  • Reset button to clear and restart
  • Close button with confirmation dialog
  • Color-changing display (green → orange → red as time decreases)
  • Visual and audio alert when timer finishes
  • Non-blocking timer using Tkinter's after() method
  • Input validation and error handling

Stack

  • Language: Python
  • Libraries:
    • tkinter (standard library - GUI)
    • tk==0.1.0 (wrapper package)
  • Complexity: Beginner-Intermediate

Installation

# Install dependencies
pip install -r requirements.txt

# Run the program
python main.py

Usage

  1. Start the application:

    python main.py
  2. Enter time:

    • Click on the input field
    • Enter time in seconds (e.g., 60 for 1 minute, 300 for 5 minutes)
    • Press Enter or click "Start"
  3. Control the timer:

    • Start: Begin countdown
    • Pause: Pause the countdown
    • Resume: Continue from where you paused
    • Reset: Stop and clear the timer
    • Close: Exit the application
  4. Timer completion:

    • Display shows 00:00:00 in red
    • Popup message appears: "⏰ Countdown finished!"

How It Works

Class Structure

The application uses OOP design with CountDownTimer class inheriting from Frame:

class CountDownTimer(Frame):
    def __init__(self, master=None)
    def create_widgets()
    def enter(event)
    def start_time()
    def pause_time()
    def reset_time()
    def close_app()
    def countdown(time_in_seconds, start=False)

Key Variables

  • _alarm_id: Stores the ID of scheduled after() call
  • _paused: Boolean flag for pause state
  • _start_time: Initial time in seconds

Timer Logic

  1. User enters time in seconds
  2. start_time() validates input and starts countdown
  3. countdown() recursively calls itself every 1000ms (1 second)
  4. Each iteration:
    • Checks if paused
    • Decrements time if not paused
    • Updates display
    • Changes color based on remaining time
    • Schedules next call using master.after(1000, ...)
  5. When time reaches 0:
    • Shows completion message
    • Resets button states

Non-Blocking Design

Uses master.after() for event-driven countdown:

  • No threading needed
  • UI remains responsive
  • Can pause/resume/reset at any time

Color Scheme

  • Green (#1abc9c): Normal countdown (> 30 seconds)
  • Orange (#f39c12): Warning (11-30 seconds)
  • Red (#e74c3c): Critical (≤ 10 seconds) and finished

GUI Layout

┌─────────────────────────────────────┐
│      ⏱️ CountDown Timer              │
│                                      │
│   [Enter Time in seconds]            │
│                                      │
│   [Start]  [Pause]                   │
│   [Reset]  [Close]                   │
│                                      │
│        00:00:00                      │
│                                      │
└─────────────────────────────────────┘

Key Functions

create_widgets()

Creates all GUI components:

  • Title label
  • Input entry field
  • Control buttons (Start, Pause, Reset, Close)
  • Countdown display label

start_time()

  • Validates user input
  • Converts to integer
  • Checks for positive value
  • Initializes countdown
  • Updates button states

pause_time()

  • Toggles _paused flag
  • Changes button text (Pause ↔ Resume)
  • Changes button color

reset_time()

  • Cancels scheduled after() call
  • Resets all variables
  • Clears display
  • Restores initial button states

countdown(time_in_seconds, start=False)

  • Recursive function called every second
  • Updates display with formatted time
  • Changes color based on remaining time
  • Handles pause state
  • Shows completion message when done

Input Validation

  • Checks for empty input
  • Validates numeric input
  • Ensures positive values
  • Shows appropriate error messages

Example Usage

5-Minute Timer

Input: 300
Display: 00:05:00 → 00:04:59 → ... → 00:00:01 → 00:00:00

1-Hour Timer

Input: 3600
Display: 01:00:00 → 00:59:59 → ... → 00:00:01 → 00:00:00

Keyboard Shortcuts

  • Enter: Start timer (when input field is focused)
  • Escape: Close confirmation dialog

Customization

Change Window Size

self.master.geometry("width x height")

Change Colors

# Normal color
self.label.config(fg="#your_color")

# Background
self.master.config(bg="#your_color")

Change Timer Interval

# Update every 100ms instead of 1000ms
self._alarm_id = self.master.after(100, self.countdown, time_in_seconds, False)

Add Sound Alert

def countdown(self, time_in_seconds, start=False):
    if time_in_seconds == 0:
        import winsound
        winsound.Beep(1000, 1000)  # Frequency, Duration

Learning Outcomes

  • Tkinter GUI development
  • OOP design with inheritance
  • Event-driven programming
  • Non-blocking timers using after()
  • State management (pause/resume)
  • Input validation
  • Time formatting and calculations
  • User experience design (color feedback)

Future Enhancements

  • Preset timer buttons (1 min, 5 min, 10 min, etc.)
  • Multiple simultaneous timers
  • Timer history
  • Custom alarm sounds
  • Save/load timer presets
  • System tray integration
  • Keyboard shortcuts for all actions
  • Progress bar visualization
  • Dark/light theme toggle
  • Timer templates (Pomodoro, workout, cooking, etc.)

Known Limitations

  • Precision limited to 1 second
  • No persistence (resets on close)
  • Single timer at a time
  • No background operation when minimized

Troubleshooting

Timer not starting

  • Ensure you've entered a valid number
  • Check that the number is positive
  • Make sure you clicked "Start" or pressed Enter

Timer not visible

  • Check if window is minimized
  • Ensure window is not behind other windows

Buttons not responding

  • Wait for current operation to complete
  • Try resetting the timer

License

This project is open source and available for educational purposes.