EagerIntern
EagerIntern

Reputation: 113

.kv file doesn't seem to work

So I'm just working with some example Kivy file code, and I came across this code which allows the user to switch between screens:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen

Builder.load_string("""
<MenuScreen>:
    BoxLayout:
        Button:
            text: 'Build Scenario'
            on_press: root.manager.current = 'settings'
        Button:
            text: 'Run Existing Scenerio'

<SettingsScreen>:
    BoxLayout:
        Button:
            text: 'Run Existing Scenerio'
        Button:
            text: 'Back to menu'
            on_press: root.manager.current = 'menu'
""")

# Declare both screens
class MenuScreen(Screen):
    pass

class SettingsScreen(Screen):
    pass

# Create the screen manager
sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(SettingsScreen(name='settings'))

class TestApp(App):

    def build(self):
        return sm

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

I wondered to myself if it would be possible to put the code given in the Builder.load_string() method into a separate .kv file. So I did just that. I commented the Builder part out (I admit I don't know what its role is) and copied the string into a .kv file which looks like this:

 # the file name is test.kv
 #:kivy 1.0.9

<MenuScreen>:
    BoxLayout:
        Button:
            text: 'Build Scenario'
            on_press: root.manager.current = 'settings'
        Button:
            text: 'Run Existing Scenerio'

<SettingsScreen>:
    BoxLayout:
        Button:
            text: 'Run Existing Scenerio'
        Button:
            text: 'Back to menu'
            on_press: root.manager.current = 'menu'

Unfortunately when I run the code now I just get a black screen. Can anyone tell me what's wrong? Thanks!

Upvotes: 2

Views: 477

Answers (1)

inclement
inclement

Reputation: 29450

The code creates the screenmanager (sm) in the main body of the python file. When the kv is loaded from a file this only happens later, so none of the kv rules are applied to sm. It was okay before because the load_string happens before it is instantiated.

For this reason, instantiating widgets this way is bad practice, and the sm = ScreenManager(... etc should be moved to the build method. This is run after the kv file is loaded, so everything should work.

Upvotes: 3

Related Questions