Reputation: 1412
Problem is with screenmanager where i have 2 screens say loadingscreen and menuscreen . In starting I load loading screen first and after 3 seconds i switch it to menuscreen which help of clock schedule . On menu screen I have a button which when pressed takes us back to loading screen .
Now i want to move back to menu screen after 3 seconds of loading screen is active .. Can someone suggest best way to do it . Below is a code which explains what i have and what i need :
I know changing below code to have Clock.schedule_interval(self.callNext, 3) in LoadingScreen Class will do the job but i am looking for a better option which is much more efficient
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()
Upvotes: 0
Views: 2044
Reputation: 1347
You should use the screen's on_enter
event. Simply do this in the kv file:
<LoadingScreen>:
on_enter: Clock.schedule_once(self.callNext, 3)
Also a the top of the kv you need to import Clock, #:import Clock kivy.clock.Clock
. Now every time the screen is opened it'll schedule the callback.
Upvotes: 1