Python Forum
[Kivy] Initialize widget value from Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Kivy] Initialize widget value from Python
#1
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.
Reply
#2
perhaps a dumb question, but do you import APP?
from kivy.app import App
Reply
#3
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGUI] Python 3.8.1 Tkinter Widget stete change sasiap 4 2,468 Feb-14-2020, 10:38 AM
Last Post: sasiap

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020