Advertisement
Anonymous0069

Countdown_Timer_Code

Mar 10th, 2024
586
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.42 KB | Source Code | 0 0
  1. import threading
  2.  
  3. import time
  4.  
  5. import tkinter as tk
  6.  
  7. from win10toast  import ToastNotifier
  8.  
  9. class Countdowntimer:
  10.     def __init__(self):
  11.         self.root = tk.Tk()
  12.         self.root.geometry("800x220")
  13.         self.root.title("Tkinter Countdown Timer")
  14.         self.root.configure(bg="black")
  15.  
  16.         self.time_entry = tk.Entry(self.root, font=("Helvetica", 30))
  17.         self.time_entry.grid(row=0, column=0, columnspan=2, padx=5, pady=5)
  18.  
  19.         self.start_button = tk.Button(self.root, font=("Helvetica", 30), text="Start", command=self.start_thread)
  20.         self.start_button.grid(row=1, column=0, padx=5, pady=5)
  21.  
  22.         self.stop_button = tk.Button(self.root, font=("Helvetica", 30), text="Reset", command=self.stop)
  23.         self.stop_button.grid(row=1, column=1, padx=5, pady=5)
  24.  
  25.         self.time_label = tk.Label(self.root, font=("Helvetica", 30), text="Time Left: 00:00:00")
  26.         self.time_label.grid(row=10, column=0, columnspan=2, padx=5, pady=5)
  27.  
  28.         self.stop_loop = False
  29.  
  30.         self.root.mainloop()
  31.  
  32.     def start_thread(self):
  33.         t = threading.Thread(target=self.start)
  34.         t.start()
  35.  
  36.     def start(self):
  37.         self.stop_loop = False
  38.         hours,minutes,seconds=0,0,0
  39.         string_split = self.time_entry.get().split(":")
  40.         if len(string_split) == 3:
  41.             hours = int(string_split[0])
  42.             minutes = int(string_split[1])
  43.             seconds = int(string_split[2])
  44.  
  45.         elif len(string_split) == 2:
  46.             minutes = int(string_split[0])
  47.             seconds = int(string_split[1])
  48.  
  49.         elif len(string_split) == 1:
  50.             seconds = int(string_split[0])
  51.  
  52.         else:
  53.             print("Invalid time format")
  54.             return
  55.         full_seconds = (hours*3600) + (minutes * 60) + seconds
  56.  
  57.         while full_seconds > 0 and not self.stop_loop:
  58.             full_seconds -= 1
  59.  
  60.             minutes, seconds = divmod(full_seconds, 60)
  61.             hours, minutes = divmod(minutes, 60)
  62.        
  63.             self.time_label.config(text=f"Time Left: {hours:02d}:{minutes:02d}:{seconds:02d}")
  64.             self.root.update()
  65.             time.sleep(1)
  66.          
  67.         if not self.stop_loop:
  68.             toast  = ToastNotifier()
  69.             toast.show_toast("Countdown Timer", "Time is up!")
  70.     def stop(self):
  71.         self.stop_loop = True
  72.         self.time_label.config(text="Time Left: 00:00:00")
  73.      
  74. Countdowntimer()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement