xopenex
xopenex

Reputation: 263

Python Timer with Qt Design Button and LCD number

Using Qt Designer, and python2.7. I am trying to create a "count up" timer out of a Qt Designer button and LCD number. I would like a single button to start and reset the timer and the LCD number to display the amount of time that has passed with hh:mm:ss format. I am also trying to have a window "pop-up" at 40 minutes, and display a message "Good Job You Made It!"

I have searched for answers, tried many different combinations of google searches... and now all my results are showing up purple (I've already followed the link)! LOL I see many examples in other languages and for countdown timers and for, what seems like, any other and all other combinations of timers and languages... but none for python! Seriously, I've been trying to figure this out for days, and just haven't got anywhere with it.

Below is the code that I do have. I'm thinking the part I need is the rest of the "def doStartReset(self):" function/method.

Hope I am clear enough. Thanks!

#!/usr/bin/python2.7
import sys
from PyQt4 import QtGui,QtCore
from timer_ui import *

class MyForm(QtGui.QMainWindow):
        def __init__(self, parent=None):
                #build parent user interface
                QtGui.QWidget.__init__(self, parent)
                self.ui = Ui_MainWindow()
                self.ui.setupUi(self)

                QtCore.QObject.connect(self.ui.btnStartReset, QtCore.SIGNAL('clicked()'), self.doStartReset)

        def doStartReset(self):



if __name__ == "__main__":
        #This function means this was run directly, not called from another python  file.
        app = QtGui.QApplication(sys.argv)
        myapp = MyForm()
        myapp.show()
        sys.exit(app.exec_())

Here is the code for the GUI in _ui.py format if you need it.

# -*- coding: utf-8 -*-

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8 except AttributeError:
    _fromUtf8 = lambda s: s

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(340, 205)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.widget = QtGui.QWidget(self.centralwidget)
        self.widget.setGeometry(QtCore.QRect(40, 50, 261, 81))
        self.widget.setObjectName(_fromUtf8("widget"))
        self.gridLayout = QtGui.QGridLayout(self.widget)
        self.gridLayout.setMargin(0)
        self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
        self.btnStartReset = QtGui.QPushButton(self.widget)
        self.btnStartReset.setObjectName(_fromUtf8("btnStartReset"))
        self.gridLayout.addWidget(self.btnStartReset, 0, 0, 1, 1)
        self.lcd40MinTimer = QtGui.QLCDNumber(self.widget)
        self.lcd40MinTimer.setObjectName(_fromUtf8("lcd40MinTimer"))
        self.gridLayout.addWidget(self.lcd40MinTimer, 0, 1, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))
        self.btnStartReset.setText(QtGui.QApplication.translate("MainWindow", "Start / Reset", None, QtGui.QApplication.UnicodeUTF8))

Thank you in advance.


Hey Guys.. here is some progress that i've made... sad that its taken me three days to accomplish this much, but hey, none the less it is progress! Now i am going to work on setting format to hh:mm:ss and have button function include start, stop, and reset... Maybe this will help some people and maybe someone can help me! Together I am convinced that we can make it happen! CHEERS!!!

#!/usr/bin/python
# -*- coding: utf-8 -*-



import sys from PyQt4 import QtGui, QtCore

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):

        self.lcd = QtGui.QLCDNumber(self)
        self.lcd.setGeometry(30, 40, 200, 25)

        self.btn = QtGui.QPushButton('Start', self)
        self.btn.move(40, 80)
        self.btn.clicked.connect(self.doAction)

        self.timer = QtCore.QBasicTimer()
        self.step = 0

        self.setGeometry(300, 300, 280, 170)
        self.setWindowTitle('QtGui.QLCDNumber')
        self.show()

    def timerEvent(self, e):

        if self.step >= 100:
            self.timer.stop()
            self.btn.setText('Finished')
            return

        self.step = self.step + 1
        self.lcd.display(self.step)

    def doAction(self):

        if self.timer.isActive():
            self.timer.stop()
            self.btn.setText('Start')
        else:
            self.timer.start(100, self)
            self.btn.setText('Stop')

def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

Upvotes: 0

Views: 4825

Answers (1)

twigmac
twigmac

Reputation: 1813

xopenex, some time ago I've coded a count-up timer in Python using Qt. Could this be what you've been looking for?

https://github.com/twigmac/count-up-timer

Upvotes: 1

Related Questions