user157947
user157947

Reputation: 101

pyQt4 timer with setting done by user

I need to use pyQt4 to write a code to pre set the time in minutes on a slider widget and then with button widget, timer is started and the counter will continue and at the set time counter will stop. I am new to pyQt4 and therefore request your expert advice to write this program. I have done some work but it is not giving the proper results.

Many Thanks

Nalaka

import sys
from PyQt4 import QtGui, QtCore

class Example(QtGui.QWidget):

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

        self.initUI()

    def initUI(self):      

        self.sld = QtGui.QSlider(QtCore.Qt.Horizontal, self)
        self.sld.setFocusPolicy(QtCore.Qt.NoFocus)
        self.sld.setGeometry(50, 100, 100, 100)
        self.sld.valueChanged[int].connect(self.changeValue)

        self.lbl1 = QtGui.QLabel(self)
        self.lbl1.move(20, 60)
        self.lbl2 = QtGui.QLabel(self)
        self.lbl2.move(20, 80)

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

        #self.le = QtGui.QLineEdit(self)
        #self.le.move(40, 120)

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

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

    def timerEvent(self, e):
        #global x

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

        self.step = self.step + 1
        self.lbl2.setNum(self.step)
        self.lbl2.adjustSize()

    def doAction(self):

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

    def changeValue(self, value):
            self.lbl1.setNum(value)
            self.lbl1.adjustSize()

def main():

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


if __name__ == '__main__':
    main()

Upvotes: 0

Views: 600

Answers (1)

Viktor Kerkez
Viktor Kerkez

Reputation: 46566

Change the:

def timerEvent(self, e):
    if self.step >= self.sld.valueChanged():

to:

def timerEvent(self, e):
    if self.step >= self.sld.value():

You want to compare against the sliders value(). valueChanged() just triggers an event and returns None.

Upvotes: 1

Related Questions