Skip to content

Instantly share code, notes, and snippets.

@kived
Last active May 31, 2016 21:09
Show Gist options
  • Save kived/0d2f95e3fe1dd116e43918991642e916 to your computer and use it in GitHub Desktop.
Save kived/0d2f95e3fe1dd116e43918991642e916 to your computer and use it in GitHub Desktop.
Kivy: referencing widgets
BoxLayout:
NavBar:
sm: screen_manager
ScreenManager:
id: screen_manager
#:import NoTransition kivy.uix.screenmanager.NoTransition
BoxLayout:
orientation: 'vertical'
NavBar:
sm: screen_manager
name: 'nav_bar' # if you actually need this, name is usually a Screen thing
ScreenManager:
id: screen_manager
transition: NoTransition()
SplashScreen:
name: 'splash_screen' # for a single-use screen, you can also set the name in the kv rule for the screen so you won't need it here
WeatherScreen:
name: 'weather_screen'
OperatorPanelScreen:
name: 'operator_panel_screen'
@ilovetogetspamed
Copy link

The reason I was doing what I was doing was so that I could keep modules of code. I know my app will grow quickly with new functionality.

I'm trying to do:

AppRootDir
    assets
        fonts
    modules
        navbar
             navbar.kv
             navbar.py
        module 1 (e.g. Splash Screen)
            module1.kv
            module1.py
         module 2 (e.g. Weather Screen)
            module2.kv
            module2.py
         module N
               ...
    main.kv <<-- based on your suggestion above
    main.py

and then load everything at startup. Sort of like Django does, if you're familiar with that. And I can keep my modules, well modular ... LOL

I had it all working with a single KV file and single class... but that will be really hard to maintain going into the future.

@kived
Copy link
Author

kived commented May 31, 2016

You can still have separate kv files for other things, but at some point you need to put together the root widget. The root widget can be defined in a separate kv file - usually with the name of your app, as we will autoload this. For an app named ScreenManagerApp we will automatically look for a file named screenmanager.kv in the same folder. If you wanted to be more dynamic about which screens are added, just can them out of the root widget kv and add the screens in Python. The app kv file is loaded before App.build() is called.

def build(self):
    self.root.ids.screen_manager.add_widget(SplashScreen())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment