QTimer: clock is not increasing the time every second
-
Hi all :)
My code displays the current time, but the time does not change at all.
Can someone please explain why?
Thanks in advance :)from PyQt5 import QtCore, QtGui, QtWidgets import sys import res class Ui_Form(object): def setTime(self): time = QtCore.QTime.currentTime() text = time.toString("HH:mm") self.label.setText(text) def setupUi(self, Form): Form.setObjectName("Form") Form.resize(400, 114) self.label = QtWidgets.QLabel(Form) self.label.setGeometry(QtCore.QRect(30, 20, 341, 71)) font = QtGui.QFont() font.setPointSize(40) self.label.setFont(font) self.label.setAlignment(QtCore.Qt.AlignCenter) self.label.setObjectName("label") timer = QtCore.QTimer(Form) timer.timeout.connect(self.setTime) timer.start(1000) self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) self.label.setText(_translate("Form", "TextLabel")) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) Form = QtWidgets.QWidget() ui = Ui_Form() ui.setupUi(Form) Form.show() sys.exit(app.exec_())
-
Hi all :)
My code displays the current time, but the time does not change at all.
Can someone please explain why?
Thanks in advance :)from PyQt5 import QtCore, QtGui, QtWidgets import sys import res class Ui_Form(object): def setTime(self): time = QtCore.QTime.currentTime() text = time.toString("HH:mm") self.label.setText(text) def setupUi(self, Form): Form.setObjectName("Form") Form.resize(400, 114) self.label = QtWidgets.QLabel(Form) self.label.setGeometry(QtCore.QRect(30, 20, 341, 71)) font = QtGui.QFont() font.setPointSize(40) self.label.setFont(font) self.label.setAlignment(QtCore.Qt.AlignCenter) self.label.setObjectName("label") timer = QtCore.QTimer(Form) timer.timeout.connect(self.setTime) timer.start(1000) self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) self.label.setText(_translate("Form", "TextLabel")) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) Form = QtWidgets.QWidget() ui = Ui_Form() ui.setupUi(Form) Form.show() sys.exit(app.exec_())
@strayaaaa said in QTimer: clock is not increasing the time every second:
timer = QtCore.QTimer(Form)
This is a local variable which goes out of scope at the end of
setupUi()
, destroying the timer. You probably want to useself.timer
so it lasts as long asUi_Form
does. -
Thanks for your reply :)
I think I understand what you mean about scope, but changing my code to the below does not increase the time.from PyQt5 import QtCore, QtGui, QtWidgets import sys import res class Ui_Form(object): def setTime(self): time = QtCore.QTime.currentTime() text = time.toString("HH:mm") self.label.setText(text) def setupUi(self, Form): Form.setObjectName("Form") Form.resize(400, 114) self.label = QtWidgets.QLabel(Form) self.label.setGeometry(QtCore.QRect(30, 20, 341, 71)) font = QtGui.QFont() font.setPointSize(40) self.label.setFont(font) self.label.setAlignment(QtCore.Qt.AlignCenter) self.label.setObjectName("label") self.timer = QtCore.QTimer(Form) self.timer.timeout.connect(self.setTime) self.timer.start(1000) self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) self.label.setText(_translate("Form", "TextLabel")) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) Form = QtWidgets.QWidget() ui = Ui_Form() ui.setupUi(Form) Form.show() sys.exit(app.exec_())
-
Thanks for your reply :)
I think I understand what you mean about scope, but changing my code to the below does not increase the time.from PyQt5 import QtCore, QtGui, QtWidgets import sys import res class Ui_Form(object): def setTime(self): time = QtCore.QTime.currentTime() text = time.toString("HH:mm") self.label.setText(text) def setupUi(self, Form): Form.setObjectName("Form") Form.resize(400, 114) self.label = QtWidgets.QLabel(Form) self.label.setGeometry(QtCore.QRect(30, 20, 341, 71)) font = QtGui.QFont() font.setPointSize(40) self.label.setFont(font) self.label.setAlignment(QtCore.Qt.AlignCenter) self.label.setObjectName("label") self.timer = QtCore.QTimer(Form) self.timer.timeout.connect(self.setTime) self.timer.start(1000) self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) self.label.setText(_translate("Form", "TextLabel")) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) Form = QtWidgets.QWidget() ui = Ui_Form() ui.setupUi(Form) Form.show() sys.exit(app.exec_())
@strayaaaa
I think this looks better. Not sure now. Putprint(text)
or similar or a debug breakpoint intosetTime()
method, is it getting called? -
@strayaaaa
I think this looks better. Not sure now. Putprint(text)
or similar or a debug breakpoint intosetTime()
method, is it getting called?Your code works for me. I changed the setTime method to the following so I could see changes sooner (by including the seconds) and printing to the console.
def setTime(self): time = QtCore.QTime.currentTime() text = time.toString("HH:mm:ss") print(f'Setting time to {text}') self.label.setText(text)
-
@strayaaaa
I think this looks better. Not sure now. Putprint(text)
or similar or a debug breakpoint intosetTime()
method, is it getting called? -
-
Did you see my minor modification to show seconds? With your code, you will only see a change every minute since you are only displaying hours and minutes (it's updating every second but for most of each minute, it's updating it with the same value).
@mchinand Yes, I did see your code. Thanks a lot :) Because I am new in this community, the system wouldn't let me post 2 replies within 10 minutes, so I couldn't reply to your message. :) It never occurred to me that my time wasn't updating by the second because I didn't even have the seconds displayed! Doh! :D
It works now! Thanks for the replies! :)