Categories We Write About

Build a Python productivity timer

To build a simple productivity timer in Python, we can use the time library for tracking the timer and tkinter for creating a graphical user interface (GUI). Here’s an example of a Pomodoro-style productivity timer. This timer works by focusing for a set duration (e.g., 25 minutes) followed by a short break (e.g., 5 minutes). After a few cycles, you can have a longer break.

Steps to create the timer:

  1. Set Timer Intervals: You will need a focus session (e.g., 25 minutes) and break intervals (e.g., 5 minutes).

  2. Graphical User Interface (GUI): We will use tkinter for a simple interface to start, stop, and reset the timer.

  3. Sound Notifications: Optional sound notifications when the timer ends.

Python Code for the Timer

python
import tkinter as tk import time import threading import winsound # For sound notifications (Windows only) # Constants for work and break times WORK_TIME = 25 * 60 # 25 minutes BREAK_TIME = 5 * 60 # 5 minutes LONG_BREAK_TIME = 15 * 60 # 15 minutes (after 4 Pomodoro sessions) # Timer class to manage the countdown class Timer: def __init__(self, master): self.master = master self.running = False self.session_time = WORK_TIME self.pomodoro_count = 0 # Label to show time remaining self.time_label = tk.Label(self.master, text=self.format_time(self.session_time), font=("Helvetica", 30)) self.time_label.pack(pady=20) # Buttons self.start_button = tk.Button(self.master, text="Start", width=15, command=self.start_timer) self.start_button.pack(pady=10) self.stop_button = tk.Button(self.master, text="Stop", width=15, command=self.stop_timer, state=tk.DISABLED) self.stop_button.pack(pady=10) self.reset_button = tk.Button(self.master, text="Reset", width=15, command=self.reset_timer, state=tk.DISABLED) self.reset_button.pack(pady=10) def start_timer(self): if not self.running: self.running = True self.start_button.config(state=tk.DISABLED) self.stop_button.config(state=tk.NORMAL) self.reset_button.config(state=tk.DISABLED) threading.Thread(target=self.countdown).start() def stop_timer(self): self.running = False self.start_button.config(state=tk.NORMAL) self.stop_button.config(state=tk.DISABLED) self.reset_button.config(state=tk.NORMAL) def reset_timer(self): self.running = False self.pomodoro_count = 0 self.session_time = WORK_TIME self.time_label.config(text=self.format_time(self.session_time)) self.start_button.config(state=tk.NORMAL) self.stop_button.config(state=tk.DISABLED) self.reset_button.config(state=tk.DISABLED) def countdown(self): while self.running and self.session_time > 0: time.sleep(1) self.session_time -= 1 self.update_time_label() if self.session_time == 0: winsound.Beep(1000, 500) # Sound notification self.change_session() def update_time_label(self): self.time_label.config(text=self.format_time(self.session_time)) def format_time(self, seconds): minutes = seconds // 60 seconds = seconds % 60 return f"{minutes:02d}:{seconds:02d}" def change_session(self): # Handle switching between work and break periods self.pomodoro_count += 1 if self.pomodoro_count % 4 == 0: self.session_time = LONG_BREAK_TIME else: self.session_time = BREAK_TIME if self.pomodoro_count % 2 == 1 else WORK_TIME # Update the time label and reset buttons self.update_time_label() self.start_button.config(state=tk.NORMAL) self.stop_button.config(state=tk.DISABLED) self.reset_button.config(state=tk.NORMAL) # Main function to set up the GUI def main(): root = tk.Tk() root.title("Productivity Timer") root.geometry("400x300") timer = Timer(root) root.mainloop() if __name__ == "__main__": main()

Key Features:

  1. Start Button: Starts the timer and counts down.

  2. Stop Button: Stops the timer at any time.

  3. Reset Button: Resets the timer to the initial state.

  4. Pomodoro Cycles: After each Pomodoro (work) session, it switches between work and break periods. After 4 work sessions, a long break is taken.

  5. Sound Notification: A sound will play when a session (work or break) finishes.

  6. GUI with Tkinter: A simple GUI displays the countdown timer and provides buttons for controlling the timer.

How to Run:

  1. Install the necessary libraries (if not already available):

    bash
    pip install tk
  2. Simply run the Python file:

    bash
    python timer.py

This code will create a simple Pomodoro timer that alternates between work and break periods. If you need further modifications or enhancements, feel free to ask!

Share This Page:

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories We Write About