Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- import time
- # Initialize the window
- window = tk.Tk()
- window.title("Stopwatch")
- # Stopwatch variables
- start_time = 0
- elapsed_time = 0
- is_running = False
- # Label to display the stopwatch time
- time_label = tk.Label(window, text="00:00:00", font=("Helvetica", 48))
- time_label.pack(pady=10)
- # Function to start the stopwatch
- def start_stopwatch():
- global start_time, is_running
- if not is_running:
- is_running = True
- start_time = time.time()
- update_stopwatch()
- # Function to stop the stopwatch
- def stop_stopwatch():
- global is_running
- if is_running:
- is_running = False
- # Function to reset the stopwatch
- def reset_stopwatch():
- global elapsed_time, is_running
- elapsed_time = 0
- is_running = False
- time_label.config(text="00:00:00")
- # Function to update the stopwatch time
- def update_stopwatch():
- global elapsed_time
- if is_running:
- elapsed_time = time.time() - start_time
- minutes = int(elapsed_time // 60)
- seconds = int(elapsed_time % 60)
- milliseconds = int((elapsed_time % 1) * 100)
- time_string = f"{minutes:02d}:{seconds:02d}:{milliseconds:02d}"
- time_label.config(text=time_string)
- window.after(10, update_stopwatch)
- # Create buttons for stopwatch control
- start_button = tk.Button(window, text="Start", command=start_stopwatch)
- start_button.pack(side=tk.LEFT, padx=5)
- stop_button = tk.Button(window, text="Stop", command=stop_stopwatch)
- stop_button.pack(side=tk.LEFT, padx=5)
- reset_button = tk.Button(window, text="Reset", command=reset_stopwatch)
- reset_button.pack(side=tk.LEFT, padx=5)
- window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement