2016-05-23 28 views
0

UI(Qt Designer)には、「開始」ボタン、「停止」ボタン、lcdNumberがあり、「開始」と「停止」の間の秒数を表示する必要があります" は、私はそこの指示に従っ:その行を書きながらconnectが私に提案されたもののQTimer/Python:開始から停止までの秒数

しかし、私のtimeoutは、動作していないCan't seem to get pyqt countdown timer to work:QtCore.QTimer.timeout.connect(self.tick_timer)はAttributeError:「PyQt4.QtCoreを。そのようなスロットメインウィンドウを:: FUNC() オブジェクト:pyqtSignal私も私のupdate_timer機能でそのような行(QtCore.QTimer.connect(QtCore.QTimer(), QtCore.SIGNAL("timeout()"), self, QtCore.SLOT("func()")))を実装しようとしたが、これはエラーが(オブジェクトん::接続する原因となる 『

を接続する」オブジェクトには属性がありません』 :: connect:(受信者名: 'MainWindow'))、私はタイムアウトで接続信号を使用する方法を本当に理解していません。

「タイムアウト」ラインアウトをコメントアウトするとMainWindowが表示されますが、lcdディスプレイに0:01が表示されるため、「スタート」ボタンをクリックすると明らかに「tick_timer」機能が1回だけ実行されます。

ありがとうございました!

代わりのQtCore.QTimer.start

from PyQt4 import QtCore, QtGui, uic 

class MainWindow(QtGui.QMainWindow): 

    def __init__(self, parent=None): 
     super(MainWindow, self).__init__(parent) 
     uic.loadUi('MainWindow.ui', self) 
     # Buttons 
     self.QStartButton.clicked.connect(self.start_timer) 
     self.QStopButton.clicked.connect(self.stop_timer) 
     # Timer 
     QtCore.QTimer.timeout.connect(self.tick_timer) 

    def start_timer(self): 
     self.now = 0 
     self.tick_timer() 
     QtCore.QTimer.start 

    def update_timer(self): 
     self.runtime = "%d:%02d" % (self.now/60,self.now % 60) 
     self.lcdNumber.display(self.runtime) 

    def tick_timer(self): 
     self.now += 1 
     self.update_timer() 

    def stop_timer(self): 
     QtCore.QTimer.stop 

答えて

0

更新:「秒」は今、本当の秒に対応して、私はすべての1000年ミリ秒を起こるべき「カチカチ」ということを定義する必要がありました:self.timer.start(1000)

from PyQt4 import QtCore, QtGui, uic 

class MainWindow(QtGui.QMainWindow): 

    def __init__(self, parent=None): 
     super(MainWindow, self).__init__(parent) 
     uic.loadUi('MainWindow.ui', self) 
     # Buttons 
     self.QStartButton.clicked.connect(self.start_timer) 
     self.QStopButton.clicked.connect(self.stop_timer) 

    def start_timer(self): 
     # Initialize timer 
     self.timer = QtCore.QTimer() 
     self.now = 0 
     # Update display and start timer 
     self.update_timer() 
     self.timer.timeout.connect(self.tick_timer) 
     self.timer.start(1000) # Duration of one second = 1000 msec 

    def update_timer(self): 
     self.runtime = "%d:%02d" % (self.now/60,self.now % 60) 
     self.lcdNumber.display(self.runtime) 

    def tick_timer(self): 
     self.now += 1 
     self.update_timer() 

    def stop_timer(self): 
     self.timer.stop 
0
あなたはメンバ変数として QTimerを持っている必要があります

...(stop | timeout)は、それがあるべきself.timer. ...

サンプル:

self.timer = QtCore.QTimer() 
self.timer.start() 
self.timer.stop() 
+0

私は 'self.timerを挿入する必要がありますか= QtCore.QTimer() 'はすべての関数で使用されますか?現時点では、この行はstart_timer関数内にあり、その他の関数はそれに不満を抱いています:AttributeError: 'MainWindow'オブジェクトに 'timer'属性がありません –

+0

いいえ、これは '__init__'関数でのみ必要です – Zaiborg

0

更新日:現在、カウンタは基本的に動作しますが、残念ながらタイマーの秒は短すぎます。 これを修正する方法はありますか?

even self.timer = QtCore.QTimer()は、self.timer.timeout.connect(self.tick_timer)を使わずにstart_timer関数で動作するようになりました。

ありがとうございました!

from PyQt4 import QtCore, QtGui, uic 

class MainWindow(QtGui.QMainWindow): 

    def __init__(self, parent=None): 
     super(MainWindow, self).__init__(parent) 
     uic.loadUi('MainWindow.ui', self) 
     # Buttons 
     self.QStartButton.clicked.connect(self.start_timer) 
     self.QStopButton.clicked.connect(self.stop_timer) 

    def start_timer(self): 
     # Initialize timer 
     self.timer = QtCore.QTimer() 
     self.now = 0 
     self.timer.timeout.connect(self.tick_timer) 
     # Start timer and update display 
     self.timer.start() 
     self.update_timer() 


    def update_timer(self): 
     self.runtime = "%d:%02d" % (self.now/60,self.now % 60) 
     self.lcdNumber.display(self.runtime) 

    def tick_timer(self): 
     self.now += 1 
     self.update_timer() 

    def stop_timer(self): 
     self.timer.stop 
関連する問題