Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. QTimer: clock is not increasing the time every second
Forum Updated to NodeBB v4.3 + New Features

QTimer: clock is not increasing the time every second

Scheduled Pinned Locked Moved Solved Qt for Python
8 Posts 3 Posters 900 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    strayaaaa
    wrote on last edited by
    #1

    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_())
    
    JonBJ 1 Reply Last reply
    0
    • S strayaaaa

      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_())
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @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 use self.timer so it lasts as long as Ui_Form does.

      1 Reply Last reply
      2
      • S Offline
        S Offline
        strayaaaa
        wrote on last edited by
        #3

        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_())
        
        JonBJ 1 Reply Last reply
        0
        • S strayaaaa

          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_())
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @strayaaaa
          I think this looks better. Not sure now. Put print(text) or similar or a debug breakpoint into setTime() method, is it getting called?

          M S 2 Replies Last reply
          0
          • JonBJ JonB

            @strayaaaa
            I think this looks better. Not sure now. Put print(text) or similar or a debug breakpoint into setTime() method, is it getting called?

            M Offline
            M Offline
            mchinand
            wrote on last edited by
            #5

            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)
            
            1 Reply Last reply
            1
            • JonBJ JonB

              @strayaaaa
              I think this looks better. Not sure now. Put print(text) or similar or a debug breakpoint into setTime() method, is it getting called?

              S Offline
              S Offline
              strayaaaa
              wrote on last edited by
              #6

              @JonB I added the print(text) in the setTime() method, and the method is definitely getting called every second, but the time on the label remains unchanged. So weird.

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mchinand
                wrote on last edited by
                #7

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

                S 1 Reply Last reply
                4
                • M mchinand

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

                  S Offline
                  S Offline
                  strayaaaa
                  wrote on last edited by strayaaaa
                  #8

                  @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! :)

                  1 Reply Last reply
                  2

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved