Change language

Create stopwatch using python

| | | |

# Python program to create
# new window using Tkinter
# import required libraries

import tkinter

 
# create top object as an instance of Tk class

top = tkinter.Tk ()

 
# This will launch a blank window
top.mainloop ()

Output:

< / p>

Creating a stopwatch using Tkinter

Now let’s try to create a program using the Tkinter module to create a stopwatch. 
Stopwatch — it is a hand-held chronometer designed to measure the amount of time elapsed from a certain point when it is activated until the moment when a part is deactivated. A large digital version of the stopwatch, designed to be viewed from a distance, like in a sports stadium, is called a stopwatch. In manual mode, the watch is started and stopped by the person pressing the button. At fully automatic time, start and stop are triggered automatically by the sensors.

Required modules: we will only use tkinter to create the GUI and no other libraries will be used in this program.

# Python program to illustrate a stopwatch
# using Tkinter
# import required libraries

import tkinter as Tkinter

 

counter = - 1

running = False

def counter_label (label):

def count ():

if running:

global counter

  

  # To control the initial latency.

if counter = = - 1

display = "Starting ..."

 < / code> else :

display = str (counter)

 

label [ ’text’ ] = display  # Or label.config (text = display)

 

# label.after (arg1, arg2) delays

# first argument in milliseconds

# and then calls the function given as the second argument.

  # Generally as we need to call here

  # the function in which it appears again.

# 1000 delay ms = 1 second and repeated call count.

label.after ( 1000 , count) 

counter + = 1

 

# Start launch counter.

count () 

 
# launch function stopwatch

def Start (label):

global running

  running = True

counter_label (label)

start [ ’state’ ] = ’ disabled’

stop [ ’state’ ] = ’normal’

  reset [ ’state’ ] = ’ normal’

 
# Stopwatch function

def Stop ():

global running

  start [ ’state’ ] = ’ normal’

stop [ ’state’ ] = ’disabled’

  reset [ ’ state’ ] = ’normal ’

  running = False

 
# Reset stopwatch function

def Reset (label):

global counter

counter = - 1

 

# If rest is pressed after pressing stops.

if running = = False

reset [ ’state’ ] = ’ disabled’

label [ ’text’ ] = ’Welcome!’

 

# If the reset button is pressed during stopwatch.

else

label [ ’text’ ] = ’Starting ...’

 

< p> root = Tkinter.Tk ()

root.title ( "Stopwatch" )

 
# Fixes the window size.

root.minsize (width = 250 , height = 70 )

label = Tkinter.Label (root, text = "Welcome!" , fg = "black" , font = "Verdana 30 bold " )

label.pack ()

start = Tkinter.Button (root, text = ’Start’

width = 15 , command = lambda : Start (label))

stop = Tkinter.Button (root, text = ’ Stop’

width = 15 , state = ’disabled’ , command = Stop) < / code>

reset = Tkinter.Button (root, text = ’Reset’ ,

width = 15 , state = ’disabled’ , command = lambda : Reset (label) )

start.pack ()
stop.pack ()
reset.pack ()
root.mainloop ()

Output:

Links Links

1. Wikipedia — Stopwatch
2. Python docs — tkinter
3. Github Code
4. Tutorial — Tkinter

This article courtesy of Subhajit Saha . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you’d like to share more information on the topic discussed above.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically