0

我需要使用pyQt4编写代码在滑块小部件上以分钟为单位预先设置时间,然后使用按钮小部件启动计时器,计数器将继续,并在设定的时间计数器将停止。我是 pyQt4 的新手,因此请求您的专家建议来编写这个程序。我做了一些工作,但没有给出正确的结果。

非常感谢

那拉卡

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()
4

1 回答 1

1

更改:

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

至:

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

您想与滑块进行比较value()valueChanged()只是触发一个事件并返回None

于 2013-09-03T15:20:42.373 回答