Digital stopwatch GUI Application in Python – PyQt5

In this post, I’ll be discussing how you can create a digital stopwatch GUI application using PyQt5 in Python. I will be using the QtDesigner utility of PyQt5 for designing the layout of the stopwatch window. All you need is to install pyqt5 and its dev-tools. This GUI application source code should be workable on both Windows and Linux without any trouble.

The Layout of the stopwatch that I will create will look like this:
Digital stopwatch GUI application in Python

This is a “dialog without button” type form. It has 4 push buttons and 1 LCD Number widget. After arranging the widgets as in the picture, change the Object Name of the four pushbuttons as “pushButtonPause“, “pushButtonReset“, “pushButtonStart” and “pushButtonStop” respectively(so that it becomes easy to understand each of the buttons during coding).

Now save the .ui file as “stopwatch.ui“. And then from terminal move to the same directory where your stopwatch.ui file is. Then type pyuic5 stopwatch.ui -o stopwatch.py.
Digital stopwatch GUI application in Python

A Python file will be generated in the same directory. Now create another Python fileĀ  “call_StopWatch.py” and edit this file.

  • Import the required modules as the top.
    import datetime
    from demo_stop_watch import *
    from PyQt5 import QtCore, QtGui
    from PyQt5.QtWidgets import QDialog, QApplication
    import sys
  • Now create a class, MyForm(QDialog): to initialize the widgets of the Ui class as below:
    class MyForm(QDialog):
        def __init__(self):
            super().__init__()
            self.ui = Ui_Dialog()
            self.ui.setupUi(self)
            self.timer = QtCore.QTimer(self)
            self.timer.timeout.connect(self.run_watch)
            self.timer.setInterval(1)
            self.mscounter = 0
            self.isreset = True
    # creating slots for event handling (button press)
            self.ui.pushButtonStart.clicked.connect(self.start_watch)
            self.ui.pushButtonStop.clicked.connect(self.stop_watch)
            self.ui.pushButtonPause.clicked.connect(self.watch_pause)
            self.ui.pushButtonReset.clicked.connect(self.watch_reset)
            self.showLCD()

     

  • Create the methods, start_watch, stop_watch, watch_pause, watch_reset for clicks on respective buttons:
    def showLCD(self):
        text = str(datetime.timedelta(milliseconds=self.mscounter))[:-3]
        self.ui.lcdNumber.setDigitCount(11)
        if not self.isreset:  # if "isreset" is False
            self.ui.lcdNumber.display(text)
        else:
            self.ui.lcdNumber.display('0:00:00.000')
    
    def run_watch(self):
        self.mscounter += 1
        self.showLCD()
    
    def start_watch(self):
        self.timer.start()
        self.isreset = False
        self.ui.pushButtonReset.setDisabled(True)
        self.ui.pushButtonStart.setDisabled(True)
        self.ui.pushButtonStop.setDisabled(False)
        self.ui.pushButtonPause.setDisabled(False)
    
    def stop_watch(self):
        self.timer.stop()
        self.mscounter = 0
    
        self.ui.pushButtonReset.setDisabled(False)
        self.ui.pushButtonStart.setDisabled(False)
        self.ui.pushButtonStop.setDisabled(True)
        self.ui.pushButtonPause.setDisabled(True)
    
    def watch_pause(self):
        self.timer.stop()
    
        self.ui.pushButtonReset.setDisabled(False)
        self.ui.pushButtonStart.setDisabled(False)
        self.ui.pushButtonStop.setDisabled(True)
        self.ui.pushButtonPause.setDisabled(True)
    
    def watch_reset(self):
        self.timer.stop()
        self.mscounter = 0
        self.isreset = True
        self.showLCD()
    
        self.ui.pushButtonReset.setDisabled(True)
        self.ui.pushButtonStart.setDisabled(False)
        self.ui.pushButtonStop.setDisabled(True)
        self.ui.pushButtonPause.setDisabled(True)

     

  • And now create the main method and call the class to display the ui as follows:
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        w = MyForm()
        w.show()
        sys.exit(app.exec_())
    
    
    

The Working Process:

self.timer.timeout.connect(self.run_watch) calls the run_watch() function at regular time-intervals of 1 millisecond and this is specified using self.timer.setInterval(1). The run_watch counter simply adds up 1 millisecond every time and displays the total time elapsed using showLCD() method and with the help of datetime module’s timedelta function. Here, the isreset variable is nescessary for displaying the specific format in my UI.

Now the above process only occurs only if you press the start button in the GUI window. When you press the stop or reset button. Control goes to its respective method and either stops the counting or make all values ‘0’. For ‘Pause‘ button it simply stops the counting, and does not changes any value.

the setDisabled() function is used to disable some buttons when some other buttons are pressed, so to make things look more logical!

Finally, you can run the call_stopwatch.py file and see the fully functional stopwatch.

**remember: Due to performance time and program complexity, the watch may be a little inaccurate than other stopwatches. The error is measured to be around 5%, and this is also mentioned in the QTimer documentation itself.

The full Python code for the two files can be found here:
Digital stopwatch in Python.zip

Also read:

Leave a Reply

Your email address will not be published. Required fields are marked *