Creating a Simple Stopwatch using Tkinter in Python: A Step-by-Step Guide

Creating a Simple Stopwatch using Tkinter in Python: A Step-by-Step Guide

Creating a Simple Stopwatch using Tkinter in Python: A Step-by-Step Guide
Creating a Simple Stopwatch using Tkinter in Python: A Step-by-Step Guide

Introduction

A stopwatch is a useful tool for timing events and activities. It can be used in various settings such as sports, cooking, and even in work or school. Creating a simple stopwatch using the Tkinter library in Python is a great way to learn how to use this library and create a useful tool for everyday use.

Setting up the Tkinter

library The first step in creating a stopwatch using Tkinter is to import the library. This can be done by using the following command:

import tkinter as tk

Once the library is imported, we can create a new window for our stopwatch. This can be done by using the following command:

root = tk.Tk()

Creating the Stopwatch function

To create the stopwatch function, we will use the time module in Python. This module allows us to track the time in seconds, minutes, and hours. We will use the time.time() function to get the current time and store it in a variable. We will then use the time.sleep() function to pause the program for a set amount of time.

import time

start_time = 0

def start():
    global start_time
    start_time = time.time()

def stop():
    global start_time
    elapsed_time = time.time() - start_time
    print("Elapsed Time: ", elapsed_time)

Adding the Start, Stop and Reset buttons

Once we have our stopwatch function set up, we can add buttons to the Tkinter window to allow the user to start, stop, and reset the stopwatch. This can be done by using the following commands:

start_button = tk.Button(root, text="Start", command=start)
stop_button = tk.Button(root, text="Stop", command=stop)
reset_button = tk.Button(root, text="Reset", command=reset)

Full Code Above app

import tkinter as tk
import time

root = tk.Tk()

start_time = 0

def start():
    global start_time
    start_time = time.time()

def stop():
    global start_time
    elapsed_time = time.time() - start_time
    print("Elapsed Time: ", elapsed_time)
    
def reset():
    global start_time
    start_time = 0

start_button = tk.Button(root, text="Start", command=start)
stop_button = tk.Button(root, text="Stop", command=stop)
reset_button = tk.Button(root, text="Reset", command=reset)

start_button.pack()
stop_button.pack()
reset_button.pack()

root.mainloop()

Output Application

How it works

The stopwatch works by using the time module to track the time in seconds, minutes, and hours. The start button begins tracking the time by storing the current time in a variable. The stop button stops tracking the time and calculates the elapsed time by subtracting the start time from the current time. The reset button sets the elapsed time to 0 and starts tracking the time again.

See also  Operators and expressions in Python

Conclusion

Creating a simple stopwatch using Tkinter in Python is a great way to learn how to use this library and create a useful tool for everyday use. By using the time module and adding buttons, we were able to create a functional stopwatch that can be used in various settings. With a little bit of tweaking, you can also add a display for the stopwatch on the GUI.

Print Friendly, PDF & Email

Author

Leave a Reply