回答編集履歴

7

コード修正。変数が未定義: count, interval

2020/04/28 16:37

投稿

teamikl
teamikl

スコア8664

test CHANGED
@@ -222,7 +222,7 @@
222
222
 
223
223
 
224
224
 
225
- def run(self):
225
+ def run(self, count=10, interval=1):
226
226
 
227
227
  while True:
228
228
 

6

属性名を変更 self.target -> self.win (Threadのコンストラクタにもtarget引数があるので、混同されるのを避ける為)

2020/04/28 16:37

投稿

teamikl
teamikl

スコア8664

test CHANGED
@@ -46,7 +46,7 @@
46
46
 
47
47
  # イベント通知対象のウィジェット。(MainWindow)
48
48
 
49
- self.target = win
49
+ self.win = win
50
50
 
51
51
 
52
52
 
@@ -88,7 +88,7 @@
88
88
 
89
89
  # 通知 (MainLoopのキューにイベントを入れる)
90
90
 
91
- wx.PostEvent(self.target, event)
91
+ wx.PostEvent(self.win, event)
92
92
 
93
93
 
94
94
 
@@ -218,7 +218,7 @@
218
218
 
219
219
  # 通知 (MainLoopのキューにイベントを入れる)
220
220
 
221
- wx.PostEvent(self.target, event)
221
+ wx.PostEvent(self.win, event)
222
222
 
223
223
 
224
224
 

5

分離方法の修正

2020/04/28 11:41

投稿

teamikl
teamikl

スコア8664

test CHANGED
@@ -182,7 +182,7 @@
182
182
 
183
183
  Pythonの数値型は多倍長なので、桁溢れの心配はないとはいえ
184
184
 
185
- 気になる部分なので、改善案。
185
+ 気になる部分なので、改善案。追記: インデントが深くならないように再度修正。
186
186
 
187
187
 
188
188
 
@@ -202,7 +202,27 @@
202
202
 
203
203
 
204
204
 
205
- def countWithInterval(self, count=10, interval=1):
205
+ def onCountInterval(self):
206
+
207
+ # 次に表示する画像ファイル
208
+
209
+ filepath = self.nextImageFile()
210
+
211
+
212
+
213
+ # イベント通知する為、引数となるイベントオブジェクトを作成
214
+
215
+ event = UpdateImageEvent(filepath=filepath)
216
+
217
+
218
+
219
+ # 通知 (MainLoopのキューにイベントを入れる)
220
+
221
+ wx.PostEvent(self.target, event)
222
+
223
+
224
+
225
+ def run(self):
206
226
 
207
227
  while True:
208
228
 
@@ -212,28 +232,8 @@
212
232
 
213
233
  else:
214
234
 
215
- yield
216
-
217
-
218
-
219
- def run(self):
220
-
221
- for _ in self.countWithInterval(count=10):
235
+ self.onCountInterval()
222
-
223
- # 次に表示する画像ファイル
236
+
224
-
225
- filepath = self.nextImageFile()
237
+
226
-
227
-
228
-
229
- # イベント通知する為、引数となるイベントオブジェクトを作成
230
-
231
- event = UpdateImageEvent(filepath=filepath)
232
-
233
-
234
-
235
- # 通知 (MainLoopのキューにイベントを入れる)
236
-
237
- wx.PostEvent(self.target, event)
238
238
 
239
239
  ```

4

説明修正

2020/04/28 01:06

投稿

teamikl
teamikl

スコア8664

test CHANGED
@@ -237,9 +237,3 @@
237
237
  wx.PostEvent(self.target, event)
238
238
 
239
239
  ```
240
-
241
-
242
-
243
- 追記: 自分で実装しなくても、threading.Timer で良かったです。
244
-
245
- ちなみに、wxにも同様のクラスが用意されています。

3

タイマークラスについて追記

2020/04/28 00:23

投稿

teamikl
teamikl

スコア8664

test CHANGED
@@ -237,3 +237,9 @@
237
237
  wx.PostEvent(self.target, event)
238
238
 
239
239
  ```
240
+
241
+
242
+
243
+ 追記: 自分で実装しなくても、threading.Timer で良かったです。
244
+
245
+ ちなみに、wxにも同様のクラスが用意されています。

2

説明修正

2020/04/28 00:06

投稿

teamikl
teamikl

スコア8664

test CHANGED
@@ -186,7 +186,7 @@
186
186
 
187
187
 
188
188
 
189
- カウント部分の処理と、ロジックの分離もできます。
189
+ カウント自体の処理と、カウント毎処理の分離もできます。
190
190
 
191
191
 
192
192
 

1

カウントタイマーの手法について

2020/04/27 23:25

投稿

teamikl
teamikl

スコア8664

test CHANGED
@@ -171,3 +171,69 @@
171
171
  main()
172
172
 
173
173
  ```
174
+
175
+
176
+
177
+
178
+
179
+ ----
180
+
181
+ 数値のカウントの方法について、
182
+
183
+ Pythonの数値型は多倍長なので、桁溢れの心配はないとはいえ
184
+
185
+ 気になる部分なので、改善案。
186
+
187
+
188
+
189
+ カウント部分の処理と、ロジックの分離もできます。
190
+
191
+
192
+
193
+ ```python
194
+
195
+ class CountTimer(Thread):
196
+
197
+ daemon = True
198
+
199
+
200
+
201
+ # 省略
202
+
203
+
204
+
205
+ def countWithInterval(self, count=10, interval=1):
206
+
207
+ while True:
208
+
209
+ for _ in range(count):
210
+
211
+ time.sleep(interval)
212
+
213
+ else:
214
+
215
+ yield
216
+
217
+
218
+
219
+ def run(self):
220
+
221
+ for _ in self.countWithInterval(count=10):
222
+
223
+ # 次に表示する画像ファイル
224
+
225
+ filepath = self.nextImageFile()
226
+
227
+
228
+
229
+ # イベント通知する為、引数となるイベントオブジェクトを作成
230
+
231
+ event = UpdateImageEvent(filepath=filepath)
232
+
233
+
234
+
235
+ # 通知 (MainLoopのキューにイベントを入れる)
236
+
237
+ wx.PostEvent(self.target, event)
238
+
239
+ ```