ここで私は関数saySomething()を繰り返し実行し続けたいが、終了ボタンを押すと終了する関数runStuffがあります。Kivyのwhileループを壊すには?
現在、終了ボタンの入力を許可せずにwhileループを実行しています。
def saySomething():
ttsSpeak('Say this phrase')
def runStuff(self):
while True:
if App.get_running_app().stop() != True:
saySomething()
time.sleep(2)
else: break
def exit(self):
App.get_running_app().stop()
class AudibleCoachApp(App):
def build(self):
layout = BoxLayout(padding=7, spacing=3, orientation='vertical') # set layout of the screen
btn1 = Button(text='Start it', size_hint=(1, .66)) # create a button instance
btn1.bind(on_press=runStuff) # binding the button with the function below
btn3 = Button(text='Exit', size_hint=(1, .17)) # create a button instance
btn3.bind(on_press=exit) # binding the button with the function below
layout.add_widget(btn1) # physically add the button onto the layout
layout.add_widget(btn2) # physically add the button onto the layout
layout.add_widget(btn3) # physically add the button onto the layout
return layout
if __name__ == '__main__':
AudibleCoachApp().run()
whileループ条件で 'stop()'の代わりに本当に何を呼び出すのですか?あなたのアプリが止まっているかどうかを知らせる何かを探しているようですね。 –
あなたの質問についてはあまり確かではありません。 exitボタンが押されていない限り、runStuff()がsaySomething()を呼び出し続けるようにします。 exitボタンを押すと、exit関数を実行します。 – KubiK888
さて、まあ、別の場所で 'stop()'と呼んでください。あなたは、それぞれの場所で全く違うことをやろうと思っているようです。 1つの場所では、プログラムを停止する必要があります。もう一方では、あなたがプログラムを停止したかどうかを確認したい。しかし、あなたはまったく同じ方法でそれを呼び出し、Pythonはあなたの心を読むことができません。だから、それらの呼び出しの1つが間違っている必要があります...それは私が言っていたすべてです。 –