Created
May 6, 2025 17:24
-
-
Save EncodeTheCode/4b45f31f7e05270f7016ac538557f380 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tkinter as tk | |
from tkinter import ttk | |
import time | |
class CountdownTimer: | |
def __init__(self, master): | |
self.master = master | |
self.master.title("Optimized Countdown + Elapsed Timer") | |
# UI layout | |
ttk.Label(master, text="3-Day Countdown Timer:", font=("Arial", 12)).grid(row=0, column=0, sticky='w') | |
self.label_3day = ttk.Label(master, text="", font=("Consolas", 14)) | |
self.label_3day.grid(row=0, column=1, sticky='w') | |
ttk.Label(master, text="Elapsed Timer (since start):", font=("Arial", 12)).grid(row=1, column=0, sticky='w') | |
self.label_custom = ttk.Label(master, text="00:00:00", font=("Consolas", 14)) | |
self.label_custom.grid(row=1, column=1, sticky='w') | |
# Buttons | |
ttk.Button(master, text="Start", command=self.start).grid(row=2, column=0, pady=10) | |
ttk.Button(master, text="Stop", command=self.stop).grid(row=2, column=1, pady=10) | |
# Timer state | |
self.running = False | |
self.start_time_3day = None | |
self.end_time_3day = None | |
self.start_time_custom = None | |
def start(self): | |
if not self.running: | |
self.running = True | |
now = time.perf_counter() | |
self.start_time_3day = now | |
self.end_time_3day = now + (3 * 24 * 3600) # 3 days countdown | |
self.start_time_custom = now # custom timer is elapsed since start | |
self.update_loop() | |
def stop(self): | |
self.running = False | |
def update_loop(self): | |
if not self.running: | |
return | |
now = time.perf_counter() | |
# 3-Day Countdown Timer | |
remaining = max(0, int(self.end_time_3day - now)) | |
d, rem = divmod(remaining, 86400) | |
h, rem = divmod(rem, 3600) | |
m, s = divmod(rem, 60) | |
self.label_3day.config(text=f"{d}d {h:02}:{m:02}:{s:02}") | |
# Elapsed Custom Timer | |
elapsed = max(0, int(now - self.start_time_custom)) | |
eh, rem = divmod(elapsed, 3600) | |
em, es = divmod(rem, 60) | |
self.label_custom.config(text=f"{eh:02}:{em:02}:{es:02}") | |
self.master.after(250, self.update_loop) # Smooth update | |
if __name__ == "__main__": | |
root = tk.Tk() | |
CountdownTimer(root) | |
root.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment