1
votes

My UI (Qt Designer) has a "Start"-button, a "Stop"-button and a lcdNumber, which should display the seconds between clicking "Start" and "Stop". I followed the instructions there: Can't seem to get pyqt countdown timer to work

But my timeout isn't working, although connect was proposed to me while writing that line: QtCore.QTimer.timeout.connect(self.tick_timer) AttributeError: 'PyQt4.QtCore.pyqtSignal' object has no attribute 'connect'

I also tried to implement lines like that (QtCore.QTimer.connect(QtCore.QTimer(), QtCore.SIGNAL("timeout()"), self, QtCore.SLOT("func()"))) in my update_timer function, but this causes errors (Object::connect: No such slot MainWindow::func() Object::connect: (receiver name: 'MainWindow')) and I don't really understand how to use the connect signal with timeout.

If I comment this "timeout"-line out, the MainWindow appears, but clicking the "Start"-button obviously runs the "tick_timer" function only once, because the lcd-display shows 0:01.

Thanks for your help!

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
3

3 Answers

0
votes

you need to have the QTimer as member variable...

instead of QtCore.QTimer.start(stop|timeout) it should be self.timer. ...

sample:

self.timer = QtCore.QTimer()
self.timer.start()
self.timer.stop()
0
votes

Update: Now the counter basically works , unfortunately the "seconds" of the timer are mutch too short. Any suggestions how to fix that?

Even self.timer = QtCore.QTimer() now works in the start_timer function, without self.timer.timeout.connect(self.tick_timer) it didn't.

Thanks for your help!

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
0
votes

Update: The "seconds" now correspond to real seconds, I had to define that a "tick" should happen every 1000 msec: self.timer.start(1000)

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