Search code examples
pythonpython-3.xkivykivy-languagekivymd

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(


Solution

  • 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