Pass variable from .py file to .kv file
Question: I'm new to Kivy and am kind of learning on the job. I've a basic understanding
of how to utilise various widgets and nest layouts. The code is as follows
(saved as GUI.py):-

    
    
    from kivy.app import App
    from kivy.lang import Builder
    from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
    from kivy.properties import ListProperty, NumericProperty, StringProperty
    
    class TestScreen(Screen):
        pass
    
    class VariableScreen(Screen):
        pass
    
    class SummaryScreen(Screen):
        pass
    
    class ProgressScreen(Screen):
        pass
    
    class CompletedResultsScreen(Screen):
        pass
    
    class SavedResultsScreen(Screen):
        pass
    
    class ScreenManagement(ScreenManager):
        pass
    
    GUI_code = Builder.load_file("GUI.kv")
    
    class GUIWindow(App):        #App class is inherited
        sampletext = StringProperty("Five times Five")
    
        def build(self):
            return GUI_code
    
    if __name__ == "__main__":
        GUIWindow().run()
    

The GUI.kv file contains the following:

    
    
    #: import FadeTransition kivy.uix.screenmanager.FadeTransition
    
    ScreenManagement:
        transition: FadeTransition()
        TestScreen:
        VariableScreen:
        SummaryScreen:
        ProgressScreen:
        CompletedResultsScreen:
        SavedResultsScreen:
    
    <TestScreen>:
        name: "Test_Screen"
        FloatLayout:
            Label:
                text: "Test"
                size_hint: 0.1,0.1
                pos_hint: {"right":0.5,"top":1}
            Label:
                text: app.sampletext
                size_hint: 0.1,0.1
                pos_hint: {"right":0.1,"top":1}
            Button:
                on_release: app.root.current = "Saved_Results_Screen"
                text: "Saved Results"
                size_hint: 0.1,0.1
                pos_hint: {"left":1,"bottom":1}
                font_size: 15
            Button:
                on_release: app.root.current = "Variable_Screen"
                text: "Variable"
                size_hint: 0.1,0.1
                pos_hint: {"right":1,"bottom":1}
                font_size: 15
    

Only the relevant part of the .kv file is posted. Some strings have to be
passed from the .py file to the .kv file. The question was addressed in the
below link:

[Pass variable value from main.py to .kv
file](http://stackoverflow.com/questions/31370346/pass-variable-value-from-
main-py-to-kv-file?lq=1&newreg=bdc6a866403649bdbb6b16334a2db62b)

Based on the suggestion there, I placed sampletext in the GUIWindow class
using the StringProperty class. (Also tried a simple string sampletext = "Five
times Five". Gives the same error)

The code does not run when the text property under the second label in is set
to text: app.sampletext (An application window with white space opens. It is
non responsive. The python kernel needs to be reloaded to shut it)

The following error message is displayed

    
    
     18: pos_hint: {"right":0.5,"top":1}
     19: Label:
     >>   20: text: app.sampletext
     21: size_hint: 0.1,0.1
     22: pos_hint: {"right":0.1,"top":1}
     ...
     AttributeError: 'NoneType' object has no attribute 'bind'
    

It runs properly when the text property is set to text: "Five times Five"

Could someone be kind enough to explain what is going wrong?


Answer: I can't find any documentation describing why, but it seems like the parser is
trying to access `app.sampletext` when you parse the file, which you are doing
before your `App` class is even defined, let alone created.

Move the `Builder.parse` line into your `build(self):` function and it will
work fine.

