質問編集履歴

3

進展のため

2021/07/14 03:17

投稿

redraccoon3
redraccoon3

スコア1

test CHANGED
File without changes
test CHANGED
@@ -8,10 +8,12 @@
8
8
 
9
9
  新たにsub_window()で定義したウィンドウでarduinoから受け取ったセンサーデータのリアルタイム表示を行いたいのですが
10
10
 
11
- def sub_window内のabout_windowを変数宣言きずアトリビュートエラーになっりま
11
+ def sub_window内で宣言されているもがやはりdef plot_data()内必要とされいるのですが、解決方法が分かりません
12
12
 
13
13
  関数内変数を他の関数に直接橋渡しして動作できるならそれでも良いのですが
14
14
 
15
+ 必要と要求されている変数:lines,fig,spec,ax0,ax1,canvas
16
+
15
17
 
16
18
 
17
19
  ### 発生している問題・エラーメッセージ
@@ -32,29 +34,11 @@
32
34
 
33
35
  func(*args)
34
36
 
35
- File "C:\Users\bakan\testes04.py", line 70, in plot_data
37
+ File "C:\Users\bakan\testes04.py", line 67, in plot_data
36
-
38
+
37
- y = np.delete(y, 0)
39
+ lines.set_xdata(t)
38
-
40
+
39
- AttributeError: 'list' object has no attribute 'after'
41
+ NameError: name 'lines' is not defined
40
-
41
- Exception in Tkinter callback
42
-
43
- Traceback (most recent call last):
44
-
45
- File "C:\Users\bakan\anaconda3\lib\tkinter\__init__.py", line 1705, in __call__
46
-
47
- return self.func(*args)
48
-
49
- File "C:\Users\bakan\anaconda3\lib\tkinter\__init__.py", line 749, in callit
50
-
51
- func(*args)
52
-
53
- File "C:\Users\bakan\testes04.py", line 70, in plot_data
54
-
55
- y = np.delete(y, 0)
56
-
57
- AttributeError: 'list' object has no attribute 'after'
58
42
 
59
43
  ```
60
44
 
@@ -328,7 +312,11 @@
328
312
 
329
313
 
330
314
 
315
+ メイン(関数宣言よりも下のserialポート開けと最初のウィンドウ生成のための場所)に
316
+
331
- sub_window内のabout_windowを現状about_window = []で初期化&宣言していすがそれでエラーが出ていま
317
+ 必要な変数をsub_windowと同じように宣言していたのですが、代わりに不必要なウィンドウが出てきてしまいました
318
+
319
+
332
320
 
333
321
 
334
322
 

2

進展したため

2021/07/14 03:17

投稿

redraccoon3
redraccoon3

スコア1

test CHANGED
File without changes
test CHANGED
@@ -8,7 +8,7 @@
8
8
 
9
9
  新たにsub_window()で定義したウィンドウでarduinoから受け取ったセンサーデータのリアルタイム表示を行いたいのですが
10
10
 
11
- def sub_windowとdef data_plot再起エラー起こしい、正常に動作しません。
11
+ def sub_window内のabout_windowを変数宣言きずアトリビュートエラーになっおり
12
12
 
13
13
  関数内変数を他の関数に直接橋渡しして動作できるならそれでも良いのですが
14
14
 
@@ -28,313 +28,307 @@
28
28
 
29
29
  return self.func(*args)
30
30
 
31
- File "C:\Users\bakan\testes04.py", line 55, in sub_window
32
-
33
- about_window.after(1,plot_data())
34
-
35
- File "C:\Users\bakan\testes04.py", line 81, in plot_data
36
-
37
- about_window.after(1,plot_data())
38
-
39
- File "C:\Users\bakan\testes04.py", line 81, in plot_data
40
-
41
- about_window.after(1,plot_data())
42
-
43
- File "C:\Users\bakan\testes04.py", line 81, in plot_data
44
-
45
- about_window.after(1,plot_data())
46
-
47
- [Previous line repeated 2948 more times]
48
-
49
- File "C:\Users\bakan\testes04.py", line 62, in plot_data
31
+ File "C:\Users\bakan\anaconda3\lib\tkinter\__init__.py", line 749, in callit
32
+
33
+ func(*args)
34
+
35
+ File "C:\Users\bakan\testes04.py", line 70, in plot_data
36
+
37
+ y = np.delete(y, 0)
38
+
39
+ AttributeError: 'list' object has no attribute 'after'
40
+
41
+ Exception in Tkinter callback
42
+
43
+ Traceback (most recent call last):
44
+
45
+ File "C:\Users\bakan\anaconda3\lib\tkinter\__init__.py", line 1705, in __call__
46
+
47
+ return self.func(*args)
48
+
49
+ File "C:\Users\bakan\anaconda3\lib\tkinter\__init__.py", line 749, in callit
50
+
51
+ func(*args)
52
+
53
+ File "C:\Users\bakan\testes04.py", line 70, in plot_data
54
+
55
+ y = np.delete(y, 0)
56
+
57
+ AttributeError: 'list' object has no attribute 'after'
58
+
59
+ ```
60
+
61
+
62
+
63
+ ### 該当のソースコード
64
+
65
+
66
+
67
+ ```python
68
+
69
+ import tkinter as tk
70
+
71
+ from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
72
+
73
+ from matplotlib.figure import Figure
74
+
75
+ import serial
76
+
77
+ import numpy as np
78
+
79
+ from matplotlib import gridspec
80
+
81
+ from matplotlib.animation import FuncAnimation
82
+
83
+
84
+
85
+ def sub_window():
86
+
87
+ global about_window
88
+
89
+ # サブウインドウの作成
90
+
91
+ about_window = tk.Toplevel(root)
92
+
93
+ about_window.title("each mode")
94
+
95
+ about_window.configure(background = "light blue")
96
+
97
+ about_window.geometry("700x600")
98
+
99
+
100
+
101
+ fig = Figure()
102
+
103
+ spec = gridspec.GridSpec(ncols=2, nrows=1,
104
+
105
+ width_ratios=[1, 3])
106
+
107
+ about_window.update();
108
+
109
+ ax0 = fig.add_subplot(spec[0])
110
+
111
+ ax1 = fig.add_subplot(spec[1])
112
+
113
+ lines = ax1.plot([], [])[0]
114
+
115
+ ax0.set_ylim([-1.1, 1.1])
116
+
117
+ ax0.axis('off')
118
+
119
+ ax1.set_ylim([0, 25])
120
+
121
+ ax1.set_xlabel("Time[s]")
122
+
123
+ ax1.set_ylabel("Extension amount[mm]")
124
+
125
+ ax1.set_xlim([max(t)-7, max(t)+50])
126
+
127
+
128
+
129
+ canvas = FigureCanvasTkAgg(fig,master=about_window)
130
+
131
+ canvas.get_tk_widget().place(x = 10,y = 10,width = 500,height = 400)
132
+
133
+ canvas.draw()
134
+
135
+
136
+
137
+ about_window.update();
138
+
139
+ run = tk.Button(about_window, text = "Run",font = ("calbiri",12), command = lambda:plot_start())
140
+
141
+ run.place(x = 100, y = 520)
142
+
143
+
144
+
145
+ about_window.update();
146
+
147
+ stop = tk.Button(about_window, text = "stop",font = ("calbiri",12), command = lambda:plot_stop())
148
+
149
+ stop.place(x = run.winfo_x()+run.winfo_reqwidth()+20, y = 520)
150
+
151
+
152
+
153
+ end = tk.Button(about_window, text = "exit",font = ("calbiri",12), command = quit_yn)
154
+
155
+ end.place(x = stop.winfo_x()+stop.winfo_reqwidth()+run.winfo_x()+run.winfo_reqwidth()+40, y = 520)
156
+
157
+ #anim = FuncAnimation(fig, update, frames=range(250), interval=1)
158
+
159
+ about_window.after(1,plot_data)
160
+
161
+ about_window.mainloop()
162
+
163
+
164
+
165
+ def plot_data():
166
+
167
+ global cond, data,t,y,tInt,lines,canvas,ser,ax1,root,about_window
168
+
169
+
50
170
 
51
171
  if (cond == True):
52
172
 
173
+ ax1.set_xlim([max(t)-7, max(t)+0.5])
174
+
175
+
176
+
177
+ ser.write("*".encode())
178
+
179
+ data = ser.readline().strip().rsplit()
180
+
181
+ t = np.append(t, (float(data[0])-tInt)/10**6)
182
+
183
+ t = np.delete(t, 0)
184
+
185
+ y = np.append(y, float(data[1])*5/1023*22.2-31.7)
186
+
187
+ y = np.delete(y, 0)
188
+
189
+ sinx = np.sin(82.0 * np.pi * (max(t)) /100)
190
+
191
+ ax0.plot(0, sinx, marker='.', markersize=40)
192
+
193
+ ax0.set_ylim([-1.1, 1.1])
194
+
195
+ ax0.axis('off')
196
+
197
+ lines.set_xdata(t)
198
+
199
+ lines.set_ydata(y)
200
+
201
+
202
+
203
+ canvas.draw()
204
+
205
+ ax0.cla()
206
+
207
+
208
+
209
+ about_window.after(1,plot_data)
210
+
211
+
212
+
213
+ def plot_start():
214
+
215
+ global cond
216
+
217
+ cond = True
218
+
219
+ ser.reset_input_buffer()
220
+
221
+
222
+
223
+ def plot_stop():
224
+
225
+ global cond
226
+
227
+ cond = False
228
+
229
+
230
+
231
+ def update(frame):
232
+
233
+ global ax0
234
+
235
+ if 0 <=frame < 50:
236
+
237
+ sinx = frame/50+0.00001
238
+
239
+ elif 50 <= frame < 100:
240
+
241
+ sinx = 1
242
+
243
+ elif 100 <= frame <200:
244
+
245
+ sinx = 1 - (frame-100)/100+0.00001
246
+
247
+ elif 200 <= frame <250:
248
+
249
+ sinx = 0
250
+
251
+ ax0.cla()
252
+
253
+ ax0.set_ylim([-0.1, 1.1])
254
+
255
+ ax0.axis('off')
256
+
257
+ ax0.plot(0,sinx,marker='.', markersize=40)
258
+
259
+
260
+
261
+ def quit_yn():
262
+
263
+ if tk.messagebox.askyesno("確認", "終了しますか?"):
264
+
265
+ root.destroy()
266
+
267
+
268
+
269
+ data = np.array([])
270
+
271
+ cond = False
272
+
273
+ t = np.zeros(200)
274
+
275
+ y = np.zeros(200)
276
+
277
+ about_window = []
278
+
279
+
280
+
281
+ ser = serial.Serial()
282
+
283
+ ser.baudrate = 9600
284
+
285
+ ser.port = 'COM3'
286
+
287
+ ser.open()
288
+
289
+ ser.reset_input_buffer()
290
+
291
+
292
+
293
+ ser.write("*".encode())
294
+
295
+ data = ser.readline().strip().rsplit()
296
+
297
+ tInt = float(data[0])
298
+
299
+
300
+
301
+ root = tk.Tk()
302
+
303
+ root.title("RTP")
304
+
53
- RecursionError: maximum recursion depth exceeded in comparison
305
+ root.configure(background = "light blue")
306
+
307
+ root.geometry("200x60")
308
+
309
+
310
+
311
+ end = tk.Button(root, text = "exit",font = ("calbiri",12), command = quit_yn)
312
+
313
+ end.place(x = 10, y = 10)
314
+
315
+
316
+
317
+ push_button = tk.Button(root, text = "Push",font = ("calbiri",12), command = sub_window)
318
+
319
+ push_button.place(x = 60, y = 10)
320
+
321
+ root.mainloop()
54
322
 
55
323
  ```
56
324
 
57
325
 
58
326
 
59
- ### 該当のソースコード
60
-
61
-
62
-
63
- ```python
64
-
65
- import tkinter as tk
66
-
67
- from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
68
-
69
- from matplotlib.figure import Figure
70
-
71
- import serial
72
-
73
- import numpy as np
74
-
75
- from matplotlib import gridspec
76
-
77
- from matplotlib.animation import FuncAnimation
78
-
79
-
80
-
81
-
82
-
83
- def sub_window():
84
-
85
- global about_window
86
-
87
- # サブウインドウの作成
88
-
89
- about_window = tk.Toplevel(root)
90
-
91
- about_window.title("each mode")
92
-
93
- about_window.configure(background = "light blue")
94
-
95
- about_window.geometry("700x600")
96
-
97
-
98
-
99
- fig = Figure()
100
-
101
- spec = gridspec.GridSpec(ncols=2, nrows=1,
102
-
103
- width_ratios=[1, 3])
104
-
105
- about_window.update();
106
-
107
- ax0 = fig.add_subplot(spec[0])
108
-
109
- ax1 = fig.add_subplot(spec[1])
110
-
111
- lines = ax1.plot([], [])[0]
112
-
113
- ax0.set_ylim([-1.1, 1.1])
114
-
115
- ax0.axis('off')
116
-
117
- ax1.set_ylim([0, 25])
118
-
119
- ax1.set_xlabel("Time[s]")
120
-
121
- ax1.set_ylabel("Extension amount[mm]")
122
-
123
- ax1.set_xlim([max(t)-7, max(t)+50])
124
-
125
-
126
-
127
- canvas = FigureCanvasTkAgg(fig,master=about_window)
128
-
129
- canvas.get_tk_widget().place(x = 10,y = 10,width = 500,height = 400)
130
-
131
- canvas.draw()
132
-
133
-
134
-
135
- about_window.update();
136
-
137
- run = tk.Button(about_window, text = "Run",font = ("calbiri",12), command = lambda:plot_start())
138
-
139
- run.place(x = 100, y = 520)
140
-
141
-
142
-
143
- about_window.update();
144
-
145
- stop = tk.Button(about_window, text = "stop",font = ("calbiri",12), command = lambda:plot_stop())
146
-
147
- stop.place(x = run.winfo_x()+run.winfo_reqwidth()+20, y = 520)
148
-
149
-
150
-
151
- end = tk.Button(about_window, text = "exit",font = ("calbiri",12), command = quit_yn)
152
-
153
- end.place(x = stop.winfo_x()+stop.winfo_reqwidth()+run.winfo_x()+run.winfo_reqwidth()+40, y = 520)
154
-
155
- #anim = FuncAnimation(fig, update, frames=range(250), interval=1)
156
-
157
- about_window.after(1,plot_data())
158
-
159
- about_window.mainloop()
160
-
161
-
162
-
163
-
164
-
165
- def plot_data():
166
-
167
- global cond, data,t,y,tInt,lines,canvas,ser,ax1,root,about_window
168
-
169
-
170
-
171
- if (cond == True):
172
-
173
- ax1.set_xlim([max(t)-7, max(t)+0.5])
174
-
175
-
176
-
177
- ser.write("*".encode())
178
-
179
- data = ser.readline().strip().rsplit()
180
-
181
- t = np.append(t, (float(data[0])-tInt)/10**6)
182
-
183
- t = np.delete(t, 0)
184
-
185
- y = np.append(y, float(data[1])*5/1023*22.2-31.7)
186
-
187
- y = np.delete(y, 0)
188
-
189
- sinx = np.sin(82.0 * np.pi * (max(t)) /100)
190
-
191
- ax0.plot(0, sinx, marker='.', markersize=40)
192
-
193
- ax0.set_ylim([-1.1, 1.1])
194
-
195
- ax0.axis('off')
196
-
197
- lines.set_xdata(t)
198
-
199
- lines.set_ydata(y)
200
-
201
-
202
-
203
- canvas.draw()
204
-
205
- ax0.cla()
206
-
207
-
208
-
209
- about_window.after(1,plot_data())
210
-
211
-
212
-
213
- def plot_start():
214
-
215
- global cond
216
-
217
- cond = True
218
-
219
- ser.reset_input_buffer()
220
-
221
-
222
-
223
- def plot_stop():
224
-
225
- global cond
226
-
227
- cond = False
228
-
229
-
230
-
231
- def update(frame):
232
-
233
- global ax0
234
-
235
- if 0 <=frame < 50:
236
-
237
- sinx = frame/50+0.00001
238
-
239
- elif 50 <= frame < 100:
240
-
241
- sinx = 1
242
-
243
- elif 100 <= frame <200:
244
-
245
- sinx = 1 - (frame-100)/100+0.00001
246
-
247
- elif 200 <= frame <250:
248
-
249
- sinx = 0
250
-
251
- ax0.cla()
252
-
253
- ax0.set_ylim([-0.1, 1.1])
254
-
255
- ax0.axis('off')
256
-
257
- ax0.plot(0,sinx,marker='.', markersize=40)
258
-
259
-
260
-
261
- def quit_yn():
262
-
263
- if tk.messagebox.askyesno("確認", "終了しますか?"):
264
-
265
- root.destroy()
266
-
267
-
268
-
269
-
270
-
271
-
272
-
273
- data = np.array([])
274
-
275
- cond = False
276
-
277
- t = np.zeros(200)
278
-
279
- y = np.zeros(200)
280
-
281
- about_window = []
282
-
283
-
284
-
285
-
286
-
287
- ser = serial.Serial()
288
-
289
- ser.baudrate = 9600
290
-
291
- ser.port = 'COM3'
292
-
293
- ser.open()
294
-
295
- ser.reset_input_buffer()
296
-
297
-
298
-
299
- ser.write("*".encode())
300
-
301
- data = ser.readline().strip().rsplit()
302
-
303
- tInt = float(data[0])
304
-
305
-
306
-
307
- root = tk.Tk()
308
-
309
- root.title("RTP")
310
-
311
- root.configure(background = "light blue")
312
-
313
- root.geometry("200x60")
314
-
315
-
316
-
317
- end = tk.Button(root, text = "exit",font = ("calbiri",12), command = quit_yn)
318
-
319
- end.place(x = 10, y = 10)
320
-
321
-
322
-
323
- push_button = tk.Button(root, text = "Push",font = ("calbiri",12), command = sub_window)
324
-
325
- push_button.place(x = 60, y = 10)
326
-
327
- root.mainloop()
328
-
329
- ```
330
-
331
-
332
-
333
327
  ### 試したこと
334
328
 
335
329
 
336
330
 
337
- sub_window内にplot_dataを作成するなど関数ネストなどにはしました同じく再起エラーが発生ししまいました
331
+ sub_window内のabout_windowを現状about_window = []で初期化&宣言ていそれでエラーがていま
338
332
 
339
333
 
340
334
 

1

誤字修正

2021/07/14 01:49

投稿

redraccoon3
redraccoon3

スコア1

test CHANGED
File without changes
test CHANGED
@@ -4,9 +4,7 @@
4
4
 
5
5
  実現したいこと:arduinoに刺してあるセンサーのリアルタイムプロットのサブウィンドウ化
6
6
 
7
- 下に書いてあるソースコード
7
+
8
-
9
- ここに質問の内容を詳しく書いてください。
10
8
 
11
9
  新たにsub_window()で定義したウィンドウでarduinoから受け取ったセンサーデータのリアルタイム表示を行いたいのですが
12
10