I've searched internet for days but can figure out how to put this code to work. It's a very simple gui (made on Qt Designer) with a lcd and a button. I want it to on the press of the button to start the countdown from 180 seconds back. In the first moment i was able to make to button decrease in one the value but after trying so many different things nothing is working. Can someone help me please? Probably is something very simple. Thank you.
# -*- coding: utf-8 -*-
import sys
import time
from PyQt4 import QtCore, QtGui
from relogio import Ui_relogiocc
class StartQT4(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_relogiocc()
self.ui.setupUi(self)
self.timer = QtCore.QTimer()
text = "%d:%02d" % (180/60,180 % 60)
self.ui.QLCDNumber.display(text)
self.timer.start(1000)
self.ui.iniciar.clicked.connect(self.updateTimerDisplay)
def updateTimerDisplay(self):
self.inicio = 180
while self.inicio != 0:
text = "%d:%02d" % (self.inicio/60,self.inicio % 60)
self.ui.QLCDNumber.display(text)
self.inicio - 1
else:
self.timer.stop()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = StartQT4()
myapp.show()
sys.exit(app.exec_())
It seems there are a number of things you are missing here.
Firstly, the timer emits a
timeout()
signal whenever each timeout period completes. In your case, this is every one second. However, you're not connecting this signal to anything.Secondly, your
updateTimerDisplay
contains the following line:This reads the value of
self.inicio
, subtracts 1 from it and then throws the result away. Becauseself.inicio
's value doesn't change yourupdateTimerDisplay
method goes into an infinite loop.I'm guessing you meant
instead, which assigns the new value of
self.inicio
back to itself.Ultimately, however, it seems you're trying to use your
updateTimerDisplay
method to start the timer, count it down and also update the display of the timer. I'd recommend breaking this method up to smaller methods.Firstly,
updateTimerDisplay
should only update the display of the timer:Secondly, you'll want a method to start the timer. Something like the following should do:
Of course, you'll also need to connect your
iniciar
button'sclicked()
signal to this function, instead of toupdateTimerDisplay
.Finally, you'll need a method that handles a tick from the timer. Something like the following should do:
You'll also need to connect the
timeout()
signal of the timer to this function, using something like:As it has been told in the other answers your code contains some obvious mistakes. Here you have a complete working example (the UI is not created via Designer) that resets properly the counter every time the button is clicked (i.e. stops the timer before starting it again. If you don't do that and click the
Start
button before the timer has stopped then the counter counts faster for every button click).There are multiple things wrong with your code. For starters, you wrote
self.inicio - 1
instead of-= 1
, and you never actually use theQtimer
you create. But ignoring that, the structure of your program isn't right: Currently you callupdateTimerDisplay
when the user clicks youriniciar
button and loop in there until your countdown reaches zero. What you want to do is start the timer when the user clicks the button and connect the timer (actually itstimeout
signal) to a method that just counts down one second and updates the display:Alter your
__init__
method to connect these functions like this: