質問編集履歴
1
進捗に応じた質問の変化
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
Pythonのtkinterを用いたGUIアプリケーションでボタンを押した際の動作が
|
1
|
+
Pythonのtkinterを用いたGUIアプリケーションでボタンを押した際の動作が実行できない
|
body
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
|
-
tkinterを用いたGUIのアプリケーションを作っているのですが、実行ボタンが押された際に関数を実行して目的の動作を行わせ、停止ボタンが押された際に停止させようと思ってい
|
2
|
+
tkinterを用いたGUIのアプリケーションを作っているのですが、実行ボタンが押された際に関数を実行して目的の動作を行わせ、停止ボタンが押された際に停止させようと思っています。ボタンを押した際の動作はうまくいったのですが、停止ボタンを押そうとしてもstartの関数内でループしてしまい停止ボタンを押すことができません、どうすればよいでしょうか
|
3
3
|
### 該当のソースコード
|
4
4
|
|
5
5
|
```Python
|
@@ -11,10 +11,11 @@
|
|
11
11
|
import threading
|
12
12
|
import sys
|
13
13
|
import tkinter
|
14
|
-
t=1
|
15
|
-
def start1():
|
16
|
-
t=0
|
17
14
|
|
15
|
+
root =tkinter.Tk()
|
16
|
+
root.title(u"app")
|
17
|
+
root.geometry("600x700")
|
18
|
+
|
18
19
|
def end1():
|
19
20
|
t=1
|
20
21
|
|
@@ -98,99 +99,94 @@
|
|
98
99
|
else:
|
99
100
|
return None
|
100
101
|
|
102
|
+
def start():
|
101
103
|
|
102
|
-
|
104
|
+
# 内蔵カメラを起動(カメラが一つしか繋がっていない場合は、引数に0を渡せば良い)
|
103
|
-
|
105
|
+
cap = cv2.VideoCapture(1)
|
104
|
-
root.geometry("600x700")
|
105
106
|
|
106
|
-
btn1=tkinter.Button(root, text='aモード 開始',font=(" ",20),bg='#f0e68c',fg='#ff0000',height=5,width=14,command=start1)
|
107
|
-
btn1.place(x=50,y=170)
|
108
|
-
|
109
|
-
btn2=tkinter.Button(root, text='aモード 停止',font=(" ",20),bg='#f0e68c',fg='#ff0000',height=5,width=14,command=end1)
|
110
|
-
btn2.place(x=340,y=170)
|
111
|
-
|
112
|
-
if t==0:
|
113
|
-
# 内蔵カメラを起動(カメラが一つしか繋がっていない場合は、引数に0を渡せば良い)
|
114
|
-
cap = cv2.VideoCapture(1)
|
115
|
-
|
116
|
-
|
107
|
+
flgb1=0
|
117
|
-
|
108
|
+
flgt1=0
|
118
|
-
|
109
|
+
flgb2=0
|
119
|
-
|
110
|
+
flgt2=0
|
120
111
|
|
121
|
-
|
112
|
+
while True:
|
122
|
-
# 赤色の円を抽出する
|
123
|
-
frame = cap.read()[1]
|
124
|
-
getframe1 = getCircle1(frame, np.array([130,80,80]), np.array([200,255,255]))
|
125
|
-
#黄色の円を検出
|
126
|
-
getframe2 = getCircle2(frame, np.array([30,50,50]), np.array([80,255,255]))
|
127
113
|
|
128
|
-
|
114
|
+
# 赤色の円を抽出する
|
129
|
-
if getframe1 is not None:
|
130
|
-
# 見つかった円の上に青い円を描画
|
131
|
-
|
115
|
+
frame = cap.read()[1]
|
116
|
+
getframe1 = getCircle1(frame, np.array([130,80,80]), np.array([200,255,255]))
|
117
|
+
#黄色の円を検出
|
132
|
-
|
118
|
+
getframe2 = getCircle2(frame, np.array([30,50,50]), np.array([80,255,255]))
|
133
|
-
if flgb1==0:#flgb1が0の時
|
134
|
-
filename = 'ファイルのパス' #再生したいmp3ファイル
|
135
|
-
pygame.mixer.init()
|
136
|
-
pygame.mixer.music.load(filename) #音源を読み込み
|
137
|
-
mp3_length = mp3(filename).info.length #音源の長さ取得
|
138
|
-
pygame.mixer.music.play(1) #再生開始。1の部分を変えるとn回再生(その場合は次の行の秒数も×nすること)
|
139
|
-
time.sleep(mp3_length + 0.25) #再生開始後、音源の長さだけ待つ(0.25待つのは誤差解消)
|
140
|
-
pygame.mixer.music.stop() #音源の長さ待ったら再生停止
|
141
|
-
flgb1=1#flgb1に1を代入し一回だけの処理
|
142
|
-
flgt1=0#flgt1に0を代入し時間処理を可能にする
|
143
|
-
at1=time.time()+5#今の時間+5秒
|
144
119
|
|
145
|
-
|
120
|
+
#赤が見つかったら
|
146
|
-
|
121
|
+
if getframe1 is not None:
|
147
|
-
|
122
|
+
# 見つかった円の上に青い円を描画
|
148
|
-
|
123
|
+
# getframe[0]:中心座標、getframe[1]:半径
|
149
|
-
|
124
|
+
cv2.circle(frame, getframe1[0], getframe1[1], (255, 0, 0), 2)
|
150
|
-
|
125
|
+
if flgb1==0:#flgb1が0の時
|
151
|
-
|
126
|
+
filename = 'ファイルパス' #再生したいmp3ファイル
|
152
|
-
|
127
|
+
pygame.mixer.init()
|
153
|
-
|
128
|
+
pygame.mixer.music.load(filename) #音源を読み込み
|
154
|
-
|
129
|
+
mp3_length = mp3(filename).info.length #音源の長さ取得
|
155
|
-
|
130
|
+
pygame.mixer.music.play(1) #再生開始。1の部分を変えるとn回再生(その場合は次の行の秒数も×nすること)
|
156
|
-
|
131
|
+
time.sleep(mp3_length + 0.25) #再生開始後、音源の長さだけ待つ(0.25待つのは誤差解消)
|
157
|
-
|
132
|
+
pygame.mixer.music.stop() #音源の長さ待ったら再生停止
|
158
|
-
|
133
|
+
flgb1=1#flgb1に1を代入し一回だけの処理
|
159
|
-
|
134
|
+
flgt1=0#flgt1に0を代入し時間処理を可能にする
|
160
|
-
|
135
|
+
at1=time.time()+5#今の時間+5秒
|
161
136
|
|
137
|
+
#黄色が見つかったら
|
138
|
+
if getframe2 is not None:
|
139
|
+
# 見つかった円の上に青い円を描画
|
140
|
+
# getframe[0]:中心座標、getframe[1]:半径
|
141
|
+
cv2.circle(frame, getframe2[0], getframe2[1], (255, 0, 0), 2)
|
142
|
+
if flgb2==0:#flgb2が0の時
|
143
|
+
filename = 'ファイルパス' #再生したいmp3ファイル
|
144
|
+
pygame.mixer.init()
|
145
|
+
pygame.mixer.music.load(filename) #音源を読み込み
|
146
|
+
mp3_length = mp3(filename).info.length #音源の長さ取得
|
147
|
+
pygame.mixer.music.play(1) #再生開始。1の部分を変えるとn回再生(その場合は次の行の秒数も×nすること)
|
148
|
+
time.sleep(mp3_length + 0.25) #再生開始後、音源の長さだけ待つ(0.25待つのは誤差解消)
|
149
|
+
pygame.mixer.music.stop() #音源の長さ待ったら再生停止
|
150
|
+
flgb2=1#flgb2に1を代入し一回だけの処理
|
151
|
+
flgt2=0#flgt2に0を代入し時間処理を可能にする
|
152
|
+
at2=time.time()+5#今の時間+5秒
|
162
153
|
|
154
|
+
nt1=time.time()
|
155
|
+
nt2=time.time()
|
163
156
|
|
157
|
+
if flgb1==1 and flgt1==0 and at1<=nt1:
|
158
|
+
flgb1=0
|
164
|
-
|
159
|
+
flgt1=1
|
165
|
-
nt2=time.time()
|
166
160
|
|
167
|
-
if flgb1==1 and flgt1==0 and at1<=nt1:
|
168
|
-
flgb1=0
|
169
|
-
flgt1=1
|
170
|
-
|
171
|
-
|
161
|
+
if flgb2==1 and flgt2==0 and at2<=nt2:
|
172
|
-
|
162
|
+
flgb2=0
|
173
|
-
|
163
|
+
flgg2=1
|
174
164
|
|
175
|
-
|
165
|
+
# 検出結果とともに映像を表示
|
176
|
-
|
166
|
+
cv2.imshow('Circle Detect', frame)
|
177
167
|
|
178
168
|
|
179
|
-
|
169
|
+
if cv2.waitKey(1) & 0xFF == ord('q'):
|
180
|
-
|
170
|
+
break
|
181
171
|
|
182
172
|
# 終了時にカメラを解放
|
183
|
-
|
173
|
+
cap.release()
|
184
|
-
|
174
|
+
cv2.destroyAllWindows()
|
185
175
|
|
176
|
+
btn1=tkinter.Button(root, text='aモード 開始',font=(" ",20),bg='#f0e68c',fg='#ff0000',height=5,width=14,command=start)
|
177
|
+
btn1.place(x=50,y=170)
|
186
178
|
|
179
|
+
btn2=tkinter.Button(root, text='aモード 停止',font=(" ",20),bg='#f0e68c',fg='#ff0000',height=5,width=14,command=end1)
|
180
|
+
btn2.place(x=340,y=170)
|
181
|
+
|
187
182
|
root.mainloop()
|
188
183
|
|
184
|
+
|
189
185
|
```
|
190
186
|
|
191
187
|
### 試したこと
|
192
|
-
ボタンが押された際に
|
188
|
+
ボタンが押された際にstartの関数を呼び出し、動作させることに成功した。
|
193
|
-
|
189
|
+
停止ボタンが押された際にstart内の動作を停止させたい。
|
194
190
|
|
195
191
|
### 補足情報(FW/ツールのバージョンなど)
|
196
192
|
|