Trouble connecting objects in .kv file to python class

Viewed 384

I am learning Kivy and having trouble connecting my objects declared in the .kv file to the python class in order to update their properties. No matter which way I try I get this error:

self.kbCompressionLabel.text = 'Hello World'
AttributeError: 'NoneType' object has no attribute 'text'

The app loads all the kivy files fine, and only breaks when I try to update from the Class.

I've dumbed down the current code to the minimum to illustrate how it is setup. Any help is much appreciated.

Main app entry

import kivy
kivy.require('1.10.0')

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

Builder.load_file('appscreenmanager.kv')
Builder.load_file('compressorscreen.kv')
Builder.load_file('slidersview.kv')

class AppScreenManager(ScreenManager):
    pass

class AppManager(App):
    def build(self):
        return AppScreenManager()

if __name__ == "__main__":
    AppManager().run()

Dumed down appscreenmanager.kv

#:kivy 1.10.0

<AppScreenManager>:
    CompressorScreen:
    ...

compressorscreen.kv

<CompressorScreen>:
    name: 'compressor'
    GridLayout:
        rows: 4
        cols: 1
    SlidersView:
    ...

This is where the issue is happening: simplified slidersview.kv

#:kivy 1.10.0
#:import slidersview slidersview

<slidersView>:
    cols: 4
    rows: 2
    id: sliders
    kbCompressionLabel: kbCompressionLabel

    Label:
        id: kbCompressionLabel
        text: 'test'

slidersview.py

import kivy
kivy.require('1.10.0')

from kivy.uix.gridlayout import GridLayout
from kivy.properties import ObjectProperty

class SlidersView(GridLayout):
    # properties
    sliders = ObjectProperty(None)
    kbCompressionLabel = ObjectProperty(None)

    def __init__(self, **kwargs):
        self.kbCompressionLabel.text = 'Hello World'
        super(SlidersView, self).__init__(**kwargs)

UPDATE

I had to add a delay into the init function then things worked. However, this feels pretty wonky to me. Is this the expected behavior?

Updated slidersview.py

import kivy
kivy.require('1.10.0')

from kivy.clock import mainthread
from kivy.uix.gridlayout import GridLayout
from kivy.properties import ObjectProperty

class SlidersView(GridLayout):
    # properties
    kbCompressionLabel = ObjectProperty(None)

    def __init__(self, **kwargs):
        super(SlidersView, self).__init__(**kwargs)
        @mainthread
        def delayed():
            self.kbCompressionLabel.text = 'Hello World'
        delayed()
1 Answers
Related