Properties 为什么ConfigParserProperty仅适用于以下示例中使用kv创建的小部件

Properties 为什么ConfigParserProperty仅适用于以下示例中使用kv创建的小部件,properties,kivy,Properties,Kivy,下面的代码按照我的预期工作。当我在TextInput中输入一个数字并按RETURN键时,该数字将显示在所有3个标签中 #!/usr/bin/python from kivy.app import App from kivy.lang import Builder from kivy.uix.boxlayout import BoxLayout from kivy.properties import ConfigParserProperty Builder.load_string(""" <

下面的代码按照我的预期工作。当我在TextInput中输入一个数字并按RETURN键时,该数字将显示在所有3个标签中

#!/usr/bin/python
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ConfigParserProperty

Builder.load_string("""
<MyBoxLayout>
    TextInput:
        text: "insert Number <RETURN>"
        multiline: False
        on_text_validate: root.numberprop=self.text
    Label:
        text: str(root.numberprop)
    Label:
        text: str(root.numberprop)
    Label:
        text: str(root.numberprop)
""")

class MyBoxLayout(BoxLayout):
    numberprop= ConfigParserProperty(3, 'one', 'two', 'app',
                                  val_type=int, errorvalue=41)

class TstApp(App):
    def build_config(self, config):
        config.setdefaults('one', {'two' : '70'})

    def build(self, **kw):
        return MyBoxLayout()

if __name__ == '__main__':
    TstApp().run()
#/usr/bin/python
从kivy.app导入应用程序
从kivy.lang导入生成器
从kivy.uix.boxlayout导入boxlayout
从kivy.properties导入ConfigParserProperty
生成器。加载\u字符串(“”)
文本输入:
正文:“插入编号”
多行:False
在文本上验证:root.numberprop=self.text
标签:
文本:str(root.numberprop)
标签:
文本:str(root.numberprop)
标签:
文本:str(root.numberprop)
""")
类MyBoxLayout(BoxLayout):
numberprop=ConfigParserProperty(3,'一','二','应用',
val_type=int,errorvalue=41)
TstApp类(应用程序):
def build_配置(自身,配置):
config.setdefaults('one',{'two':'70'})
def构建(自身,**千瓦):
返回MyBoxLayout()
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
TstApp().run()
下面的代码没有像我预期的那样工作。当我在Textinput中输入一个数字并按RETURN键时,只有最后一个标签显示该数字

#!/usr/bin/python
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.properties import ConfigParserProperty

def MyConfigParserProperty():
    return ConfigParserProperty(3, 'one', 'two', 'app',
                                  val_type=int, errorvalue=41)

Builder.load_string("""
<MyBoxLayout>
    TextInput:
        text: "insert Number <RETURN>"
        multiline: False
        on_text_validate: root.numberprop=self.text

<MyLabel>
    text: str(root.numberprop)
""")

class MyLabel(Label):
    numberprop=MyConfigParserProperty()

class MyBoxLayout(BoxLayout):
    numberprop=MyConfigParserProperty()

    def __init__(self, **kw):
        super(MyBoxLayout, self).__init__(**kw)
        for i in range(3):
            self.add_widget(MyLabel())

class TstApp(App):
    def build_config(self, config):
        config.setdefaults('one', {'two' : '70'})

    def build(self, **kw):
        return MyBoxLayout()

if __name__ == '__main__':
    TstApp().run()
#/usr/bin/python
从kivy.app导入应用程序
从kivy.lang导入生成器
从kivy.uix.boxlayout导入boxlayout
从kivy.uix.label导入标签
从kivy.properties导入ConfigParserProperty
def MyConfigParserProperty():
返回ConfigParserProperty(3,'一','二','应用',
val_type=int,errorvalue=41)
生成器。加载\u字符串(“”)
文本输入:
正文:“插入编号”
多行:False
在文本上验证:root.numberprop=self.text
文本:str(root.numberprop)
""")
类别MyLabel(标签):
numberprop=MyConfigParserProperty()
类MyBoxLayout(BoxLayout):
numberprop=MyConfigParserProperty()
def初始功率(自身功率,**kw):
超级(MyBoxLayout,self)。\uuuu初始功率(**kw)
对于范围(3)中的i:
self.add_小部件(MyLabel())
TstApp类(应用程序):
def build_配置(自身,配置):
config.setdefaults('one',{'two':'70'})
def构建(自身,**千瓦):
返回MyBoxLayout()
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
TstApp().run()

我需要一种动态创建标签的方法。我该怎么做呢?

下面的代码解决了这个问题

#!/usr/bin/python
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.properties import ConfigParserProperty

Builder.load_string("""
<MyBoxLayout>
    TextInput:
        text: "insert Number <RETURN>"
        multiline: False
        on_text_validate: app.numberprop=self.text

<MyLabel>
    text: str(app.numberprop)
""")

class MyLabel(Label):
    pass

class MyBoxLayout(BoxLayout):

    def __init__(self, **kw):
        super(MyBoxLayout, self).__init__(**kw)
        for i in range(3):
            self.add_widget(MyLabel())

class TstApp(App):
    numberprop=ConfigParserProperty(3, 'one', 'two', 'app',
                                  val_type=int, errorvalue=41)

    def build_config(self, config):
        config.setdefaults('one', {'two' : '70'})

    def build(self, **kw):
        return MyBoxLayout()

if __name__ == '__main__':
    TstApp().run()
#/usr/bin/python
从kivy.app导入应用程序
从kivy.lang导入生成器
从kivy.uix.boxlayout导入boxlayout
从kivy.uix.label导入标签
从kivy.properties导入ConfigParserProperty
生成器。加载\u字符串(“”)
文本输入:
正文:“插入编号”
多行:False
在文本上验证:app.numberprop=self.text
文本:str(app.numberprop)
""")
类别MyLabel(标签):
通过
类MyBoxLayout(BoxLayout):
def初始功率(自身功率,**kw):
超级(MyBoxLayout,self)。\uuuu初始功率(**kw)
对于范围(3)中的i:
self.add_小部件(MyLabel())
TstApp类(应用程序):
numberprop=ConfigParserProperty(3,'一','二','应用',
val_type=int,errorvalue=41)
def build_配置(自身,配置):
config.setdefaults('one',{'two':'70'})
def构建(自身,**千瓦):
返回MyBoxLayout()
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
TstApp().run()

根目录
是kv规则的本地目录;在MyLabel规则中,它指的是MyLabel,而不是父MyBoxLayout。是的,但是MyLabel和MyBoxLayout都有相同的数字Op,所以这不重要。它也适用于最后一个MyLabel。为什么不使用前两个MyLabel?哦,这与从函数返回ConfigParserProperty有关,这是个坏主意。但我不确定这到底是为什么会导致问题。显然,您可以通过不这样做并直接声明来解决它。