Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

on_pre_enter and on_enter are not called when a Screen instance is declared in kv #2565

Open
dessant opened this issue Oct 9, 2014 · 7 comments
Labels
Component: KV-lang kivy/lang, factory Status: Confirmed Confirmed as real issue Type: Bug A bug or something not working as intended

Comments

@dessant
Copy link
Contributor

dessant commented Oct 9, 2014

The python equivalent works as expected. The bug only appears for the first added screen, and only at add time, subsequent screen changes fire the events.

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


kv = """
<Scr@Screen>:
    on_pre_enter: print('on_pre_enter', self.name)
    on_enter: print('on_enter', self.name)

<Test>:
    ScreenManager:
        Scr:
            name: 'from kv'
"""

Builder.load_string(kv)


class Test(Factory.BoxLayout):
    def __init__(self, *args, **kwargs):
        super(Test, self).__init__(*args, **kwargs)
        sm = Factory.ScreenManager()
        s1 = Factory.Scr(name='from python')
        sm.add_widget(s1)
        self.add_widget(sm)


class TestApp(App):
    def build(self):
        return Test()

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

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

@dessant dessant added Component: KV-lang kivy/lang, factory Status: Needs-analysis Issue needs to be analyzed if it's real labels Oct 9, 2014
@tito
Copy link
Member

tito commented Oct 9, 2014

That's the same kind of issue that we have with parent and on_parent.

The KV language does:

  • Create the empty widget
  • Add it to the parent
  • Apply all the rules
    • Set attributes
    • Bind handlers

On the very first screen added in the screenmanager, the on_pre_enter/on_enter are fired when the current is changed. But at this time, handlers are not yet attached.

While we did something specific for parent / on_parent, we must find a solution for it, and not integrate specific fixes in kv lang again.

Proposals:

A. I'm thinking about a state that indicate the widget is still under construction (is_built = False by default, until apply() finished the job.). Then, a widget like the screenmanager can handle this issue (if child.is_built, then disptach event right now, else delay them).

B. Delay the add_widget until the widget is completly built. But this cannot works, as some rules might depends of others widget (id) and/or not built yet.

@tito tito added Status: Confirmed Confirmed as real issue and removed Status: Needs-analysis Issue needs to be analyzed if it's real labels Oct 9, 2014
@darkopevec
Copy link
Contributor

Currently, the on_enter does get triggered from KV. But when first Screen's on_enter is triggered, the Screen does not yet have a name - self.name is still ''.
The current code in ScreenManager changes the self.current from None to ''

How about postponing dispatching all events until the root is entirely built?

@stale
Copy link

stale bot commented Oct 7, 2017

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Oct 7, 2017
@victorbutler
Copy link

Was a solution ever implemented? Could on_enter be delayed until KV assigns a name?

@stale stale bot removed the stale label Oct 17, 2017
@KeyWeeUsr
Copy link
Contributor

@victorbutler I'm not aware of any implemented solution, however I like the A option as @tito mentioned it as it would provide quite a good anchor to work with and not just attempt to delay anything. Specifying the exact place and the whole code flow for KV interpreting with some variables seems more reasonable to me as it could be used for KV compiler, unlike the option B which I bet complicates stuff even more (at least in my eyes if I'd like to write a unittest for it).

With A implemented we could add the KV lang flow to the docs i.e. how it's parsed, what happens in the rules, etc, which seems to me like a common missing knowledge for beginners that is learned via trial & error.

@Julian-O
Copy link
Contributor

Confirmed still a problem in Kivy 2.2.1, Windows 11.

@Julian-O Julian-O added the Type: Bug A bug or something not working as intended label Nov 14, 2023
@ElliotGarbus
Copy link
Contributor

My workaround for this, if an on_enter was required on the "first" screen on a was to add a dummy first screen.
While I realize this is a bug, I had rationalized this as the first screen is starting in that position - so "of course it has not been entered..." I would also use on_start to change from the dummy screen, so the user does not see it. The dummy screen can also be deleted...

I think this is a valid option, and could be documented rather than fixed.

ScreenManager:
    Screen:
        name: 'dummy'  # does not receive on_enter at startup
    MyScreen:
        name: 'first_screen'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: KV-lang kivy/lang, factory Status: Confirmed Confirmed as real issue Type: Bug A bug or something not working as intended
Projects
None yet
Development

No branches or pull requests

7 participants