Build a Real-Time Countdown Timer in Python

Need a terminal-based countdown timer for your script? Maybe to delay tasks, simulate loading, or create focus timers?

In this post, you'll build a sleek countdown timer using:

  • time.sleep() for delays

  • tqdm for terminal progress bars

  • argparse to customize duration from CLI


🛠️ What You’ll Build

bashCopyEditpython countdown.py --minutes 1

➡️ This runs a 1-minute countdown, updating in real time:

plaintextCopyEdit⏳ Countdown: 00:59 ▓▓▓▓░░░░░░░░░░░░░ 25%

📦 Step 1: Install tqdm

bashCopyEditpip install tqdm

📄 Step 2: countdown.py

pythonCopyEditimport time
from tqdm import tqdm
import argparse

def countdown_timer(seconds):
    for i in tqdm(range(seconds), desc="⏳ Countdown", unit="s", ncols=70):
        time.sleep(1)

🧪 Step 3: Add CLI Support

pythonCopyEditdef parse_args():
    parser = argparse.ArgumentParser(description="Run a countdown timer.")
    parser.add_argument("--minutes", type=int, default=1, help="Minutes to count down")
    return parser.parse_args()

if __name__ == "__main__":
    args = parse_args()
    total_seconds = args.minutes * 60
    countdown_timer(total_seconds)
    print("\n🚀 Time's up!")

🚀 Run It

bashCopyEditpython countdown.py --minutes 2

✅ Terminal shows a dynamic progress bar and countdown seconds.


🧩 Bonus: Show Time Left in Text

Enhance with actual time display:

pythonCopyEditdef countdown_timer(seconds):
    for i in tqdm(range(seconds), desc="⏳ Countdown", ncols=70):
        mins, secs = divmod(seconds - i, 60)
        tqdm.write(f"⏰ {mins:02d}:{secs:02d}")
        time.sleep(1)

Note: tqdm.write() prints above the bar, preserving formatting.


🎯 Use Cases

  • Break timers (Pomodoro technique)

  • CLI loading demos

  • Sleep/wait between automation steps

  • Educational games (e.g., quiz timers)


🧵 Wrapping Up

With just a few lines of Python and tqdm, you've built a real-time countdown timer for the terminal — reusable, customizable, and cool-looking. 🎉

Want to turn this into a full-featured Pomodoro CLI app next? Or wrap it in a GUI with Tkinter? Let’s go!

0
Subscribe to my newsletter

Read articles from Ashraful Islam Leon directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ashraful Islam Leon
Ashraful Islam Leon

Passionate Software Developer | Crafting clean code and elegant solutions