在Kivy中控制2个时钟时间表的问题

时间:2016-03-19 05:57:36

标签: python kivy

我有一个按钮。按下它,我想在5秒后调用一次动作,然后在每1秒后无限次地调动第二次动作。但我需要在行动一完成后才开始行动。

我的代码:

#!/usr/bin/kivy
import kivy
kivy.require('1.7.2')

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.button import Button
from kivy.clock import Clock
from functools import partial

Builder.load_string('''
<MenuScreen>:            
    Button:
        id:timer1
        text: 'click me'
        on_press: root.val()
''')
class MenuScreen(Screen):
    def val(self):
        Clock.schedule_once(self.my_callback_timer1, 5)
        Clock.schedule_interval(self.my_callback_timer2, 1)
    def my_callback_timer1(self, interval):
        print "5 sec code executed"
    def my_callback_timer2(self, interval):
        print "1 sec code executed"


sm = ScreenManager()
menu = MenuScreen(name='menu')
sm.add_widget(menu)

class MainApp(App):
    def build(self):
        return sm

if __name__ == '__main__':
    MainApp().run()

这使得o / p为:

1 sec code executed
1 sec code executed
1 sec code executed
1 sec code executed
5 sec code executed
1 sec code executed
...
...

我需要的是:

5 sec code executed
(wait 5 sec)
1 sec code executed
1 sec code executed
1 sec code executed
...

1 个答案:

答案 0 :(得分:0)

在第一个回调中安排第二个时钟:

class MenuScreen(Screen):
    def val(self):
        Clock.schedule_once(self.my_callback_timer1, 5)

    def my_callback_timer1(self, interval):
        print "5 sec code executed"
        Clock.schedule_interval(self.my_callback_timer2, 1)
    def my_callback_timer2(self, interval):
        print "1 sec code executed"