Search code examples
pythonpyqtpyqt4pyqt5

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 second. I've tried the following code but it stops after adding only one second.

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

Solution

  • 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)
    
    def time(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.