Python Forum

Full Version: Initialize widget value from Python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I can't for the life of me get my code to initialize a widget (in a kv file) from Python code on program startup.
Simplified Python code is here:
inital_text = "init text"

class MainApp(App):

    def initialize_widgets(self):
        self.root.ids.my_label.text = initial_text

if __name__ == '__main__':
    MainApp().run()
    MainApp.initialize_widgets(App)
And kv file:
Label:
    id: my_label
    text: "default text" 
When I run the program, the label says "default text". When quitting it, I get the error:
Error:
line 5, in initialize_widgets self.root.ids.my_label.text = initial_text AttributeError: type object 'App' has no attribute 'root'
I have tried various workarounds, many quite desperate ones, with no success. Help is much appreciated.
perhaps a dumb question, but do you import APP?
from kivy.app import App
I do, I get the window drawn.

I found App on_start method to be of some use. Looks like its purpose is for profiling, but it can be used for initializing values.