HTML STOPWATCH
import tkinter as tk
from tkinter import simpledialog
class TimerApp:
def __init__(self, root):
self.root = root
self.root.title("Timer")
self.count = 0.0 # Use float to accommodate tenths of a second
self.is_running = False
self.is_counting_up = True
self.label = tk.Label(root, text=str(self.count), font=("Helvetica", 48)) # Increased font size to 48
self.label.pack(pady=20)
self.up_button = tk.Button(root, text="Count Up", command=self.start_counting_up, font=("Helvetica", 18))
self.up_button.pack(side=tk.LEFT, padx=20)
self.down_button = tk.Button(root, text="Count Down", command=self.start_counting_down, font=("Helvetica", 18))
self.down_button.pack(side=tk.RIGHT, padx=20)
self.stop_button = tk.Button(root, text="Stop", command=self.stop_timer, font=("Helvetica", 18))
self.stop_button.pack(pady=20)
self.update_timer()
def start_counting_up(self):
self.is_counting_up = True
self.is_running = True
self.count = 0.0 # Reset count
self.label.config(text=str(self.count))
def start_counting_down(self):
self.is_counting_up = False
self.is_running = True
# Ask user for countdown time in seconds
countdown_time = simpledialog.askfloat("Input", "Set countdown time in seconds:", minvalue=0.1)
if countdown_time is not None:
self.count = countdown_time # Set countdown start
self.label.config(text=str(self.count))
def stop_timer(self):
self.is_running = False
def update_timer(self):
if self.is_running:
if self.is_counting_up:
self.count += 0.1 # Increment by tenths of a second
else:
self.count -= 0.1 # Decrement by tenths of a second
# Ensure count doesn't go below 0 for countdown
if not self.is_counting_up and self.count < 0:
self.count = 0
self.label.config(text=f"{self.count:.1f}") # Format to one decimal place
self.root.after(100, self.update_timer) # Update every 100 milliseconds
if __name__ == "__main__":
root = tk.Tk()
app = TimerApp(root)
root.mainloop()