質問編集履歴

4

詰まった際のコードに変更

2020/08/13 15:18

投稿

chanpoo
chanpoo

スコア3

test CHANGED
File without changes
test CHANGED
@@ -14,9 +14,11 @@
14
14
 
15
15
  ```python
16
16
 
17
+ import tkinter as tk
18
+
19
+ import tkinter.ttk as ttk
20
+
17
21
  import pyaudio
18
-
19
- import tkinter as tk
20
22
 
21
23
  import matplotlib.pyplot as plt
22
24
 
@@ -26,53 +28,29 @@
26
28
 
27
29
  from matplotlib.animation import FuncAnimation
28
30
 
31
+
32
+
33
+ def click():
34
+
29
- import threading
35
+ P = pyaudio.PyAudio()
36
+
37
+ stream = P.open(format=pyaudio.paInt16, channels=1, rate=44100, frames_per_buffer=8820, input=True, output=False)
38
+
39
+ ani = FuncAnimation(fig,update)
30
40
 
31
41
 
32
42
 
33
- def start():
43
+ def update(Z):
34
44
 
35
- global flag
45
+ input = stream.read(8820, exception_on_overflow=False)
36
46
 
37
- flag = True
47
+ ndarray = np.frombuffer(input, dtype='int16')
38
48
 
39
- while stream.is_active():
49
+ ax.cla()
40
50
 
41
- input = stream.read(8820, exception_on_overflow=False)
51
+ ax.plot(ndarray)
42
52
 
43
- ndarray = np.frombuffer(input, dtype='int16')
44
-
45
- ax.cla()
46
-
47
- ax.plot(ndarray)
48
-
49
- canvas.draw()
50
-
51
- root.after(200)
53
+ frame.after(200)
52
-
53
- if flag == False:
54
-
55
- break
56
-
57
- def click():
58
-
59
- thread = threading.Thread(target=start)
60
-
61
- thread.start()
62
-
63
- def stop():
64
-
65
- global flag
66
-
67
- flag = False
68
-
69
- root.after(200)
70
-
71
- stream.stop_stream()
72
-
73
- stream.close()
74
-
75
- P.terminate()
76
54
 
77
55
 
78
56
 
@@ -80,7 +58,7 @@
80
58
 
81
59
  root.title("abc")
82
60
 
83
- root.geometry("800x700") ##### guiの用意
61
+ root.geometry("800x600") ##### guiの用意
84
62
 
85
63
 
86
64
 
@@ -106,21 +84,11 @@
106
84
 
107
85
 
108
86
 
109
- start = tk.Button(root, text='Start', command=click)
87
+ button = tk.Button(root, text='Start', command=click)
110
-
111
- start.grid(row=6, column=10, columnspan=1, rowspan=1, padx=10, pady=10)
112
88
 
113
89
 
114
90
 
115
- stop = tk.Button(root, text='Stop', command=stop)
116
-
117
- stop.grid(row=7, column=10, columnspan=1, rowspan=1, padx=10, pady=10)
91
+ button.grid(row=6, column=11, columnspan=1, rowspan=1, padx=10, pady=10)
118
-
119
-
120
-
121
- P = pyaudio.PyAudio()
122
-
123
- stream = P.open(format=pyaudio.paInt16, channels=1, rate=44100, frames_per_buffer=8820, input=True, output=False)
124
92
 
125
93
 
126
94
 

3

アニメーションなしの場合に変更

2020/08/13 15:18

投稿

chanpoo
chanpoo

スコア3

test CHANGED
File without changes
test CHANGED
@@ -14,11 +14,9 @@
14
14
 
15
15
  ```python
16
16
 
17
+ import pyaudio
18
+
17
19
  import tkinter as tk
18
-
19
- import tkinter.ttk as ttk
20
-
21
- import pyaudio
22
20
 
23
21
  import matplotlib.pyplot as plt
24
22
 
@@ -28,31 +26,53 @@
28
26
 
29
27
  from matplotlib.animation import FuncAnimation
30
28
 
29
+ import threading
31
30
 
31
+
32
+
33
+ def start():
34
+
35
+ global flag
36
+
37
+ flag = True
38
+
39
+ while stream.is_active():
40
+
41
+ input = stream.read(8820, exception_on_overflow=False)
42
+
43
+ ndarray = np.frombuffer(input, dtype='int16')
44
+
45
+ ax.cla()
46
+
47
+ ax.plot(ndarray)
48
+
49
+ canvas.draw()
50
+
51
+ root.after(200)
52
+
53
+ if flag == False:
54
+
55
+ break
32
56
 
33
57
  def click():
34
58
 
35
- P = pyaudio.PyAudio()
59
+ thread = threading.Thread(target=start)
36
60
 
37
- stream = P.open(format=pyaudio.paInt16, channels=1, rate=44100, frames_per_buffer=8820, input=True, output=False)
61
+ thread.start()
38
62
 
39
- ani = FuncAnimation(fig,update)
63
+ def stop():
40
64
 
65
+ global flag
41
66
 
67
+ flag = False
42
68
 
43
- def update(Z):
69
+ root.after(200)
44
70
 
71
+ stream.stop_stream()
45
72
 
73
+ stream.close()
46
74
 
47
- input = stream.read(8820, exception_on_overflow=False)
48
-
49
- ndarray = np.frombuffer(input, dtype='int16')
50
-
51
- ax.cla()
52
-
53
- ax.plot(ndarray)
75
+ P.terminate()
54
-
55
- canvas.draw()
56
76
 
57
77
 
58
78
 
@@ -60,7 +80,7 @@
60
80
 
61
81
  root.title("abc")
62
82
 
63
- root.geometry("800x600") ##### guiの用意
83
+ root.geometry("800x700") ##### guiの用意
64
84
 
65
85
 
66
86
 
@@ -86,11 +106,21 @@
86
106
 
87
107
 
88
108
 
89
- button = tk.Button(root, text='Start', command=click)
109
+ start = tk.Button(root, text='Start', command=click)
110
+
111
+ start.grid(row=6, column=10, columnspan=1, rowspan=1, padx=10, pady=10)
90
112
 
91
113
 
92
114
 
115
+ stop = tk.Button(root, text='Stop', command=stop)
116
+
93
- button.grid(row=6, column=11, columnspan=1, rowspan=1, padx=10, pady=10)
117
+ stop.grid(row=7, column=10, columnspan=1, rowspan=1, padx=10, pady=10)
118
+
119
+
120
+
121
+ P = pyaudio.PyAudio()
122
+
123
+ stream = P.open(format=pyaudio.paInt16, channels=1, rate=44100, frames_per_buffer=8820, input=True, output=False)
94
124
 
95
125
 
96
126
 
@@ -104,7 +134,7 @@
104
134
 
105
135
 
106
136
 
107
-
137
+ threadingにて他の関数の場合問題なく起動することを確認
108
138
 
109
139
 
110
140
 

2

コード修正

2020/08/13 13:42

投稿

chanpoo
chanpoo

スコア3

test CHANGED
File without changes
test CHANGED
@@ -30,31 +30,29 @@
30
30
 
31
31
 
32
32
 
33
+ def click():
34
+
35
+ P = pyaudio.PyAudio()
36
+
37
+ stream = P.open(format=pyaudio.paInt16, channels=1, rate=44100, frames_per_buffer=8820, input=True, output=False)
38
+
39
+ ani = FuncAnimation(fig,update)
40
+
41
+
42
+
33
43
  def update(Z):
44
+
45
+
34
46
 
35
47
  input = stream.read(8820, exception_on_overflow=False)
36
48
 
37
49
  ndarray = np.frombuffer(input, dtype='int16')
38
50
 
39
- plt.clf()
51
+ ax.cla()
40
52
 
41
- plt.plot(ndarray)
53
+ ax.plot(ndarray)
42
54
 
43
-
44
-
45
- P = pyaudio.PyAudio()
46
-
47
- stream = P.open(format=pyaudio.paInt16, channels=1, rate=44100, frames_per_buffer=8820, input=True, output=False)
48
-
49
- fig = plt.figure()
50
-
51
- ani = FuncAnimation(fig,update)
52
-
53
- plt.show()
55
+ canvas.draw()
54
-
55
-
56
-
57
- ################# これより上だけでは動く ####################
58
56
 
59
57
 
60
58
 
@@ -62,7 +60,7 @@
62
60
 
63
61
  root.title("abc")
64
62
 
65
- root.geometry("400x300") ##### guiの用意
63
+ root.geometry("800x600") ##### guiの用意
66
64
 
67
65
 
68
66
 
@@ -70,11 +68,9 @@
70
68
 
71
69
 
72
70
 
73
- fig = plt.figure() ##### この3行をgifに書き換えたい
71
+ fig = plt.figure()
74
72
 
75
- ax = fig.add_subplot(1,1,1)
73
+ ax = fig.add_subplot(1,1,1)
76
-
77
- ax.scatter(1,2)
78
74
 
79
75
 
80
76
 
@@ -86,7 +82,15 @@
86
82
 
87
83
  canvas.get_tk_widget().pack() ##### canvasを配置
88
84
 
89
- frame.pack() ##### frameを配置
85
+ frame.grid(row=0, column=0, columnspan=10, rowspan=5, padx=10, pady=10) ##### frameを配置
86
+
87
+
88
+
89
+ button = tk.Button(root, text='Start', command=click)
90
+
91
+
92
+
93
+ button.grid(row=6, column=11, columnspan=1, rowspan=1, padx=10, pady=10)
90
94
 
91
95
 
92
96
 

1

コードを見やすくした。

2020/08/13 04:04

投稿

chanpoo
chanpoo

スコア3

test CHANGED
File without changes
test CHANGED
@@ -12,21 +12,17 @@
12
12
 
13
13
  ### 該当のソースコード
14
14
 
15
-
16
-
17
- python
15
+ ```python
18
-
19
-
20
16
 
21
17
  import tkinter as tk
18
+
19
+ import tkinter.ttk as ttk
22
20
 
23
21
  import pyaudio
24
22
 
25
23
  import matplotlib.pyplot as plt
26
24
 
27
25
  import numpy as np
28
-
29
-
30
26
 
31
27
  from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
32
28
 
@@ -40,29 +36,9 @@
40
36
 
41
37
  ndarray = np.frombuffer(input, dtype='int16')
42
38
 
43
- fig.clf()
39
+ plt.clf()
44
40
 
45
- fig.plot(ndarray)
41
+ plt.plot(ndarray)
46
-
47
-
48
-
49
- root = tk.Tk()
50
-
51
- root.title("")
52
-
53
- root.geometry("400x300")
54
-
55
-
56
-
57
- fig = plt.figure()
58
-
59
- frame = tk.Frame(root)
60
-
61
- canvas = FigureCanvasTkAgg(fig, master=frame)
62
-
63
- canvas.draw()
64
-
65
- canvas.get_tk_widget().pack()
66
42
 
67
43
 
68
44
 
@@ -70,21 +46,53 @@
70
46
 
71
47
  stream = P.open(format=pyaudio.paInt16, channels=1, rate=44100, frames_per_buffer=8820, input=True, output=False)
72
48
 
73
-
49
+ fig = plt.figure()
74
50
 
75
51
  ani = FuncAnimation(fig,update)
76
52
 
53
+ plt.show()
77
54
 
78
55
 
79
- stream.stop_stream()
80
56
 
81
- stream.close()
57
+ ################# これより上だけでは動く ####################
82
58
 
59
+
60
+
83
- P.terminate()
61
+ root = tk.Tk()
62
+
63
+ root.title("abc")
64
+
65
+ root.geometry("400x300") ##### guiの用意
66
+
67
+
68
+
69
+ frame = tk.Frame(root) ##### frameの用意
70
+
71
+
72
+
73
+ fig = plt.figure() ##### この3行をgifに書き換えたい
74
+
75
+ ax = fig.add_subplot(1,1,1)
76
+
77
+ ax.scatter(1,2)
78
+
79
+
80
+
81
+ canvas = FigureCanvasTkAgg(fig, master=frame) ##### frameにcanvasを置く
82
+
83
+ canvas.draw() ##### canvasを書く?
84
+
85
+
86
+
87
+ canvas.get_tk_widget().pack() ##### canvasを配置
88
+
89
+ frame.pack() ##### frameを配置
84
90
 
85
91
 
86
92
 
87
93
  root.mainloop()
94
+
95
+ ```
88
96
 
89
97
 
90
98