teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

8

追加

2020/10/26 05:23

投稿

person
person

スコア224

title CHANGED
File without changes
body CHANGED
@@ -60,12 +60,65 @@
60
60
 
61
61
  ![キャプチャ](d6fda319922d35ba0477df2bde0fc471.png)
62
62
 
63
- 表示文字列の設定はスレッドで、tkinterウィジェット操作はafterで処理するように変更
64
63
 
65
64
 
65
+ after()をサブスレッドから呼び出し(これもダメか・・・)
66
+
66
67
  ```Python
67
68
  import tkinter as tk
68
69
  import threading
70
+ import queue
71
+
72
+ def func1():
73
+ global flag, labels, win
74
+ cnt = 0
75
+ while flag:
76
+ txt = str(cnt)
77
+ win.after_idle(lambda:func2(txt))
78
+ cnt += 1
79
+
80
+ def func2(txt):
81
+ global labels
82
+ for i in range(150):
83
+ labels[i]["text"] = txt
84
+
85
+ def close():
86
+ global flag, thread, win
87
+ flag = False
88
+ thread.join()
89
+ win.destroy()
90
+
91
+ if __name__ == "__main__":
92
+
93
+ win = tk.Tk()
94
+
95
+ row = 15
96
+ column = 10
97
+ labels = []
98
+
99
+ for i in range(row):
100
+ for j in range(column):
101
+ label = tk.Label(win)
102
+ label.grid(row=i, column=j)
103
+ labels.append(label)
104
+
105
+ flag = True
106
+ thread = threading.Thread(target=func1)
107
+ thread.start()
108
+
109
+ win.protocol("WM_DELETE_WINDOW", close)
110
+ win.mainloop()
111
+ ```
112
+
113
+
114
+
115
+
116
+ afterの呼び出しをサブスレッドからメインスレッドに変更
117
+ (これは上手くいっている?)
118
+
119
+ ```Python
120
+ import tkinter as tk
121
+ import threading
69
122
  import time
70
123
  import queue
71
124
 

7

試験的なソースコードを変更

2020/10/26 05:23

投稿

person
person

スコア224

title CHANGED
File without changes
body CHANGED
@@ -60,95 +60,54 @@
60
60
 
61
61
  ![キャプチャ](d6fda319922d35ba0477df2bde0fc471.png)
62
62
 
63
- tkinterウィジェット操作をメインスレッドで処理するように変更
63
+ 表示文字列の設定はスレッドで、tkinterウィジェット操作はafterで処理するように変更
64
- うまく動作してはいない(test.csvの値を変えてもtkinterの表示が変わらない→キューによるデータの受け渡し部分が怪しい)
64
+
65
+
65
66
  ```Python
66
- import csv
67
- import os
68
- import queue
67
+ import tkinter as tk
69
68
  import threading
70
69
  import time
71
- import tkinter as tk
72
- import sys
70
+ import queue
73
71
 
74
- def main():
72
+ def loop():
73
+ global data, flag
75
- flag = True
74
+ cnt = 0
75
+ while flag:
76
- data = queue.Queue()
76
+ data.put(str(cnt))
77
+ cnt += 1
78
+ time.sleep(0.1)
77
79
 
80
+ def disp():
81
+ global data, flag, label1, label2
82
+ while not data.empty():
78
- win = tk.Tk()
83
+ tmp = data.get()
79
- win.geometry("200x100")
84
+ label1["text"] = tmp
85
+ label2["text"] = tmp
86
+ win.after(10, disp) # 指定時間[ms]はloop()の更新時間[s]より短くしないと、while not data.empty()で瞬間的に処理されてしまう
80
87
 
81
- def loop():
88
+ def close():
82
- nonlocal flag, data
89
+ global flag, thread, win
83
- while flag:
84
- file = os.path.dirname(sys.argv[0]) + "/test.csv"
85
- tmp = []
86
- with open(file, "r", encoding="utf8") as rf:
87
- f = csv.reader(rf, lineterminator="\r\n")
88
- for row in f:
90
+ flag = False
89
- tmp.append(row[1])
91
+ thread.join()
90
- #print(tmp)
91
- data.put(tmp)
92
+ win.destroy()
92
-
93
- time.sleep(0.001)
94
93
 
95
- def close():
96
- nonlocal flag, thread
97
- flag = False
98
- thread.join()
99
- win.destroy()
100
94
 
101
- win.protocol("WM_DELETE_WINDOW", close)
95
+ win = tk.Tk()
102
96
 
103
- labels = []
104
- for i in range(4):
105
- for j in range(5):
106
- label = tk.Label(win)
97
+ data = queue.Queue()
107
- label.grid(row=i, column=j)
108
- labels.append(label)
109
98
 
110
- thread = threading.Thread(target=lambda:loop())
99
+ label1 = tk.Label(win)
111
- thread.start()
100
+ label1.grid()
101
+ label2 = tk.Label(win)
102
+ label2.grid()
112
103
 
113
- while True:
104
+ flag = True
114
- while not data.empty():
105
+ thread = threading.Thread(target=loop)
115
- tmp = data.get()
106
+ thread.start()
116
- print(tmp)
117
- for i in range(20):
118
- if not flag:
119
- return
120
- labels[i]["text"] = tmp[i]
121
- win.update_idletasks()
122
- else:
123
- win.update()
124
- time.sleep(0.001)
125
107
 
108
+ # winが存在する(破棄されていない)場合はdisp()を実行
109
+ win.after_idle(disp) # labelを破棄する予定はないので、winでafter設定
126
110
 
127
- if __name__ == '__main__':
111
+ win.protocol("WM_DELETE_WINDOW", close)
128
- main()
112
+ win.mainloop()
129
- ```
130
-
131
- test.csv
132
-
133
- ```csv
134
- 1,100
135
- 2,100
136
- 3,100
137
- 4,100
138
- 5,100
139
- 6,100
140
- 7,100
141
- 8,100
142
- 9,100
143
- 10,100
144
- 11,100
145
- 12,100
146
- 13,100
147
- 14,100
148
- 15,100
149
- 16,100
150
- 17,100
151
- 18,100
152
- 19,100
153
- 20,100
154
113
  ```

6

修正

2020/10/25 02:53

投稿

person
person

スコア224

title CHANGED
File without changes
body CHANGED
@@ -61,7 +61,7 @@
61
61
  ![キャプチャ](d6fda319922d35ba0477df2bde0fc471.png)
62
62
 
63
63
  tkinterウィジェット操作をメインスレッドで処理するように変更
64
- うまく動作してはいない)
64
+ うまく動作してはいない(test.csvの値を変えてもtkinterの表示が変わらない→キューによるデータの受け渡し部分が怪しい
65
65
  ```Python
66
66
  import csv
67
67
  import os
@@ -126,4 +126,29 @@
126
126
 
127
127
  if __name__ == '__main__':
128
128
  main()
129
+ ```
130
+
131
+ test.csv
132
+
133
+ ```csv
134
+ 1,100
135
+ 2,100
136
+ 3,100
137
+ 4,100
138
+ 5,100
139
+ 6,100
140
+ 7,100
141
+ 8,100
142
+ 9,100
143
+ 10,100
144
+ 11,100
145
+ 12,100
146
+ 13,100
147
+ 14,100
148
+ 15,100
149
+ 16,100
150
+ 17,100
151
+ 18,100
152
+ 19,100
153
+ 20,100
129
154
  ```

5

修正

2020/10/23 12:34

投稿

person
person

スコア224

title CHANGED
@@ -1,1 +1,1 @@
1
- いpPython3 Tkinter スレッドで応答なしになる
1
+ Python3 Tkinter スレッドで応答なしになる
body CHANGED
@@ -60,7 +60,8 @@
60
60
 
61
61
  ![キャプチャ](d6fda319922d35ba0477df2bde0fc471.png)
62
62
 
63
+ tkinterウィジェット操作をメインスレッドで処理するように変更
63
- 上手くいない
64
+ (うま動作してはいない
64
65
  ```Python
65
66
  import csv
66
67
  import os

4

追加

2020/10/23 12:32

投稿

person
person

スコア224

title CHANGED
@@ -1,1 +1,1 @@
1
- Python3 Tkinter スレッドで応答なしになる
1
+ いpPython3 Tkinter スレッドで応答なしになる
body CHANGED
@@ -58,4 +58,71 @@
58
58
  ウィンドウを閉じた後にスレッドの処理をしようとしてエラーになったり、
59
59
  VSCodeで終わっていないような振る舞い(添付画像)をします。
60
60
 
61
- ![キャプチャ](d6fda319922d35ba0477df2bde0fc471.png)
61
+ ![キャプチャ](d6fda319922d35ba0477df2bde0fc471.png)
62
+
63
+ 上手くいかない
64
+ ```Python
65
+ import csv
66
+ import os
67
+ import queue
68
+ import threading
69
+ import time
70
+ import tkinter as tk
71
+ import sys
72
+
73
+ def main():
74
+ flag = True
75
+ data = queue.Queue()
76
+
77
+ win = tk.Tk()
78
+ win.geometry("200x100")
79
+
80
+ def loop():
81
+ nonlocal flag, data
82
+ while flag:
83
+ file = os.path.dirname(sys.argv[0]) + "/test.csv"
84
+ tmp = []
85
+ with open(file, "r", encoding="utf8") as rf:
86
+ f = csv.reader(rf, lineterminator="\r\n")
87
+ for row in f:
88
+ tmp.append(row[1])
89
+ #print(tmp)
90
+ data.put(tmp)
91
+
92
+ time.sleep(0.001)
93
+
94
+ def close():
95
+ nonlocal flag, thread
96
+ flag = False
97
+ thread.join()
98
+ win.destroy()
99
+
100
+ win.protocol("WM_DELETE_WINDOW", close)
101
+
102
+ labels = []
103
+ for i in range(4):
104
+ for j in range(5):
105
+ label = tk.Label(win)
106
+ label.grid(row=i, column=j)
107
+ labels.append(label)
108
+
109
+ thread = threading.Thread(target=lambda:loop())
110
+ thread.start()
111
+
112
+ while True:
113
+ while not data.empty():
114
+ tmp = data.get()
115
+ print(tmp)
116
+ for i in range(20):
117
+ if not flag:
118
+ return
119
+ labels[i]["text"] = tmp[i]
120
+ win.update_idletasks()
121
+ else:
122
+ win.update()
123
+ time.sleep(0.001)
124
+
125
+
126
+ if __name__ == '__main__':
127
+ main()
128
+ ```

3

修正

2020/10/23 07:51

投稿

person
person

スコア224

title CHANGED
File without changes
body CHANGED
@@ -25,7 +25,7 @@
25
25
  cnt = 0
26
26
  while flag:
27
27
  for i in range(150):
28
- labels[i]["text"] = cnt
28
+ labels[i]["text"] = str(cnt)
29
29
  cnt += 1
30
30
  time.sleep(0.1)
31
31
 

2

追加

2020/10/23 05:53

投稿

person
person

スコア224

title CHANGED
File without changes
body CHANGED
@@ -52,4 +52,10 @@
52
52
 
53
53
  win.protocol("WM_DELETE_WINDOW", close)
54
54
  win.mainloop()
55
- ```
55
+ ```
56
+
57
+ thread.join()を消すと応答なしにはなりませんが、
58
+ ウィンドウを閉じた後にスレッドの処理をしようとしてエラーになったり、
59
+ VSCodeで終わっていないような振る舞い(添付画像)をします。
60
+
61
+ ![キャプチャ](d6fda319922d35ba0477df2bde0fc471.png)

1

追加

2020/10/22 17:49

投稿

person
person

スコア224

title CHANGED
File without changes
body CHANGED
@@ -7,7 +7,11 @@
7
7
 
8
8
  time.sleep()の値を増やしたら少しは軽くなりましたが、
9
9
  この値はあまり増やしたくはありません。
10
+ (スレッドをもっと生成して、処理を分散させるべき?)
10
11
 
12
+ 対処方法はありますか?
13
+
14
+
11
15
  Windowsです。
12
16
 
13
17
  ```Python