回答編集履歴

1

コード修正

2020/10/23 04:16

投稿

teamikl
teamikl

スコア8664

test CHANGED
@@ -132,6 +132,10 @@
132
132
 
133
133
 
134
134
 
135
+
136
+
137
+
138
+
135
139
  import time
136
140
 
137
141
  import threading
@@ -146,7 +150,7 @@
146
150
 
147
151
 
148
152
 
149
- def tmain():
153
+ def worker():
150
154
 
151
155
  global flag
152
156
 
@@ -196,69 +200,63 @@
196
200
 
197
201
 
198
202
 
199
- def main():
200
-
201
- logging.basicConfig(
202
-
203
- level=logging.DEBUG,
204
-
205
- format="%(threadName)s %(message)s")
206
-
207
-
208
-
209
- root = tk.Tk()
210
-
211
- label = tk.Label(root)
212
-
213
- label.pack()
214
-
215
-
216
-
217
- thread = threading.Thread(target=tmain)
218
-
219
- thread.start()
220
-
221
-
222
-
223
- def close():
224
-
225
- global flag
226
-
227
- flag = False
228
-
229
-
230
-
231
- # スレッドの終了を待つブロッキング処理
232
-
233
- logging.debug("thread join")
234
-
235
- thread.join()
236
-
237
-
238
-
239
- # スレッドが終了するまでここ実行されない
240
-
241
- # thread.join により tkinter のイベントループは停止中
242
-
243
-
244
-
245
- logging.debug("root destroy")
246
-
247
- root.destroy()
248
-
249
-
250
-
251
- root.protocol("WM_DELETE_WINDOW", close)
252
-
253
- root.mainloop()
254
-
255
-
256
-
257
-
258
-
259
- if __name__ == '__main__':
260
-
261
- main()
203
+ logging.basicConfig(
204
+
205
+ level=logging.DEBUG,
206
+
207
+ format="%(threadName)s %(message)s")
208
+
209
+
210
+
211
+ root = tk.Tk()
212
+
213
+ label = tk.Label(root)
214
+
215
+ label.pack()
216
+
217
+
218
+
219
+ thread = threading.Thread(target=worker)
220
+
221
+ thread.start()
222
+
223
+
224
+
225
+ def close():
226
+
227
+ global flag
228
+
229
+ flag = False
230
+
231
+
232
+
233
+ # スレッドの終了を待つブロッキング処理
234
+
235
+ logging.debug("thread join")
236
+
237
+ thread.join()
238
+
239
+
240
+
241
+ # スレッドが終了するまでここは実行されない
242
+
243
+ # thread.join により tkinter のイベントループ停止中
244
+
245
+
246
+
247
+ logging.debug("root destroy")
248
+
249
+ root.destroy()
250
+
251
+
252
+
253
+ root.protocol("WM_DELETE_WINDOW", close)
254
+
255
+ root.mainloop()
256
+
257
+
258
+
259
+
262
260
 
263
261
  ```
264
262