Mirai
Mirai

Reputation: 135

Why don't the kv file variables accept self?

How can the kv variable of a file refer to the py variable of a file using self?

The fact is that App is inherent only in classes, and self-only in functions.

The kv file variable accepts only app:

'''
MDFlatButton:
    id: flat
    text: app.gg
'''

But I need it to work like this:

class Test(MDApp):
    gg = '123'
MDFlatButton:
    id: flat
    text: self.gg
class Test(MDApp):
    def build(self):
        gg = '123'

to refer to a variable inside a function, you need self, which does not accept kv. Question: how do I make it work and not give an error?

MDFlatButton: 
    id: flat 
    text: self.gg

Or something else, but that the MDFlatButton button takes the text argument from the function.

Help(

Upvotes: 0

Views: 145

Answers (2)

Mirai
Mirai

Reputation: 135

Turn out! This work for me:

from kivymd.app import MDApp
from kivy.lang import Builder
KV = '''
Screen:
    MDFlatButton:
        id: flat
        text: app.gg
'''
class Test(MDApp):
    def build(self):
        self.gg = '555'
        return Builder.load_string(KV)
Test().run()

don't know what i did, but this work

Upvotes: 1

Yoav Glazner
Yoav Glazner

Reputation: 8066

you can use root

MDFlatButton: 
    id: flat 
    text: root.gg

the 2nd mistake is that u didn't set it to self.

class Test(MDApp):
    def build(self):
        self.gg = '123' # <---- was gg = '123'

Upvotes: 0

Related Questions