如何在Kivy中打破一段时间?

时间:2016-05-09 04:22:02

标签: android python-2.7 while-loop kivy

这里我有一个函数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()

1 个答案:

答案 0 :(得分:2)

无限循环在UI编程中很少是一个很好的解决方案,因为它们会阻塞它们运行的​​整个线程。最好使用kivy的Clock对象来安排你的方法。

from kivy.clock import Clock

def runStuff(self):
    Clock.schedule(saySomething, 2)

然后可以使用Clock.unschedule取消调度回调。

def exit(self):
    App.get_running_app().stop()
    Clock.unschedule(saySomething)

此外,通常不鼓励使用get_running_app,因为将所有可以访问应用程序的功能作为应用程序的方法是一种很好的做法。由于您在每个函数中都包含self个参数,因此我认为这是您计划要做的事情。

class AudibleCoachApp(App):

    def runStuff(self):
        Clock.schedule(saySomething, 2)

    def exit(self):
        Clock.unschedule(saySomething)
        self.stop()

您必须将runStuff中的exitbuild的来电更改为self.runStuffself.exit

btn1.bind(on_press=self.runStuff) # binding the button with the function          

...

btn3.bind(on_press=self.exit) # binding the button with the function below

而且,您需要明确绑定应用以阻止Clock被GC加入。

if __name__ == '__main__':
    app = AudibleCoachApp()
    app.run()

总而言之,代码变为以下内容(不要忘记从Clock添加kivy.clock的导入。)

import pyttsx
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.clock import Clock

# ...

def saySomething():
    ttsSpeak('Say this phrase')

class AudibleCoachApp(App):

    def runStuff(self):
        Clock.schedule_interval(saySomething, 2)

    def exit(self):
        Clock.unschedule(saySomething)
        self.stop()

    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=self.runStuff) # binding the button with the function below
        btn2 = Button()
        btn3 = Button(text='Exit', size_hint=(1, .17)) # create a button instance
        btn3.bind(on_press=self.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__':
    app = AudibleCoachApp()
    app.run()

请注意,上述所有代码均未经过测试。如果您发现任何错误,请发表评论。