Skip to content Skip to sidebar Skip to footer

How To Create A Timer In Pyqt

I have a question that may be simple but i hav failed to get it solved I want to create a timer in pyqt using QTimeEdit with default time starting at 00:00:00 and increasing every

Solution 1:

{yout time}.addSecs(1) does not change time, but returns the changed time. Your must be use {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(1000)

sys.exit(app.exec_())

Output:

00:00:0100:00:0200:00:0300:00:0400:00:0500:00:0600:00:0700:00:0800:00:0900:00:1000:00:1100:00:12
#

Solution 2:

I can't test it but I think you need

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

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

deftime(self):
    self.curr_time = self.curr_time.addSecs()
    self.upTime.setTime(self.curr_time))

You have to create QtCore.QTime(00,00,00) only once and later increase its value in time().

Now you always use QtCore.QTime(00,00,00) and increase this value.

Solution 3:

You just need to take the current time in the QTimeEdit and increase it by one second:

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

And make sure the QTimeEdit is initialized properly whenever the up-time begins:

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

Solution 4:

Here is a solution:

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

Post a Comment for "How To Create A Timer In Pyqt"