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

import os
import sys

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, NoTransition
from kivy.uix.boxlayout import BoxLayout

from kivy.core.text import LabelBase, Label
from kivy.config import Config

from kivy.properties import NumericProperty

from kivy.lang import Factory

from modules.navbar import NavBar
from modules.splashscreen import SplashScreen
from modules.weatherscreen import WeatherScreen
from modules.operatorpanelscreen import OperatorPanelScreen

Adjust for RasPi touch screen size

Config.set('graphics', 'width', '800')
Config.set('graphics', 'height', '480')

APP_PATH = os.path.dirname(file)

LabelBase.register(name='FontAwesome',
fn_regular='assets/fonts/font-awesome-4.6.3/fonts/fontawesome-webfont.ttf')

LabelBase.register(name='LCDMono',
fn_regular='assets/fonts/LCDMonoWinTT/LCDM2N__.TTF',
fn_bold='assets/fonts/LCDMonoWinTT/LCDM2B__.TTF')

class ScreenManagerApp(App):

def build(self):

    # # Common modules
    Factory.register('NavBar', module='modules.navbar')
    Factory.register('SplashScreen', module='modules.splashscreen')
    Factory.register('WeatherScreen', module='modules.weatherscreen')
    Factory.register('OperatorPanelScreen', module='modules.operatorpanelscreen')

    splash_screen = SplashScreen(name='splash_screen')
    weather_screen = WeatherScreen(name='weather_screen')
    operator_panel_screen = OperatorPanelScreen(name='operator_panel_screen')

    root = BoxLayout(orientation='vertical')
    root.add_widget(NavBar(name='nav_bar'))

    sm = ScreenManager(id='sm', transition=NoTransition())
    sm.add_widget(splash_screen)
    sm.add_widget(weather_screen)
    sm.add_widget(operator_panel_screen)
    sm.current = 'splash_screen'

    root.add_widget(sm)

    return root

if name == 'main':
ScreenManagerApp().run()

@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