質問編集履歴
8
追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -122,9 +122,11 @@
|
|
122
122
|
|
123
123
|
|
124
124
|
|
125
|
+
|
126
|
+
|
127
|
+
|
128
|
+
|
125
|
-
|
129
|
+
after()をサブスレッドから呼び出し(これもダメか・・・)
|
126
|
-
|
127
|
-
|
128
130
|
|
129
131
|
|
130
132
|
|
@@ -134,6 +136,110 @@
|
|
134
136
|
|
135
137
|
import threading
|
136
138
|
|
139
|
+
import queue
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
def func1():
|
144
|
+
|
145
|
+
global flag, labels, win
|
146
|
+
|
147
|
+
cnt = 0
|
148
|
+
|
149
|
+
while flag:
|
150
|
+
|
151
|
+
txt = str(cnt)
|
152
|
+
|
153
|
+
win.after_idle(lambda:func2(txt))
|
154
|
+
|
155
|
+
cnt += 1
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
def func2(txt):
|
160
|
+
|
161
|
+
global labels
|
162
|
+
|
163
|
+
for i in range(150):
|
164
|
+
|
165
|
+
labels[i]["text"] = txt
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
def close():
|
170
|
+
|
171
|
+
global flag, thread, win
|
172
|
+
|
173
|
+
flag = False
|
174
|
+
|
175
|
+
thread.join()
|
176
|
+
|
177
|
+
win.destroy()
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
if __name__ == "__main__":
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
win = tk.Tk()
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
row = 15
|
190
|
+
|
191
|
+
column = 10
|
192
|
+
|
193
|
+
labels = []
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
for i in range(row):
|
198
|
+
|
199
|
+
for j in range(column):
|
200
|
+
|
201
|
+
label = tk.Label(win)
|
202
|
+
|
203
|
+
label.grid(row=i, column=j)
|
204
|
+
|
205
|
+
labels.append(label)
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
flag = True
|
210
|
+
|
211
|
+
thread = threading.Thread(target=func1)
|
212
|
+
|
213
|
+
thread.start()
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
win.protocol("WM_DELETE_WINDOW", close)
|
218
|
+
|
219
|
+
win.mainloop()
|
220
|
+
|
221
|
+
```
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
afterの呼び出しをサブスレッドからメインスレッドに変更
|
232
|
+
|
233
|
+
(これは上手くいっている?)
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
```Python
|
238
|
+
|
239
|
+
import tkinter as tk
|
240
|
+
|
241
|
+
import threading
|
242
|
+
|
137
243
|
import time
|
138
244
|
|
139
245
|
import queue
|
7
試験的なソースコードを変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -122,186 +122,104 @@
|
|
122
122
|
|
123
123
|
|
124
124
|
|
125
|
-
tkinterウィジェット操作
|
125
|
+
表示文字列の設定はスレッドで、tkinterウィジェット操作はafterで処理するように変更
|
126
|
-
|
127
|
-
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
|
128
130
|
|
129
131
|
```Python
|
130
132
|
|
133
|
+
import tkinter as tk
|
134
|
+
|
135
|
+
import threading
|
136
|
+
|
131
|
-
import
|
137
|
+
import time
|
132
|
-
|
133
|
-
import os
|
134
138
|
|
135
139
|
import queue
|
136
140
|
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
d
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
de
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
thread.start()
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
while True:
|
226
|
-
|
227
|
-
while not data.empty():
|
228
|
-
|
229
|
-
tmp = data.get()
|
230
|
-
|
231
|
-
print(tmp)
|
232
|
-
|
233
|
-
for i in range(20):
|
234
|
-
|
235
|
-
if not flag:
|
236
|
-
|
237
|
-
return
|
238
|
-
|
239
|
-
labels[i]["text"] = tmp[i]
|
240
|
-
|
241
|
-
win.update_idletasks()
|
242
|
-
|
243
|
-
else:
|
244
|
-
|
245
|
-
win.update()
|
246
|
-
|
247
|
-
time.sleep(0.001)
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
if __name__ == '__main__':
|
254
|
-
|
255
|
-
main()
|
141
|
+
|
142
|
+
|
143
|
+
def loop():
|
144
|
+
|
145
|
+
global data, flag
|
146
|
+
|
147
|
+
cnt = 0
|
148
|
+
|
149
|
+
while flag:
|
150
|
+
|
151
|
+
data.put(str(cnt))
|
152
|
+
|
153
|
+
cnt += 1
|
154
|
+
|
155
|
+
time.sleep(0.1)
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
def disp():
|
160
|
+
|
161
|
+
global data, flag, label1, label2
|
162
|
+
|
163
|
+
while not data.empty():
|
164
|
+
|
165
|
+
tmp = data.get()
|
166
|
+
|
167
|
+
label1["text"] = tmp
|
168
|
+
|
169
|
+
label2["text"] = tmp
|
170
|
+
|
171
|
+
win.after(10, disp) # 指定時間[ms]はloop()の更新時間[s]より短くしないと、while not data.empty()で瞬間的に処理されてしまう
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
def close():
|
176
|
+
|
177
|
+
global flag, thread, win
|
178
|
+
|
179
|
+
flag = False
|
180
|
+
|
181
|
+
thread.join()
|
182
|
+
|
183
|
+
win.destroy()
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
win = tk.Tk()
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
data = queue.Queue()
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
label1 = tk.Label(win)
|
198
|
+
|
199
|
+
label1.grid()
|
200
|
+
|
201
|
+
label2 = tk.Label(win)
|
202
|
+
|
203
|
+
label2.grid()
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
flag = True
|
208
|
+
|
209
|
+
thread = threading.Thread(target=loop)
|
210
|
+
|
211
|
+
thread.start()
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
# winが存在する(破棄されていない)場合はdisp()を実行
|
216
|
+
|
217
|
+
win.after_idle(disp) # labelを破棄する予定はないので、winでafter設定
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
win.protocol("WM_DELETE_WINDOW", close)
|
222
|
+
|
223
|
+
win.mainloop()
|
256
224
|
|
257
225
|
```
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
test.csv
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
```csv
|
266
|
-
|
267
|
-
1,100
|
268
|
-
|
269
|
-
2,100
|
270
|
-
|
271
|
-
3,100
|
272
|
-
|
273
|
-
4,100
|
274
|
-
|
275
|
-
5,100
|
276
|
-
|
277
|
-
6,100
|
278
|
-
|
279
|
-
7,100
|
280
|
-
|
281
|
-
8,100
|
282
|
-
|
283
|
-
9,100
|
284
|
-
|
285
|
-
10,100
|
286
|
-
|
287
|
-
11,100
|
288
|
-
|
289
|
-
12,100
|
290
|
-
|
291
|
-
13,100
|
292
|
-
|
293
|
-
14,100
|
294
|
-
|
295
|
-
15,100
|
296
|
-
|
297
|
-
16,100
|
298
|
-
|
299
|
-
17,100
|
300
|
-
|
301
|
-
18,100
|
302
|
-
|
303
|
-
19,100
|
304
|
-
|
305
|
-
20,100
|
306
|
-
|
307
|
-
```
|
6
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -124,7 +124,7 @@
|
|
124
124
|
|
125
125
|
tkinterウィジェット操作をメインスレッドで処理するように変更
|
126
126
|
|
127
|
-
|
127
|
+
うまく動作してはいない(test.csvの値を変えてもtkinterの表示が変わらない→キューによるデータの受け渡し部分が怪しい)
|
128
128
|
|
129
129
|
```Python
|
130
130
|
|
@@ -255,3 +255,53 @@
|
|
255
255
|
main()
|
256
256
|
|
257
257
|
```
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
test.csv
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
```csv
|
266
|
+
|
267
|
+
1,100
|
268
|
+
|
269
|
+
2,100
|
270
|
+
|
271
|
+
3,100
|
272
|
+
|
273
|
+
4,100
|
274
|
+
|
275
|
+
5,100
|
276
|
+
|
277
|
+
6,100
|
278
|
+
|
279
|
+
7,100
|
280
|
+
|
281
|
+
8,100
|
282
|
+
|
283
|
+
9,100
|
284
|
+
|
285
|
+
10,100
|
286
|
+
|
287
|
+
11,100
|
288
|
+
|
289
|
+
12,100
|
290
|
+
|
291
|
+
13,100
|
292
|
+
|
293
|
+
14,100
|
294
|
+
|
295
|
+
15,100
|
296
|
+
|
297
|
+
16,100
|
298
|
+
|
299
|
+
17,100
|
300
|
+
|
301
|
+
18,100
|
302
|
+
|
303
|
+
19,100
|
304
|
+
|
305
|
+
20,100
|
306
|
+
|
307
|
+
```
|
5
修正
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
Python3 Tkinter スレッドで応答なしになる
|
test
CHANGED
@@ -122,7 +122,9 @@
|
|
122
122
|
|
123
123
|
|
124
124
|
|
125
|
+
tkinterウィジェット操作をメインスレッドで処理するように変更
|
126
|
+
|
125
|
-
|
127
|
+
(うまく動作してはいない)
|
126
128
|
|
127
129
|
```Python
|
128
130
|
|
4
追加
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
Python3 Tkinter スレッドで応答なしになる
|
1
|
+
いpPython3 Tkinter スレッドで応答なしになる
|
test
CHANGED
@@ -119,3 +119,137 @@
|
|
119
119
|
|
120
120
|
|
121
121
|
![キャプチャ](d6fda319922d35ba0477df2bde0fc471.png)
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
上手くいかない
|
126
|
+
|
127
|
+
```Python
|
128
|
+
|
129
|
+
import csv
|
130
|
+
|
131
|
+
import os
|
132
|
+
|
133
|
+
import queue
|
134
|
+
|
135
|
+
import threading
|
136
|
+
|
137
|
+
import time
|
138
|
+
|
139
|
+
import tkinter as tk
|
140
|
+
|
141
|
+
import sys
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
def main():
|
146
|
+
|
147
|
+
flag = True
|
148
|
+
|
149
|
+
data = queue.Queue()
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
win = tk.Tk()
|
154
|
+
|
155
|
+
win.geometry("200x100")
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
def loop():
|
160
|
+
|
161
|
+
nonlocal flag, data
|
162
|
+
|
163
|
+
while flag:
|
164
|
+
|
165
|
+
file = os.path.dirname(sys.argv[0]) + "/test.csv"
|
166
|
+
|
167
|
+
tmp = []
|
168
|
+
|
169
|
+
with open(file, "r", encoding="utf8") as rf:
|
170
|
+
|
171
|
+
f = csv.reader(rf, lineterminator="\r\n")
|
172
|
+
|
173
|
+
for row in f:
|
174
|
+
|
175
|
+
tmp.append(row[1])
|
176
|
+
|
177
|
+
#print(tmp)
|
178
|
+
|
179
|
+
data.put(tmp)
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
time.sleep(0.001)
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
def close():
|
188
|
+
|
189
|
+
nonlocal flag, thread
|
190
|
+
|
191
|
+
flag = False
|
192
|
+
|
193
|
+
thread.join()
|
194
|
+
|
195
|
+
win.destroy()
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
win.protocol("WM_DELETE_WINDOW", close)
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
labels = []
|
204
|
+
|
205
|
+
for i in range(4):
|
206
|
+
|
207
|
+
for j in range(5):
|
208
|
+
|
209
|
+
label = tk.Label(win)
|
210
|
+
|
211
|
+
label.grid(row=i, column=j)
|
212
|
+
|
213
|
+
labels.append(label)
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
thread = threading.Thread(target=lambda:loop())
|
218
|
+
|
219
|
+
thread.start()
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
while True:
|
224
|
+
|
225
|
+
while not data.empty():
|
226
|
+
|
227
|
+
tmp = data.get()
|
228
|
+
|
229
|
+
print(tmp)
|
230
|
+
|
231
|
+
for i in range(20):
|
232
|
+
|
233
|
+
if not flag:
|
234
|
+
|
235
|
+
return
|
236
|
+
|
237
|
+
labels[i]["text"] = tmp[i]
|
238
|
+
|
239
|
+
win.update_idletasks()
|
240
|
+
|
241
|
+
else:
|
242
|
+
|
243
|
+
win.update()
|
244
|
+
|
245
|
+
time.sleep(0.001)
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
if __name__ == '__main__':
|
252
|
+
|
253
|
+
main()
|
254
|
+
|
255
|
+
```
|
3
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -52,7 +52,7 @@
|
|
52
52
|
|
53
53
|
for i in range(150):
|
54
54
|
|
55
|
-
labels[i]["text"] = cnt
|
55
|
+
labels[i]["text"] = str(cnt)
|
56
56
|
|
57
57
|
cnt += 1
|
58
58
|
|
2
追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -107,3 +107,15 @@
|
|
107
107
|
win.mainloop()
|
108
108
|
|
109
109
|
```
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
thread.join()を消すと応答なしにはなりませんが、
|
114
|
+
|
115
|
+
ウィンドウを閉じた後にスレッドの処理をしようとしてエラーになったり、
|
116
|
+
|
117
|
+
VSCodeで終わっていないような振る舞い(添付画像)をします。
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
![キャプチャ](d6fda319922d35ba0477df2bde0fc471.png)
|
1
追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -15,6 +15,14 @@
|
|
15
15
|
time.sleep()の値を増やしたら少しは軽くなりましたが、
|
16
16
|
|
17
17
|
この値はあまり増やしたくはありません。
|
18
|
+
|
19
|
+
(スレッドをもっと生成して、処理を分散させるべき?)
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
対処方法はありますか?
|
24
|
+
|
25
|
+
|
18
26
|
|
19
27
|
|
20
28
|
|