Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qtimer. A little assistance



  • Hello

    Some demo code for Qtimer facility that I have scoured Google for around 2 hours now is driving me mad.
    They label the page 'Qtimer tutorial' and it always seems to have some vital bit of information missing. Hence, it doesn't compile.

    All I am trying to achieve is calling a routine once a second.

    Anyone got a link to a decent Python 3.8 tutorial that an idiot can follow?


  • Lifetime Qt Champion

    Hi,

    What error do you have ?
    Can you show the code you are using ?
    It will be easier to spot the issue.



  • @Stevolution
    Glancing through the code in https://learndataanalysis.org/how-to-create-a-digital-clock-with-pyqt5-pyqt5-tutorial/ looks simple and workable. Did you try this one? I'm hoping you can copy & paste and it will just work. If you are PySide2, replacing all PyQt5 by PySide2 looks to me will work.


  • Banned

    Here is a slightly cleaned up version (problematic code has been replaced with solid code) of that code that JonB shared -- it also handles the if you use PyQt5 versus PySide2

    try:
        from PyQt5.QtCore    import QTimer, QTime, Qt, pyqtSlot
        from PyQt5.QtGui     import QFont
        from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel
    
    except ImportError:
        from PySide2.QtCore    import QTimer, QTime, Qt, Slot as pyqtSlot
        from PySide2.QtGui     import QFont
        from PySide2.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel
    
    class AppDemo(QWidget):
        def __init__(self):
            QWidget.__init__(self)
            self.resize(250, 100)
            self.setWindowTitle('Clock')
    
         #  Not a  problem but I find explicit is easier to understand and maintain
            Font = QFont()
            Font.setFamily('Open Sans')
            Font.setPointSize(36)
            Font.setBold(True)
    
            self.lblClock = QLabel()
            self.lblClock.setAlignment(Qt.AlignCenter)
            self.lblClock.setFont(Font)
    
            VBox = QVBoxLayout()
            VBox.addWidget(self.lblClock)
     
            self.setLayout(VBox)
     
            tmrClock = QTimer(self)
            tmrClock .timeout.connect(self.TickTock)
            tmrClock .start(1000) # update every second
    
          # This Primes the View 
            self.showTime()
     
        @pyqtSlot() 
        def TickTock(self):
            CurTime = QTime.currentTime()
     
            TimeDsply = CurTime.toString('hh:mm:ss')
     
            self.lblClock.setText(TimeDsply)
     
    if __name__ == "__main__":
        MainEvntThred = QApplication([])
    
        MainApp = AppDemo()
        MainApp.show()
    
        MainEvntThred.exec()
    

Log in to reply