【发布时间】:2016-05-09 04:22:02
【问题描述】:
这里我有一个 runStuff 函数,我想重复运行 saySomething() 函数,但按下退出按钮后会退出。
当前正在运行 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()的真正意思是什么?您似乎正在寻找可以告诉您应用程序是否停止的信息? -
不太确定您的问题。只要未按下退出按钮,我希望 runStuff() 继续调用 saySomething() 。当按下退出按钮时,我希望它执行退出功能。
-
好吧,你在两个不同的地方打电话给
stop(),你似乎希望它在每个地方都能做一些完全不同的事情。在某个地方,您希望它停止您的程序。另一方面,您希望它检查您是否已停止该程序。但是你以完全相同的方式调用它,Python 无法读懂你的想法。所以其中一个电话肯定是错的……我就是这么说的。 -
我是 kivy 新手,对如何在 runStuff() 循环中激活 stop() 有点困惑。
-
您可能会看到问题,因为您在回调函数中放置了带有
time.sleep()的循环。如果你想每 X 秒运行一次,看起来你应该使用Clock。
标签: android python-2.7 while-loop kivy