註: これは『1秒ごとに処理を実行する方法』です。
次の二点によって達成できます。
0. タイマーのtimeoutシグナルとaddメソッドを前以て紐付ける
0. addメソッド内でタイマーを再度走らせる
Python
1 def __init__ ( self , parent = None ) :
2 . . .
3
4 self . _timer = QtCore . QTimer ( )
5 self . _timer . timeout . connect ( self . _run )
6
7 self . _start_button . pressed . connect (
8 lambda : self . _timer . start ( 1000 )
9 )
10 self . _stop_button . pressed . connect (
11 self . _timer . stop
12 )
13
14 def _run ( self ) :
15 print ( 'Run' )
16 self . _timer . start ( 1000 )
あるいは、タイマーにインターバルを設定したり。
Python
1 self . _timer = QtCore . QTimer ( )
2 self . _timer . setInterval ( 1000 )
3 self . _timer . timeout . connect ( self . _run )
4
5 self . _start_button . pressed . connect ( self . _timer . start )
6 self . _stop_button . pressed . connect ( self . _timer . stop )
7
8
9 def _run ( self ) :
10 print ( 'Run' )
こんなユーティリティ関数を用意しておいても便利かもしれません...
...と思いましたが、GCがtimerオブジェクトを回収してしまうようなので、やや運用は面倒です。
Python
1 def cyclicRun ( msec , startSignal , stopSignal , run ) :
2 timer = QtCore . QTimer ( )
3 timer . setInterval ( msec )
4
5 startSignal . connect ( timer . start )
6 stopSignal . connect ( timer . stop )
7
8 timer . timeout . connect ( run )
Python
1 cyclicRun (
2 1000 , self . _start_button . pressed , self . _stop_button . pressed , self . _run
3 )
質問編集を受けて
time.sleep(10000)の代わり
走らせたい処理をメソッドに閉じ込め、タイマーと紐づければ良いだけかと。
追記
Python
1 from datetime import datetime
2
3 from PyQt5 . QtCore import *
4 from PyQt5 . QtWidgets import *
5
6
7 class Main ( QWidget ) :
8 def __init__ ( self , parent = None ) :
9 QWidget . __init__ ( self , parent )
10
11 self . _check_timer = QTimer ( )
12 self . _check_timer . timeout . connect ( self . _check )
13 self . _check_timer . setInterval ( 20000 )
14
15 self . _layout = QVBoxLayout ( )
16
17 self . _start_button = QPushButton ( 'start' )
18 self . _layout . addWidget ( self . _start_button )
19 self . _start_button . pressed . connect (
20 lambda : QTimer . singleShot ( 0 , self . _check )
21 )
22
23 self . _stop_button = QPushButton ( 'stop' )
24 self . _layout . addWidget ( self . _stop_button )
25 self . _stop_button . pressed . connect ( self . _check_timer . stop )
26
27 self . setLayout ( self . _layout )
28
29
30 def _check ( self ) :
31 def check_n_times ( n ) :
32 if n == 0 :
33 print ( )
34 return
35
36 print ( f' { datetime . now ( ) . strftime ( "%M.%S.%f" ) } check once' )
37 QTimer . singleShot ( 1000 , lambda : check_n_times ( n - 1 ) )
38
39 check_n_times ( 10 )
40 self . _check_timer . start ( )
41
42
43 if __name__ == "__main__" :
44 import sys
45 app = QApplication ( sys . argv )
46
47 m = Main ( )
48 m . show ( )
49
50 sys . exit ( app . exec_ ( ) )
実行イメージ
plain
1 25.16.160331 check once
2 25.17.189607 check once
3 25.18.189929 check once
4 25.19.190254 check once
5 25.20.190572 check once
6 25.21.190903 check once
7 25.22.191227 check once
8 25.23.191553 check once
9 25.24.191876 check once
10 25.25.192206 check once
11
12 25.36.182811 check once
13 25.37.183130 check once
14 25.38.183461 check once
15 25.39.183786 check once
16 25.40.184114 check once
17 25.41.184437 check once
18 25.42.185751 check once
19 25.43.186082 check once
20 25.44.186401 check once
21 25.45.186736 check once
あんまり上手く書けませんでした。
stopを押しても、即座には停止してくれない
ワンセットの検査に要する秒数は20000msに決め打ち
とりあえず動作はします。