Kivy中两个时钟调度的控制问题

2024-04-26 17:42:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个纽扣。按下它,我想在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
...

Tags: fromimportselfmydefcallbackcodesec
1条回答
网友
1楼 · 发布于 2024-04-26 17:42:54

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

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"

相关问题 更多 >