回答編集履歴
5
追記
answer
CHANGED
@@ -60,4 +60,90 @@
|
|
60
60
|
---
|
61
61
|
> time.sleep(10000)の代わり
|
62
62
|
|
63
|
-
走らせたい処理をメソッドに閉じ込め、タイマーと紐づければ良いだけかと。
|
63
|
+
走らせたい処理をメソッドに閉じ込め、タイマーと紐づければ良いだけかと。
|
64
|
+
|
65
|
+
追記
|
66
|
+
---
|
67
|
+
```Python
|
68
|
+
from datetime import datetime
|
69
|
+
|
70
|
+
from PyQt5.QtCore import *
|
71
|
+
from PyQt5.QtWidgets import *
|
72
|
+
|
73
|
+
|
74
|
+
class Main(QWidget):
|
75
|
+
def __init__(self, parent=None):
|
76
|
+
QWidget.__init__(self, parent)
|
77
|
+
|
78
|
+
self._check_timer = QTimer()
|
79
|
+
self._check_timer.timeout.connect(self._check)
|
80
|
+
self._check_timer.setInterval(20000)
|
81
|
+
|
82
|
+
self._layout = QVBoxLayout()
|
83
|
+
|
84
|
+
self._start_button = QPushButton('start')
|
85
|
+
self._layout.addWidget(self._start_button)
|
86
|
+
self._start_button.pressed.connect(
|
87
|
+
lambda: QTimer.singleShot(0, self._check)
|
88
|
+
)
|
89
|
+
|
90
|
+
self._stop_button = QPushButton('stop')
|
91
|
+
self._layout.addWidget(self._stop_button)
|
92
|
+
self._stop_button.pressed.connect(self._check_timer.stop)
|
93
|
+
|
94
|
+
self.setLayout(self._layout)
|
95
|
+
|
96
|
+
|
97
|
+
def _check(self):
|
98
|
+
def check_n_times(n):
|
99
|
+
if n == 0:
|
100
|
+
print()
|
101
|
+
return
|
102
|
+
|
103
|
+
print(f'{datetime.now().strftime("%M.%S.%f")} check once')
|
104
|
+
QTimer.singleShot(1000, lambda: check_n_times(n-1))
|
105
|
+
|
106
|
+
check_n_times(10)
|
107
|
+
self._check_timer.start()
|
108
|
+
|
109
|
+
|
110
|
+
if __name__ == "__main__":
|
111
|
+
import sys
|
112
|
+
app = QApplication(sys.argv)
|
113
|
+
|
114
|
+
m = Main()
|
115
|
+
m.show()
|
116
|
+
|
117
|
+
sys.exit(app.exec_())
|
118
|
+
```
|
119
|
+
|
120
|
+
**実行イメージ**
|
121
|
+
```plain
|
122
|
+
25.16.160331 check once
|
123
|
+
25.17.189607 check once
|
124
|
+
25.18.189929 check once
|
125
|
+
25.19.190254 check once
|
126
|
+
25.20.190572 check once
|
127
|
+
25.21.190903 check once
|
128
|
+
25.22.191227 check once
|
129
|
+
25.23.191553 check once
|
130
|
+
25.24.191876 check once
|
131
|
+
25.25.192206 check once
|
132
|
+
|
133
|
+
25.36.182811 check once
|
134
|
+
25.37.183130 check once
|
135
|
+
25.38.183461 check once
|
136
|
+
25.39.183786 check once
|
137
|
+
25.40.184114 check once
|
138
|
+
25.41.184437 check once
|
139
|
+
25.42.185751 check once
|
140
|
+
25.43.186082 check once
|
141
|
+
25.44.186401 check once
|
142
|
+
25.45.186736 check once
|
143
|
+
```
|
144
|
+
|
145
|
+
あんまり上手く書けませんでした。
|
146
|
+
- stopを押しても、即座には停止してくれない
|
147
|
+
- ワンセットの検査に要する秒数は20000msに決め打ち
|
148
|
+
|
149
|
+
とりあえず動作はします。
|
4
修正
answer
CHANGED
@@ -23,25 +23,37 @@
|
|
23
23
|
self._timer.start(1000)
|
24
24
|
```
|
25
25
|
|
26
|
-
|
26
|
+
あるいは、タイマーにインターバルを設定したり。
|
27
27
|
```Python
|
28
|
+
self._timer = QtCore.QTimer()
|
29
|
+
self._timer.setInterval(1000)
|
30
|
+
self._timer.timeout.connect(self._run)
|
31
|
+
|
32
|
+
self._start_button.pressed.connect(self._timer.start)
|
33
|
+
self._stop_button.pressed.connect(self._timer.stop)
|
34
|
+
|
35
|
+
|
36
|
+
def _run(self):
|
37
|
+
print('Run')
|
38
|
+
```
|
39
|
+
|
40
|
+
こんなユーティリティ関数を用意しておいても便利かもしれません...
|
41
|
+
...と思いましたが、GCがtimerオブジェクトを回収してしまうようなので、やや運用は面倒です。
|
42
|
+
```Python
|
28
43
|
def cyclicRun(msec, startSignal, stopSignal, run):
|
29
44
|
timer = QtCore.QTimer()
|
45
|
+
timer.setInterval(msec)
|
30
46
|
|
31
|
-
def runner():
|
32
|
-
run()
|
33
|
-
timer.start(msec)
|
34
|
-
|
35
|
-
startSignal.connect(
|
47
|
+
startSignal.connect(timer.start)
|
36
48
|
stopSignal.connect(timer.stop)
|
37
49
|
|
38
|
-
timer.timeout.connect(
|
50
|
+
timer.timeout.connect(run)
|
39
51
|
```
|
40
52
|
|
41
53
|
```Python
|
42
54
|
cyclicRun(
|
43
55
|
1000, self._start_button.pressed, self._stop_button.pressed, self._run
|
44
|
-
)
|
56
|
+
)
|
45
57
|
```
|
46
58
|
|
47
59
|
質問編集を受けて
|
3
追記
answer
CHANGED
@@ -23,6 +23,27 @@
|
|
23
23
|
self._timer.start(1000)
|
24
24
|
```
|
25
25
|
|
26
|
+
こんなユーティリティ関数を用意しておいても便利かもしれませんね。
|
27
|
+
```Python
|
28
|
+
def cyclicRun(msec, startSignal, stopSignal, run):
|
29
|
+
timer = QtCore.QTimer()
|
30
|
+
|
31
|
+
def runner():
|
32
|
+
run()
|
33
|
+
timer.start(msec)
|
34
|
+
|
35
|
+
startSignal.connect(lambda: timer.start(msec))
|
36
|
+
stopSignal.connect(timer.stop)
|
37
|
+
|
38
|
+
timer.timeout.connect(runner)
|
39
|
+
```
|
40
|
+
|
41
|
+
```Python
|
42
|
+
cyclicRun(
|
43
|
+
1000, self._start_button.pressed, self._stop_button.pressed, self._run
|
44
|
+
)
|
45
|
+
```
|
46
|
+
|
26
47
|
質問編集を受けて
|
27
48
|
---
|
28
49
|
> time.sleep(10000)の代わり
|
2
追記
answer
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
**註: これは『1秒ごとに処理を実行する方法』です。**
|
2
|
+
|
1
3
|
次の二点によって達成できます。
|
2
4
|
0. タイマーのtimeoutシグナルとaddメソッドを前以て紐付ける
|
3
5
|
0. addメソッド内でタイマーを再度走らせる
|
1
追記
answer
CHANGED
@@ -19,4 +19,10 @@
|
|
19
19
|
def _run(self):
|
20
20
|
print('Run')
|
21
21
|
self._timer.start(1000)
|
22
|
-
```
|
22
|
+
```
|
23
|
+
|
24
|
+
質問編集を受けて
|
25
|
+
---
|
26
|
+
> time.sleep(10000)の代わり
|
27
|
+
|
28
|
+
走らせたい処理をメソッドに閉じ込め、タイマーと紐づければ良いだけかと。
|