Screen Manager, Screen events. on_pre_enter etc

1,098 views
Skip to first unread message

Sam Berry

unread,
Mar 7, 2013, 4:44:57 PM3/7/13
to kivy-...@googlegroups.com
Hey,

Iv got a Screenmanager app already up and running. However within one of my screens i want to update the information from a file every time i enter that screen. Iv seen in the documentation for Screenmanager that you can use commands such as on_pre_enter, on_enter etc. However i have no idea how to implement these commands.

Has anybody used these? or can give me a quick example of how to call a function on_pre_enter to a screen?

Thanks, Sam

Gabriel Pettier

unread,
Mar 7, 2013, 5:42:54 PM3/7/13
to kivy-...@googlegroups.com
Hi

as can be seen in the doc
(http://kivy.org/docs/api-kivy.uix.screenmanager.html#kivy.uix.screenmanager.Screen)
these are events dispatched by the screen instance, so you can bind to
them, either from python:

```
sm = Screenmanager()
s = Screen(name='test')
s.bind(on_enter=some_callback)
sm.add_widget(s)
```

or in kv:

```
ScreenManager:
Screen:
name: 'test'
on_enter: some_callback()
```

hope this helps
> --
> You received this message because you are subscribed to the Google Groups "Kivy users support" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Sam Berry

unread,
Mar 7, 2013, 6:16:19 PM3/7/13
to kivy-...@googlegroups.com
Hey,

Thanks for the quick reply. That helps me to an extent, however i tried implementing it and failed. I have been building my App using the examples and pot luck really. Within my python file for the screenmanager i have....

sm = ScreenManager(transition=SwapTransition())
sm.add_widget(Home(name='Menu'))
sm.add_widget(Camera1(name='Camera 1'))
sm.add_widget(Camera2(name='Camera 2'))
sm.add_widget(Record(name='Record Page'))
sm.add_widget(Settings1(name='Settings Page'))
sm.add_widget(Team_Select(name='Team_Select'))

And a snippet of the kv file which i would like the function to be run when entering

<Team_Select>:
    canvas:
        Color:
            rgb: 0,0,.1
        Rectangle:
            size: self.size
 
    BoxLayout:
        orientation: 'vertical'
        GridLayout:
            rows: 6
            columns: 2
            CheckBox:
                group: 'Team1'
                on_active: root.set_active(self)
            Label:
                font_size: 15
                text: 'Team1'
            CheckBox:
                group: 'Team2'
                on_active: root.set_active(self) 
            Label:
                font_size: 15
                text: 'Team2'

I have tried adding on_pre_enter: root.Myfunction() within the kv file with many errors, which from looking at your Kv example i haven't set up my ScreenManager in a conventional way?

I then tried adding

ts = Screen(name='Team_Select')
ts.bind(on_enter=Test())

And adding the Test() function into     class Team_Select(Screen):  with no success.

Sorry for the essay! Just trying to give as much info as possible as iv been stuck on this for hours!

Thanks, Sam

Gabriel Pettier

unread,
Mar 8, 2013, 5:19:58 AM3/8/13
to kivy-...@googlegroups.com
Hi,

your bindind must pass the function/method object, like any regular
python callback passing.

assuming i do

```
def test(*args):
print args
```

I can do

```
my_object.bind(on_something=test)
```

because "test" is a known function name here.

But if i do

```
class MyClass(SomeWidgetClass):
def test(self, *args):
print args

my_object = MyClass()
```

i have to do:

```
my_object.bind(on_something=my_object.test)
```

because "test" is nothing here, we only have "my_object" which has a
"test" method, that's this one we want to pass.

cheers
Reply all
Reply to author
Forward
0 new messages