user3437022
user3437022

Reputation: 97

Kivy - Error: "object has no attribute" when I called it from .kv file

When I run the next part of code:

class VState(State):

def __init__(self, name='', stateType=None, **kwargs):
    super(VState, self).__init__(**kwargs)      
    self.vBackground = 'my_background'
    self.name = name
    
def setBackgroundImage(self):

  print (self.vBackground)

  return 'gui/my_background_image'

it will be done. When I call setBackgroundImage() method from .kv file, I get an error: "AttributeError: 'VState' object has no attribute 'vBackground'"

.kv file:

...

source: 'atlas://' + root.setBackgroundImage()

but when I run code above without referencing to any attribute, it will be done again... Without line

print (self.vBackground)

it will be done. Why I can't refer to any attributes from kv file?

Thanks for something ideas...

Upvotes: 3

Views: 3601

Answers (1)

inclement
inclement

Reputation: 29450

The kv is first evaluated during the widget __init__, which in this case happens in your super call before you set self.vBackground.

You can instead change the order to

self.vBackground = 'my_background'
super(VState, self).__init__(**kwargs) 

It may be even better to use a StringProperty.

Upvotes: 2

Related Questions