PyQt4 - creating a timer

前端 未结 1 1042
粉色の甜心
粉色の甜心 2020-11-29 12:38

I\'m sorry for the question but I have read a bunch of things and it seems that I do not get how to make a timer. So I\'m posting my code:

from PyQt4 import         


        
相关标签:
1条回答
  • 2020-11-29 12:41

    Use new style signals, they are easier to understand.

    Swap -

    QtCore.QTimer.connect(timer, QtCore.SIGNAL("timeout()"), self, QtCore.SLOT("func()"))
    

    With -

    timer.timeout.connect(self.move_towards)   # assuming that move_towards is the handler
    

    A simple but full example of a working timer -

    import sys
    
    from PyQt4.QtCore import QTimer
    from PyQt4.QtGui import QApplication
    
    app = QApplication(sys.argv)
    app.setQuitOnLastWindowClosed(False)
    
    def tick():
        print 'tick'
    
    timer = QTimer()
    timer.timeout.connect(tick)
    timer.start(1000)
    
    # run event loop so python doesn't exit
    app.exec_()
    
    0 讨论(0)
提交回复
热议问题