Countdown Timer Project (code) in python

Countdown Timer in Python



import time

# Ask the user to enter the number of seconds
seconds = int(input("Enter the number of seconds for the countdown: "))

# Countdown loop
while seconds > 0:
    print(f"Time left: {seconds} seconds")
    time.sleep(1)  # Wait for 1 second
    seconds -= 1

print("⏰ Time's up!")


Countdown Timer with handing wrong input value



import time

print("⏳ Welcome to the Countdown Timer!")

# Ask the user to enter time in seconds
try:
    seconds = int(input("Enter the number of seconds to count down: "))

    # Countdown loop
    while seconds > 0:
        mins, secs = divmod(seconds, 60)
        time_format = f"{mins:02d}:{secs:02d}"
        print(f"Time left: {time_format}")  # Print on a new line each time
        time.sleep(1)
        seconds -= 1

    print("⏰ Time's up!")

except ValueError:
    print("❌ Please enter a valid number.")


Countdown Timer GUI (code) in python


import tkinter as tk
from tkinter import messagebox
import time

class CountdownTimer:
    def __init__(self, root):
        self.root = root
        root.title("Countdown Timer")

        # Minutes input
        tk.Label(root, text="Minutes:").grid(row=0, column=0, padx=5, pady=5)
        self.minutes_var = tk.StringVar(value="0")
        self.minutes_entry = tk.Entry(root, width=5, textvariable=self.minutes_var)
        self.minutes_entry.grid(row=0, column=1, padx=5, pady=5)

        # Seconds input
        tk.Label(root, text="Seconds:").grid(row=1, column=0, padx=5, pady=5)
        self.seconds_var = tk.StringVar(value="0")
        self.seconds_entry = tk.Entry(root, width=5, textvariable=self.seconds_var)
        self.seconds_entry.grid(row=1, column=1, padx=5, pady=5)

        # Start button
        self.start_button = tk.Button(root, text="Start Timer", command=self.start_timer)
        self.start_button.grid(row=2, column=0, columnspan=2, pady=10)

        # Label to show countdown
        self.timer_label = tk.Label(root, text="00:00", font=("Helvetica", 48))
        self.timer_label.grid(row=3, column=0, columnspan=2, pady=10)

        self.total_seconds = 0
        self.running = False

    def start_timer(self):
        if self.running:
            return  # Prevent multiple timers running

        try:
            minutes = int(self.minutes_var.get())
            seconds = int(self.seconds_var.get())

            # Validation: minutes or seconds can be zero, but not both
            if minutes < 0 or seconds < 0 or seconds >= 60:
                messagebox.showerror("Invalid input", "Enter minutes >= 0 and seconds between 0 and 59.")
                return
            if minutes == 0 and seconds == 0:
                messagebox.showerror("Invalid input", "Please enter a time greater than zero.")
                return

            self.total_seconds = minutes * 60 + seconds
            self.running = True
            self.countdown()

        except ValueError:
            messagebox.showerror("Invalid input", "Please enter valid integer values.")

    def countdown(self):
        if self.total_seconds >= 0:
            mins, secs = divmod(self.total_seconds, 60)
            time_str = f"{mins:02d}:{secs:02d}"
            self.timer_label.config(text=time_str)
            self.total_seconds -= 1
            if self.total_seconds >= 0:
                # Call countdown() again after 1000ms (1 second)
                self.root.after(1000, self.countdown)
            else:
                self.running = False
                messagebox.showinfo("Time's up!", "⏰ Time's up!")
        else:
            self.running = False

if __name__ == "__main__":
    root = tk.Tk()
    app = CountdownTimer(root)
    root.mainloop()

Post a Comment

0 Comments