如何在pyqt中创建一个计时器

时间:2016-12-26 06:57:57

标签: python pyqt pyqt4 pyqt5

我有一个问题可能很简单,但我没有解决它 我想使用QTimeEdit在pyqt中创建一个计时器,默认时间从00:00:00开始,每秒都在增加。我已经尝试了以下代码,但只在添加一秒后停止。

self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.time)
self.timer.start(1000)

def time(self):
    self.upTime.setTime(QtCore.QTime(00,00,00).addSecs())

4 个答案:

答案 0 :(得分:5)

with s as ( select 1 as pk, 100 as sales union all select 2, 50 union all select 3, 250 union all select 4, 100 union all select 5, 500 ) select *, sum(sales) over () from s ; pk | sales | ?column? ----+-------+---------- 1 | 100 | 1000 2 | 50 | 1000 3 | 250 | 1000 4 | 100 | 1000 5 | 500 | 1000 不会改变时间,但会返回更改的时间。您必须使用{yout time}.addSecs(1)

{yout time} = {yout time}.addSecs(1)

输出:

import sys

from PyQt5 import QtCore


def timerEvent():
    global time
    time = time.addSecs(1)
    print(time.toString("hh:mm:ss"))


app = QtCore.QCoreApplication(sys.argv)

timer = QtCore.QTimer()
time = QtCore.QTime(0, 0, 0)

timer.timeout.connect(timerEvent)
timer.start(100)

sys.exit(app.exec_())

答案 1 :(得分:3)

我无法测试,但我认为你需要

self.curr_time = QtCore.QTime(00,00,00)

self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.time)
self.timer.start(1000)

def time(self):
    self.curr_time = self.curr_time.addSecs()
    self.upTime.setTime(self.curr_time))

您必须仅创建QtCore.QTime(00,00,00)一次,然后在time()中增加其值。

现在您始终使用QtCore.QTime(00,00,00)并增加此值。

答案 2 :(得分:1)

您只需要在QTimeEdit中获取当前时间并将其增加一秒:

def time(self):
    self.upTime.setTime(self.upTime.time().addSecs(1))

确保在正常运行时间内正确初始化QTimeEdit

self.upTime.setTime(QtCore.QTime(0, 0, 0))
self.upTime.setDisplayFormat('hh:mm:ss')

答案 3 :(得分:0)

这是一个解决方案:

import sys

from PySide import QtCore

def calculo():
    global time
    time = time.addSecs(1)
    print(time.toString("hh:mm:ss"))

app = QtCore.QCoreApplication(sys.argv)

timer0 = QtCore.QTimer()
time = QtCore.QTime(0, 0, 0)
timer0.setInterval(1000)
timer0.timeout.connect(calculo)
timer0.start()

sys.exit(app.exec_())