Skip to content

Instantly share code, notes, and snippets.

@devbisme
Created May 23, 2023 17:22
Show Gist options
  • Save devbisme/82c5c444210183a5d504b5f0ed6d8f53 to your computer and use it in GitHub Desktop.
Save devbisme/82c5c444210183a5d504b5f0ed6d8f53 to your computer and use it in GitHub Desktop.
Timer program created using ChatGPT.
#! /usr/bin/env python
"""
This program was created using ChatGPT (GPT4) with the following prompt:
You are an expert python programmer. Create a command-line program that accepts
a time interval as a string of "hours:minutes:seconds" and displays a small window
in which the interval counts down by seconds. It should also display a progress bar
that decreases each second until it reaches zero and the timer expires.
Upon expiration, an audible tone should be created. Produce commented Python code.
Further prompts were:
please edit the program so the remaining time is displayed as "hours:minutes:seconds".
Use argparse to accept mandatory time interval and title and also an optional command
to be executed when the timer elapses.
After that, I manually edited the program to add an option to repeat the timer once it expired.
"""
import sys
import time
import argparse
import subprocess
import tkinter as tk
from tkinter import ttk
import playsound
import pathlib
# Function to update the timer and progress bar
def update_timer():
remaining_time = int(timer_var.get())
# Update the progress bar and timer label
progress_bar['value'] = remaining_time
timer_label.config(text=gmtime_str(remaining_time))
if remaining_time <= 0:
# Play an audible tone when the timer expires
playsound.playsound(pathlib.Path.home() / "bin/notify.mp3")
# Execute the optional command
if args.command:
subprocess.Popen(args.command, shell=True)
root.destroy()
else:
root.after(1000, update_timer)
timer_var.set(remaining_time - 1)
# Convert seconds to "HH:MM:SS" while removing any leading "00:".
def gmtime_str(seconds):
time_str = time.strftime("%H:%M:%S", time.gmtime(seconds))
while time_str.startswith("00:"):
time_str = time_str[len("00:"):]
return time_str
# Convert the input time interval to seconds
def time_to_seconds(time_str):
hms = map(int, time_str.split(':'))
secs = 0
for v in hms:
secs = secs * 60 + v
return secs
if __name__ == "__main__":
# Parse command-line arguments
parser = argparse.ArgumentParser(description="Countdown timer with optional command execution")
parser.add_argument("time", help="Time interval in the format hours:minutes:seconds")
parser.add_argument("-t", "--title", default="Countdown", help="Title for the countdown timer window")
parser.add_argument("-c", "--command", help="Optional command to be executed when the timer elapses")
parser.add_argument("-r", "--repeat", type=int, default=1, help="Reset the timer and repeat on expiration.")
args = parser.parse_args()
for _ in range(args.repeat):
total_seconds = time_to_seconds(args.time)
# Create the main window
root = tk.Tk()
root.title(args.title)
# Create a label to display the timer
timer_var = tk.StringVar()
timer_var.set(total_seconds)
timer_label = tk.Label(root, text=time.strftime("%H:%M:%S", time.gmtime(total_seconds)), font=("Helvetica", 24))
timer_label.pack(pady=10)
# Create a progress bar
progress_bar = ttk.Progressbar(root, orient="horizontal", length=300, mode="determinate")
progress_bar.pack(pady=10)
progress_bar['maximum'] = total_seconds
progress_bar['value'] = total_seconds
# Start the countdown
update_timer()
# Run the main loop
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment