为什么kv文件变量不接受self? [英] Why don't the kv file variables accept self?

查看:68
本文介绍了为什么kv文件变量不接受self?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文件的kv变量如何使用self引用文件的py变量?

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

事实是,App仅在类中是固有的,而在函数中仅是自身的.

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

kv文件变量仅接受应用程序:

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'

要引用函数内的变量,您需要self,而self不接受kv.问题:如何使它正常工作并且不出错?

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

或者其他的东西,但是MDFlatButton按钮从函数中获取文本参数.

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

帮助(

推荐答案

结果!这对我有用:

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

这篇关于为什么kv文件变量不接受self?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆