Hi! Hope you're enjoying this blog. I have a new home at www.goldsborough.me. Be sure to also check by there for new posts <3

Saturday, August 17, 2013

PyQt Stopwatch and Timer

Made these two in about an hour, just wanted to share with you ☺

Stopwatch:


import sys
from PyQt4 import QtGui, QtCore

s = 0
m = 0
h = 0

class Main(QtGui.QMainWindow):

    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.initUI()

    def initUI(self):

        centralwidget = QtGui.QWidget(self)

        self.lcd = QtGui.QLCDNumber(self)

        self.timer = QtCore.QTimer(self)
        self.timer.timeout.connect(self.Time)

        self.start = QtGui.QPushButton("Start",self)
        self.start.clicked.connect(self.Start)

        self.stop = QtGui.QPushButton("Stop",self)
        self.stop.clicked.connect(lambda: self.timer.stop())

        self.reset = QtGui.QPushButton("Reset",self)
        self.reset.clicked.connect(self.Reset)

        grid = QtGui.QGridLayout()
        
        grid.addWidget(self.start,1,0)
        grid.addWidget(self.stop,1,1)
        grid.addWidget(self.reset,1,2)
        grid.addWidget(self.lcd,2,0,1,3)

        centralwidget.setLayout(grid)

        self.setCentralWidget(centralwidget)

#---------Window settings --------------------------------
        
        self.setGeometry(300,300,280,170)

    def Reset(self):
        global s,m,h

        self.timer.stop()

        s = 0
        m = 0
        h = 0

        time = "{0}:{1}:{2}".format(h,m,s)

        self.lcd.setDigitCount(len(time))
        self.lcd.display(time)

    def Start(self):
        global s,m,h
        
        self.timer.start(1000)
    
    def Time(self):
        global s,m,h

        if s < 59:
            s += 1
        else:
            if m < 59:
                s = 0
                m += 1
            elif m == 59 and h < 24:
                h += 1
                m = 0
                s = 0 
            else:
                self.timer.stop()

        time = "{0}:{1}:{2}".format(h,m,s)

        self.lcd.setDigitCount(len(time))
        self.lcd.display(time)

def main():
    app = QtGui.QApplication(sys.argv)
    main= Main()
    main.show()

    sys.exit(app.exec_())

if __name__ == "__main__":
    main()



Timer:


import sys
from PyQt4 import QtGui, QtCore

class Main(QtGui.QMainWindow):

    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.initUI()

    def initUI(self):

        self.timer = QtCore.QTimer(self)

        self.lcd = QtGui.QLCDNumber(self)
        self.lcd.setDigitCount(8)

        self.time = QtGui.QTimeEdit(self)
        self.timer.timeout.connect(self.Time)

        self.set = QtGui.QPushButton("Set",self)
        self.set.clicked.connect(self.Set)

        self.stop = QtGui.QPushButton("Stop",self)
        self.stop.clicked.connect(lambda: self.timer.stop())

        grid = QtGui.QGridLayout(self)

        grid.addWidget(self.lcd,3,0)
        grid.addWidget(self.time,0,0)
        grid.addWidget(self.set,1,0)
        grid.addWidget(self.stop,2,0)

        centralwidget = QtGui.QWidget()

        self.setCentralWidget(centralwidget)

        centralwidget.setLayout(grid)

#---------Window settings --------------------------------
        
        self.setGeometry(300,300,280,170)

    def Set(self):
        global t,h,m,s
        
        t = self.time.time()
        self.lcd.display(t.toString())

        self.timer.start(1000)

        h = t.hour()
        m = t.minute()
        s = t.second()

    def Time(self):
        global t,h,m,s

        if s > 0:
            s -=1
        else:
            if m > 0:
                m -= 1
                s = 59
            elif m == 0 and h > 0:
                h -= 1
                m = 59
                s = 59
            else:
                self.timer.stop()

                stop = QtGui.QMessageBox.warning(self,"Time is up","Time is up")

        time = ("{0}:{1}:{2}".format(h,m,s))

        self.lcd.setDigitCount(len(time))
        self.lcd.display(time)

def main():
    app = QtGui.QApplication(sys.argv)
    main= Main()
    main.show()

    sys.exit(app.exec_())

if __name__ == "__main__":
    main()



1 comment :