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
updateTimerDisplaycontains 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 yourupdateTimerDisplaymethod goes into an infinite loop.I’m guessing you meant
instead, which assigns the new value of
self.inicioback to itself.Ultimately, however, it seems you’re trying to use your
updateTimerDisplaymethod 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,
updateTimerDisplayshould 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
iniciarbutton’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: