回答編集履歴

5

追記

2018/11/19 07:28

投稿

LouiS0616
LouiS0616

スコア35676

test CHANGED
@@ -123,3 +123,175 @@
123
123
 
124
124
 
125
125
  走らせたい処理をメソッドに閉じ込め、タイマーと紐づければ良いだけかと。
126
+
127
+
128
+
129
+ 追記
130
+
131
+ ---
132
+
133
+ ```Python
134
+
135
+ from datetime import datetime
136
+
137
+
138
+
139
+ from PyQt5.QtCore import *
140
+
141
+ from PyQt5.QtWidgets import *
142
+
143
+
144
+
145
+
146
+
147
+ class Main(QWidget):
148
+
149
+ def __init__(self, parent=None):
150
+
151
+ QWidget.__init__(self, parent)
152
+
153
+
154
+
155
+ self._check_timer = QTimer()
156
+
157
+ self._check_timer.timeout.connect(self._check)
158
+
159
+ self._check_timer.setInterval(20000)
160
+
161
+
162
+
163
+ self._layout = QVBoxLayout()
164
+
165
+
166
+
167
+ self._start_button = QPushButton('start')
168
+
169
+ self._layout.addWidget(self._start_button)
170
+
171
+ self._start_button.pressed.connect(
172
+
173
+ lambda: QTimer.singleShot(0, self._check)
174
+
175
+ )
176
+
177
+
178
+
179
+ self._stop_button = QPushButton('stop')
180
+
181
+ self._layout.addWidget(self._stop_button)
182
+
183
+ self._stop_button.pressed.connect(self._check_timer.stop)
184
+
185
+
186
+
187
+ self.setLayout(self._layout)
188
+
189
+
190
+
191
+
192
+
193
+ def _check(self):
194
+
195
+ def check_n_times(n):
196
+
197
+ if n == 0:
198
+
199
+ print()
200
+
201
+ return
202
+
203
+
204
+
205
+ print(f'{datetime.now().strftime("%M.%S.%f")} check once')
206
+
207
+ QTimer.singleShot(1000, lambda: check_n_times(n-1))
208
+
209
+
210
+
211
+ check_n_times(10)
212
+
213
+ self._check_timer.start()
214
+
215
+
216
+
217
+
218
+
219
+ if __name__ == "__main__":
220
+
221
+ import sys
222
+
223
+ app = QApplication(sys.argv)
224
+
225
+
226
+
227
+ m = Main()
228
+
229
+ m.show()
230
+
231
+
232
+
233
+ sys.exit(app.exec_())
234
+
235
+ ```
236
+
237
+
238
+
239
+ **実行イメージ**
240
+
241
+ ```plain
242
+
243
+ 25.16.160331 check once
244
+
245
+ 25.17.189607 check once
246
+
247
+ 25.18.189929 check once
248
+
249
+ 25.19.190254 check once
250
+
251
+ 25.20.190572 check once
252
+
253
+ 25.21.190903 check once
254
+
255
+ 25.22.191227 check once
256
+
257
+ 25.23.191553 check once
258
+
259
+ 25.24.191876 check once
260
+
261
+ 25.25.192206 check once
262
+
263
+
264
+
265
+ 25.36.182811 check once
266
+
267
+ 25.37.183130 check once
268
+
269
+ 25.38.183461 check once
270
+
271
+ 25.39.183786 check once
272
+
273
+ 25.40.184114 check once
274
+
275
+ 25.41.184437 check once
276
+
277
+ 25.42.185751 check once
278
+
279
+ 25.43.186082 check once
280
+
281
+ 25.44.186401 check once
282
+
283
+ 25.45.186736 check once
284
+
285
+ ```
286
+
287
+
288
+
289
+ あんまり上手く書けませんでした。
290
+
291
+ - stopを押しても、即座には停止してくれない
292
+
293
+ - ワンセットの検査に要する秒数は20000msに決め打ち
294
+
295
+
296
+
297
+ とりあえず動作はします。

4

修正

2018/11/19 07:28

投稿

LouiS0616
LouiS0616

スコア35676

test CHANGED
@@ -48,7 +48,37 @@
48
48
 
49
49
 
50
50
 
51
+ あるいは、タイマーにインターバルを設定したり。
52
+
53
+ ```Python
54
+
55
+ self._timer = QtCore.QTimer()
56
+
57
+ self._timer.setInterval(1000)
58
+
59
+ self._timer.timeout.connect(self._run)
60
+
61
+
62
+
63
+ self._start_button.pressed.connect(self._timer.start)
64
+
65
+ self._stop_button.pressed.connect(self._timer.stop)
66
+
67
+
68
+
69
+
70
+
71
+ def _run(self):
72
+
73
+ print('Run')
74
+
75
+ ```
76
+
77
+
78
+
51
- こんなユーティリティ関数を用意しておいても便利かもしれませんね。
79
+ こんなユーティリティ関数を用意しておいても便利かもしれません...
80
+
81
+ ...と思いましたが、GCがtimerオブジェクトを回収してしまうようなので、やや運用は面倒です。
52
82
 
53
83
  ```Python
54
84
 
@@ -56,23 +86,17 @@
56
86
 
57
87
  timer = QtCore.QTimer()
58
88
 
59
-
60
-
61
- def runner():
62
-
63
- run()
64
-
65
- timer.start(msec)
89
+ timer.setInterval(msec)
66
90
 
67
91
 
68
92
 
69
- startSignal.connect(lambda: timer.start(msec))
93
+ startSignal.connect(timer.start)
70
94
 
71
95
  stopSignal.connect(timer.stop)
72
96
 
73
97
 
74
98
 
75
- timer.timeout.connect(runner)
99
+ timer.timeout.connect(run)
76
100
 
77
101
  ```
78
102
 
@@ -84,7 +108,7 @@
84
108
 
85
109
  1000, self._start_button.pressed, self._stop_button.pressed, self._run
86
110
 
87
- )
111
+ )
88
112
 
89
113
  ```
90
114
 

3

追記

2018/11/19 03:23

投稿

LouiS0616
LouiS0616

スコア35676

test CHANGED
@@ -48,6 +48,48 @@
48
48
 
49
49
 
50
50
 
51
+ こんなユーティリティ関数を用意しておいても便利かもしれませんね。
52
+
53
+ ```Python
54
+
55
+ def cyclicRun(msec, startSignal, stopSignal, run):
56
+
57
+ timer = QtCore.QTimer()
58
+
59
+
60
+
61
+ def runner():
62
+
63
+ run()
64
+
65
+ timer.start(msec)
66
+
67
+
68
+
69
+ startSignal.connect(lambda: timer.start(msec))
70
+
71
+ stopSignal.connect(timer.stop)
72
+
73
+
74
+
75
+ timer.timeout.connect(runner)
76
+
77
+ ```
78
+
79
+
80
+
81
+ ```Python
82
+
83
+ cyclicRun(
84
+
85
+ 1000, self._start_button.pressed, self._stop_button.pressed, self._run
86
+
87
+ )
88
+
89
+ ```
90
+
91
+
92
+
51
93
  質問編集を受けて
52
94
 
53
95
  ---

2

追記

2018/11/19 03:10

投稿

LouiS0616
LouiS0616

スコア35676

test CHANGED
@@ -1,3 +1,7 @@
1
+ **註: これは『1秒ごとに処理を実行する方法』です。**
2
+
3
+
4
+
1
5
  次の二点によって達成できます。
2
6
 
3
7
  0. タイマーのtimeoutシグナルとaddメソッドを前以て紐付ける

1

追記

2018/11/19 02:51

投稿

LouiS0616
LouiS0616

スコア35676

test CHANGED
@@ -41,3 +41,15 @@
41
41
  self._timer.start(1000)
42
42
 
43
43
  ```
44
+
45
+
46
+
47
+ 質問編集を受けて
48
+
49
+ ---
50
+
51
+ > time.sleep(10000)の代わり
52
+
53
+
54
+
55
+ 走らせたい処理をメソッドに閉じ込め、タイマーと紐づければ良いだけかと。