回答編集履歴
1
コード修正
answer
CHANGED
@@ -65,6 +65,8 @@
|
|
65
65
|
|
66
66
|
```python
|
67
67
|
|
68
|
+
|
69
|
+
|
68
70
|
import time
|
69
71
|
import threading
|
70
72
|
import tkinter as tk
|
@@ -72,7 +74,7 @@
|
|
72
74
|
|
73
75
|
flag = True
|
74
76
|
|
75
|
-
def
|
77
|
+
def worker():
|
76
78
|
global flag
|
77
79
|
for i in range(5):
|
78
80
|
logging.debug("before text")
|
@@ -97,38 +99,35 @@
|
|
97
99
|
if not flag:
|
98
100
|
break
|
99
101
|
|
100
|
-
def main():
|
101
|
-
|
102
|
+
logging.basicConfig(
|
102
|
-
|
103
|
+
level=logging.DEBUG,
|
103
|
-
|
104
|
+
format="%(threadName)s %(message)s")
|
104
105
|
|
105
|
-
|
106
|
+
root = tk.Tk()
|
106
|
-
|
107
|
+
label = tk.Label(root)
|
107
|
-
|
108
|
+
label.pack()
|
108
109
|
|
109
|
-
|
110
|
+
thread = threading.Thread(target=worker)
|
110
|
-
|
111
|
+
thread.start()
|
111
112
|
|
112
|
-
|
113
|
+
def close():
|
113
|
-
|
114
|
+
global flag
|
114
|
-
|
115
|
+
flag = False
|
115
116
|
|
116
|
-
|
117
|
+
# スレッドの終了を待つブロッキング処理
|
117
|
-
|
118
|
+
logging.debug("thread join")
|
118
|
-
|
119
|
+
thread.join()
|
119
120
|
|
120
|
-
|
121
|
+
# スレッドが終了するまでここは実行されない
|
121
|
-
|
122
|
+
# thread.join により tkinter のイベントループは停止中
|
122
123
|
|
123
|
-
|
124
|
+
logging.debug("root destroy")
|
124
|
-
|
125
|
+
root.destroy()
|
125
126
|
|
126
|
-
|
127
|
+
root.protocol("WM_DELETE_WINDOW", close)
|
127
|
-
|
128
|
+
root.mainloop()
|
128
129
|
|
129
130
|
|
130
|
-
if __name__ == '__main__':
|
131
|
-
main()
|
132
131
|
```
|
133
132
|
|
134
133
|
|