How to call a variable in .kv kivymd

狂风中的少年 提交于 2020-03-06 09:34:32

问题


I have a problem, that when i want to call a variable in the .kv gave me an error

is it posible to call a variable like this:

    FloatLayout:
    MDCard:
        orientation: "vertical"
        size_hint: .43, .3
        height: self.minimum_height
        pos_hint: {"x": .05, "y": .35}

    MDLabel:
        id: card
        text: self.data_ebay          #here is the variable i want to be the text

The variable has a text but when i run it, it gave me this error:

 AttributeError: 'MDLabel' object has no attribute 'app'
 File "E:\pythonf2\lib\site-packages\kivy\lang\builder.py", line 249, in create_handler
 return eval(value, idmap), bound_list
 File "<string>", line 28, in <module>
 File "kivy\weakproxy.pyx", line 32, in kivy.weakproxy.WeakProxy.__getattr__

 File "E:\pythonf2\lib\site-packages\kivy\lang\builder.py", line 692, in _apply_rule
 rctx['ids'])
 File "E:\pythonf2\lib\site-packages\kivy\lang\builder.py", line 254, in create_handler
 cause=tb)

回答1:


You should use 'app.variable' if the variable in the App class or 'root.variable' if it's in the class that contains this widget. I suppose in your case it would be:

    MDLabel:
        id: card
        text: root.data_ebay


来源:https://stackoverflow.com/questions/60160749/how-to-call-a-variable-in-kv-kivymd

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!