Skip to content
Related Articles
Open in App
Not now

Related Articles

PyQt5 – Digital StopWatch

Improve Article
Save Article
  • Last Updated : 23 Jan, 2023
Improve Article
Save Article

In this article we will see how we can create a stop watch using Python GUI – PyQt5. A stopwatch is a handheld timepiece designed to measure the amount of time that elapses between its activation and deactivation. A large digital version of a stopwatch designed for viewing at a distance, as in a sports stadium, is called a Stopclock.

Steps to create a stop watch – 1. Create a counter which signify the seconds 2. Create flag to know when to start and when to pause, set it to False 3. Create label to show seconds 4. Create three push button for starting the stop watch pausing the stop watch and for re-setting the stop watch 5. Add action to each push button. 6. Inside start push button action make flag to true 7. Inside pause push button action make flag to false 8. Inside reset push button action make flag to false and make the counter value to 0 9. Create QTimer object which calls a method after every 100 millisecond i.e 10th part of second 10. Inside QTimer object method check for the flag status if it is true increment the counter 11. Show the counter value through label using setText method.

Below is the implementation – 


# importing libraries
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class Window(QMainWindow):
    def __init__(self):
        # setting title
        self.setWindowTitle("Python Stop watch")
        # setting geometry
        self.setGeometry(100, 100, 400, 500)
        # calling method
        # showing all the widgets
    # method for widgets
    def UiComponents(self):
        # counter
        self.count = 0
        # creating flag
        self.flag = False
        # creating a label to show the time
        self.label = QLabel(self)
        # setting geometry of label
        self.label.setGeometry(75, 100, 250, 70)
        # adding border to the label
        self.label.setStyleSheet("border : 4px solid black;")
        # setting text to the label
        # setting font to the label
        self.label.setFont(QFont('Arial', 25))
        # setting alignment to the text of label
        # creating start button
        start = QPushButton("Start", self)
        # setting geometry to the button
        start.setGeometry(125, 250, 150, 40)
        # add action to the method
        # creating pause button
        pause = QPushButton("Pause", self)
        # setting geometry to the button
        pause.setGeometry(125, 300, 150, 40)
        # add action to the method
        # creating reset button
        re_set = QPushButton("Re-set", self)
        # setting geometry to the button
        re_set.setGeometry(125, 350, 150, 40)
        # add action to the method
        # creating a timer object
        timer = QTimer(self)
        # adding action to timer
        # update the timer every tenth second
    # method called by timer
    def showTime(self):
        # checking if flag is true
        if self.flag:
            # incrementing the counter
            self.count+= 1
        # getting text from count
        text = str(self.count / 10)
        # showing text
    def Start(self):
        # making flag to true
        self.flag = True
    def Pause(self):
        # making flag to False
        self.flag = False
    def Re_set(self):
        # making flag to false
        self.flag = False
        # resetting the count
        self.count = 0
        # setting text to label
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app

Output :

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!