Here’s a Python program that implements a simple stopwatch with start, stop, and reset functionality using tkinter.
The stopwatch counts in seconds and milliseconds and updates every 100 milliseconds.
Code
import tkinter as tk # Initialize the main window root = tk.Tk() root.title("Stopwatch") root.geometry("300x200") # Global variables for time and state counter = 0 running = False # Update the timer display def update_time(): if running: global counter counter += 1 time_display = f"{counter//600}:{(counter//10)%60:02}.{counter%10}" label.config(text=time_display) root.after(100, update_time) # Start the stopwatch def start(): global running if not running: running = True update_time() # Stop the stopwatch def stop(): global running running = False # Reset the stopwatch def reset(): global counter, running running = False counter = 0 label.config(text="0:00.0") # Display label for the stopwatch label = tk.Label(root, text="0:00.0", font=("Helvetica", 40)) label.pack(pady=20) # Buttons for start, stop, and reset start_button = tk.Button(root, text="Start", command=start, width=10) start_button.pack(side="left", padx=10) stop_button = tk.Button(root, text="Stop", command=stop, width=10) stop_button.pack(side="left", padx=10) reset_button = tk.Button(root, text="Reset", command=reset, width=10) reset_button.pack(side="left", padx=10) # Run the main event loop root.mainloop()
Explanation of the Code
- Initialize the Main Window:
- root = tk.Tk(): Creates the main application window.
- root.title(“Stopwatch”): Sets the window title to “Stopwatch”.
- root.geometry(“300×200”): Sets the size of the window to 300×200 pixels.
- Global Variables:
- counter: Holds the current time in tenths of a second (i.e., each increment is 100ms).
- running: A boolean flag indicating whether the stopwatch is currently running.
- update_time() Function:
- This function is responsible for updating the time displayed on the label.
- It checks if running is True; if so, it:
- Increments counter by 1 (which represents 0.1 seconds).
- Formats counter to display minutes, seconds, and tenths of a second.
- counter//600 calculates the minutes.
- (counter//10)%60 calculates seconds, ensuring it resets every 60 seconds.
- counter%10 calculates tenths of a second.
- label.config(text=time_display): Updates the label with the formatted time.
- root.after(100, update_time): Schedules the update_time function to run again after 100 milliseconds, creating a loop.
- Start, Stop, and Reset Functions:
- start():
- Starts the stopwatch if it’s not already running.
- Sets running = True and calls update_time() to start updating the timer.
- stop():
- Stops the stopwatch by setting running = False.
- reset():
- Stops the stopwatch (running = False).
- Resets counter to 0.
- Updates the label text to “0:00.0”.
- start():
- Creating the Display Label:
- label = tk.Label(…): A Label widget that displays the current time.
- text=”0:00.0″ initializes the label with “0:00.0” (0 minutes, 0 seconds).
- font=(“Helvetica”, 40) sets the font size for visibility.
- Creating the Start, Stop, and Reset Buttons:
- Each button is created using tk.Button(…), specifying the button’s text, command, and width.
- start_button, stop_button, and reset_button trigger start(), stop(), and reset() respectively when clicked.
- side=”left” arranges the buttons horizontally in the window.
- Run the Main Loop:
- root.mainloop() starts the tkinter event loop, keeping the application window open and responsive.
How It Works
- Starting the Stopwatch:
- Clicking “Start” sets running = True and begins incrementing counter every 100 milliseconds.
- Stopping the Stopwatch:
- Clicking “Stop” sets running = False, stopping further updates until “Start” is clicked again.
- Resetting the Stopwatch:
- Clicking “Reset” stops the stopwatch and resets counter to 0, updating the display to show “0:00.0”.
Summary
This Python program provides a basic stopwatch with start, stop, and reset functionality using tkinter for the GUI.
The update_time() function continuously updates the display, while the start, stop, and reset functions control the stopwatch state.
The program is a simple example of using tkinter for GUI-based timing applications.