Screen.on_enter() not called in the first screen after app start

406 views
Skip to first unread message

Tomek CEDRO

unread,
Aug 21, 2022, 2:09:58 PM8/21/22
to kivy-...@googlegroups.com
Hello world :-)

I have noticed that although Screen.on_enter() is defined (in a *.kv
file) it is not being called on the first screen when application
starts. When I go to another screen and come back to the first screen
then on_enter() is called properly.

Is it a bug or feature? ;-)

How can I make on_enter() being called on application start?

The reason for using this call is to check user if the "Terms and
Conditions" have been agreed, if not then user is redirected to the
TaC screen.

Any hints welcome :-)
Tomek

--
CeDeROM, SQ7MHZ, http://www.tomek.cedro.info

Elliot Garbus

unread,
Aug 21, 2022, 2:50:06 PM8/21/22
to kivy-...@googlegroups.com

This is the correct operation.  The screen change is relative to the ScreenManager the screen belongs to.  You are setting the initial state as on the “first” screen.  So on_enter is not firing.  As a work around you could create a dummy screen as initial screen, and have it switch to the “first” screen causing on_enter to fire.

--

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.

To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CAFYkXjkX0N7-djFd3aoCKcjQzgBuF%2BqBqnqaZW2RXe61zjFMzA%40mail.gmail.com.

 

Tomek CEDRO

unread,
Aug 21, 2022, 3:01:15 PM8/21/22
to kivy-...@googlegroups.com
On Sun, Aug 21, 2022 at 8:50 PM Elliot Garbus wrote:
> This is the correct operation. The screen change is relative to the ScreenManager the screen belongs to. You are setting the initial state as on the “first” screen. So on_enter is not firing. As a work around you could create a dummy screen as initial screen, and have it switch to the “first” screen causing on_enter to fire.

Thank you Elliot :-)

I did so.. but then on the initial screen on_enter() also does not fire :-(

I did not find anything like this in ScreenManager so I could attach a
check that would put a desired screen as the current screen.

I guess that launching the on_enter() also on the initial screen would
solve the issue.. and made that behavior more intuitive / coherent.

By the way how can I access the root.manager from the Python code?

I did an ugly hack in the App.on_start() but I would be more happy to
have elegant solution :-)

Elliot Garbus

unread,
Aug 21, 2022, 3:52:28 PM8/21/22
to kivy-...@googlegroups.com

From any screen you can access that screens manager with it’s manager attribute, self.manager.

If the root widget is a screenmanager, you can access it from kv as app.root or from python as:

app = App.get_running_app()

manager = app.root

 

I’ll see if I can put together an example that does what you’re looking for.

--

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.

Elliot Garbus

unread,
Aug 21, 2022, 3:52:38 PM8/21/22
to kivy-...@googlegroups.com

You can use the on_kv_post event to change the screen… example below:

 

from kivy.app import App
from kivy.lang import Builder


kv =
"""
<DummyScreen@Screen>:
    on_kv_post: self.manager.current = 'First'  # change screen after kv is initialized
    Label:
        text: root.name

<LabelScreen@Screen>:
    on_enter: print(f'Screen {self.name} entered')
    BoxLayout:
        orientation: 'vertical'
        Label:
            text: root.name
        Button:
            size_hint_y: None
            height: 48
            text: 'Next'
            on_release: root.manager.current = root.manager.next()
           

ScreenManager:
    DummyScreen:
        name: 'Dummy'
    LabelScreen:
        name: 'First'
    LabelScreen:
        name: 'Second'
    LabelScreen:
        name: 'Third'

"""


class ScreenEnterExampleApp(App):
   
def build(self):
       
return Builder.load_string(kv)


ScreenEnterExampleApp().run()

 

 

 

 

 

From: Tomek CEDRO
Sent: Sunday, August 21, 2022 12:01 PM
To: kivy-...@googlegroups.com

--

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.

Tomek CEDRO

unread,
Aug 21, 2022, 6:51:48 PM8/21/22
to kivy-...@googlegroups.com
On Sun, Aug 21, 2022 at 9:52 PM Elliot Garbus wrote:
> You can use the on_kv_post event to change the screen… example below:

on_kv_post() is the way to go even with no additional screen! :-)

Also thank you for the:
app = App.get_running_app()
manager = app.root

Thank you Elliot You Rox! :-)

Tomek

Elliot Garbus

unread,
Aug 21, 2022, 7:13:45 PM8/21/22
to kivy-...@googlegroups.com

😊

 

-Elliot

 

From: Tomek CEDRO
Sent: Sunday, August 21, 2022 3:51 PM
To: kivy-...@googlegroups.com
Subject: Re: [kivy-users] Screen.on_enter() not called in the firstscreenafterapp start

 

On Sun, Aug 21, 2022 at 9:52 PM Elliot Garbus  wrote:

--

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.

Reply all
Reply to author
Forward
0 new messages