Skip to content

Instantly share code, notes, and snippets.

@hughpearse
Last active January 3, 2024 13:56
Show Gist options
  • Save hughpearse/6208368cc5f9eaf3d3b52238670cda4f to your computer and use it in GitHub Desktop.
Save hughpearse/6208368cc5f9eaf3d3b52238670cda4f to your computer and use it in GitHub Desktop.
Task Tracker

Stopwatch Task Tracker

The application is a simple stopwatch task tracker built using the Tkinter library in Python. It was tested in Python 3.10.11. It has the following features:

Display

When the application is first opened, it displays the initial time as 00:00 at the top of the window. The title of the window is set to "Stopwatch task tracker".

Task Name Input

In the middle of the window, there's a label "Task Name:" to the left of a text input box where users can enter the name of their task.

Buttons

At the bottom of the window, there are three buttons arranged horizontally:

  • Start/Stop Button: Starts and stops the stopwatch. The button's text changes between "Start" and "Stop" depending on whether the stopwatch is running.
  • Reset Button: Resets the stopwatch to 00:00, changes the "Start/Stop" button's text to "Start", and clears the text input box.
  • Save Button: Appends the current time from the stopwatch and the text from the input box to a CSV file named times.csv. After saving, it performs a reset operation.

CSV File

The application maintains a CSV file named times.csv. Each row in the file corresponds to a "save" operation and contains the time from the stopwatch and the task name from the input box at the time of saving.

This application provides a simple way to track the time spent on different tasks. Users can start and stop the stopwatch as they work on a task, enter the task name in the input box, and then save the task name and time spent to the CSV file for later reference. The reset button allows users to clear the stopwatch and input box in preparation for the next task.

import tkinter as tk
import time
import csv
from datetime import datetime
class Stopwatch:
def __init__(self, master):
self.master = master
self.master.title("Stopwatch task tracker")
self.running = False
self.time = 0
self.start_time = 0
self.start_datetime = None
self.label = tk.Label(master, text='00:00')
self.label.pack(side='top')
self.entry_frame = tk.Frame(master)
self.entry_frame.pack(side='top')
self.entry_label = tk.Label(self.entry_frame, text='Task Name:')
self.entry_label.pack(side='left')
self.entry = tk.Entry(self.entry_frame)
self.entry.pack(side='left')
self.button_frame = tk.Frame(master)
self.button_frame.pack(side='bottom')
self.start_stop_button = tk.Button(self.button_frame, text="Start", command=self.start_stop)
self.start_stop_button.pack(side='left')
self.reset_button = tk.Button(self.button_frame, text="Reset", command=self.reset)
self.reset_button.pack(side='left')
self.save_button = tk.Button(self.button_frame, text="Save", command=self.save)
self.save_button.pack(side='left')
def start_stop(self):
if self.running:
self.running = False
self.time += time.time() - self.start_time
self.start_stop_button.config(text='Start')
else:
self.running = True
self.start_time = time.time()
self.start_datetime = datetime.now()
self.start_stop_button.config(text='Stop')
self.update_clock()
def reset(self):
self.running = False
self.time = 0
self.start_time = time.time()
self.start_datetime = None
self.label.config(text='00:00')
self.start_stop_button.config(text='Start')
self.entry.delete(0, 'end')
def save(self):
elapsed_time = self.time + (time.time() - self.start_time if self.running else 0)
minutes = int(elapsed_time / 60)
seconds = int(elapsed_time - minutes * 60)
with open('times.csv', 'a', newline='') as f:
writer = csv.writer(f)
writer.writerow([self.start_datetime.strftime('%Y-%m-%d %H:%M:%S'), datetime.now().strftime('%Y-%m-%d %H:%M:%S'), f'{minutes:02d}:{seconds:02d}', self.entry.get()])
self.reset()
def update_clock(self):
if self.running:
elapsed_time = self.time + time.time() - self.start_time
minutes = int(elapsed_time / 60)
seconds = int(elapsed_time - minutes * 60)
self.label.config(text='%02d:%02d' % (minutes, seconds))
self.master.after(1000, self.update_clock)
root = tk.Tk()
my_stopwatch = Stopwatch(root)
root.mainloop()
@hughpearse
Copy link
Author

IMG-20240103-WA0000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment