关于qt:QTimer / Python:从开始到停止计数秒数

QTimer/Python: Count seconds from start to stop

我的UI(Qt Designer)有一个"开始"按钮,一个"停止"按钮和一个lcdNumber,它们应该显示单击"开始"和"停止"之间的秒数。
我按照那里的说明进行操作:似乎无法让pyqt倒数计时器正常工作

但是我的timeout不起作用,尽管在编写该行时向我提出了connect:QtCore.QTimer.timeout.connect(self.tick_timer)AttributeError:'PyQt4.QtCore.pyqtSignal'对象没有属性'connect'

我还试图在我的update_timer函数中实现类似(QtCore.QTimer.connect(QtCore.QTimer(), QtCore.SIGNAL("timeout()"), self, QtCore.SLOT("func()")))的行,但这会导致错误(Object :: connect:没有这样的插槽MainWindow :: func()
Object :: connect:(接收者名称:" MainWindow")),我不太了解如何在超时时使用connect信号。

如果我注释掉该"超时"行,则会出现MainWindow,但是单击"开始"按钮显然只运行一次" tick_timer"功能,因为液晶显示器显示0:01。

感谢您的帮助!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from PyQt4 import QtCore, QtGui, uic

class MainWindow(QtGui.QMainWindow):

    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        uic.loadUi('MainWindow.ui', self)
        # Buttons
        self.QStartButton.clicked.connect(self.start_timer)
        self.QStopButton.clicked.connect(self.stop_timer)
        # Timer
        QtCore.QTimer.timeout.connect(self.tick_timer)

    def start_timer(self):
        self.now = 0
        self.tick_timer()
        QtCore.QTimer.start

    def update_timer(self):
        self.runtime ="%d:%02d" % (self.now/60,self.now % 60)
        self.lcdNumber.display(self.runtime)

    def tick_timer(self):
        self.now += 1
        self.update_timer()

    def stop_timer(self):
        QtCore.QTimer.stop

更新:现在,"秒"对应于实际秒,我必须定义每1000毫秒应发生一次"滴答":self.timer.start(1000)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from PyQt4 import QtCore, QtGui, uic

class MainWindow(QtGui.QMainWindow):

    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        uic.loadUi('MainWindow.ui', self)
        # Buttons
        self.QStartButton.clicked.connect(self.start_timer)
        self.QStopButton.clicked.connect(self.stop_timer)

    def start_timer(self):
        # Initialize timer
        self.timer = QtCore.QTimer()
        self.now = 0
        # Update display and start timer
        self.update_timer()
        self.timer.timeout.connect(self.tick_timer)
        self.timer.start(1000) # Duration of one second = 1000 msec

    def update_timer(self):
        self.runtime ="%d:%02d" % (self.now/60,self.now % 60)
        self.lcdNumber.display(self.runtime)

    def tick_timer(self):
        self.now += 1
        self.update_timer()

    def stop_timer(self):
        self.timer.stop

更新:现在计数器基本上可以工作了,不幸的是计时器的"秒"太短了。
关于如何解决该问题的任何建议?

即使self.timer = QtCore.QTimer()现在也可以在start_timer函数中工作,而没有self.timer.timeout.connect(self.tick_timer)则不能。

感谢您的帮助!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from PyQt4 import QtCore, QtGui, uic

class MainWindow(QtGui.QMainWindow):

    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        uic.loadUi('MainWindow.ui', self)
        # Buttons
        self.QStartButton.clicked.connect(self.start_timer)
        self.QStopButton.clicked.connect(self.stop_timer)

    def start_timer(self):
        # Initialize timer
        self.timer = QtCore.QTimer()
        self.now = 0
        self.timer.timeout.connect(self.tick_timer)
        # Start timer and update display
        self.timer.start()
        self.update_timer()


    def update_timer(self):
        self.runtime ="%d:%02d" % (self.now/60,self.now % 60)
        self.lcdNumber.display(self.runtime)

    def tick_timer(self):
        self.now += 1
        self.update_timer()

    def stop_timer(self):
        self.timer.stop

您需要将QTimer作为成员变量...

而不是QtCore.QTimer.start(stop | timeout),应为self.timer. ...

样本:

1
2
3
self.timer = QtCore.QTimer()
self.timer.start()
self.timer.stop()