在屏幕上停留3秒后切换屏幕

时间:2014-04-04 05:51:07

标签: python kivy

问题在于screenmanager我有2个屏幕说装载屏幕和菜单屏幕。 在开始我首先加载加载屏幕,3秒后我将它切换到菜单屏幕,这有助于时钟安排。 在菜单屏幕上,我有一个按钮,按下后会将我们带回加载屏幕。

现在我想在加载屏幕激活3秒后返回菜单屏幕。有人可以提出最佳方法。下面的代码解释了我拥有的和我需要的东西:

我知道更改下面的代码以使LoadingScreen类中的Clock.schedule_interval(self.callNext,3)可以完成这项工作,但我正在寻找更有效的选项

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen,FallOutTransition
from kivy.properties import ObjectProperty,NumericProperty
from kivy.uix.image import Image
from kivy.graphics import Color
from kivy.clock import Clock


gui_v3 = '''
<PlayScreen>:
    play_Button: playButton
    canvas.before:
        Color:
            rgb: (0, 0, 1)
    GridLayout:
        rows:1
        size_hint: .1,.1
        pos_hint: {'center_x': .5, 'center_y': .5}
        Button:
            id: playButton
            size_hint_x: None
            width: 100
            text: 'Play !'
            font_size: 12
            bold: True
            italic: False
            border: 10,10,10,10
            color: (0.5, 1, 0.5, 1)

            on_press: root.playButton_press()
<LoadingScreen>:
    canvas:
        Color:
            rgba: 0.4, 0.4, 0.4, 1
        Rectangle:
            pos: root.center
            size: (32, 32)
    BoxLayout:
        Label:
            text: 'JB'
            font_size: 100
        Label:
            text: 'Loading...'
            font_size: 10

'''


class PlayScreen(Screen):
    play_Button = ObjectProperty(None)

    def __init__(self, **kwargs):
        super(PlayScreen, self).__init__(**kwargs)
        Clock.schedule_interval(self.update, 1.0/2)

    def update(self,dt):
        print "Current screen is ",self.manager.current


    def playButton_press(self):
        print "Hi Play button is pressed"
        sm.current = 'loading'


class LoadingScreen(Screen):
    def __init__(self, **kwargs):
        super(LoadingScreen, self).__init__(**kwargs)
        Clock.schedule_once(self.callNext, 3)


    def callNext(self,dt):
        self.manager.current = 'play'
        print "Hi this is call Next Function of loading 1"


# Create the screen manager
Builder.load_string(gui_v3)
sm = ScreenManager(transition= FallOutTransition())
sm.add_widget(LoadingScreen(name='loading'))
sm.add_widget(PlayScreen(name='play'))


class MyJB(App):
    def build(self):
        print sm.screen_names
        return sm

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

1 个答案:

答案 0 :(得分:1)

您应该使用屏幕on_enter事件。只需在kv文件中执行此操作:

<LoadingScreen>:
    on_enter: Clock.schedule_once(self.callNext, 3)

还需要导入时钟{k}的顶部#:import Clock kivy.clock.Clock。现在,每次打开屏幕时,它都会安排回调。