为什么 ConfigParserProperty 仅适用于以下示例中使用 kv 创建的 Widget
Why does ConfigParserProperty only work for Widgets created with kv in examples below
以下代码按我的预期工作。当我在 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()
下面的代码没有按我预期的那样工作。当我在 Textinput 中输入数字并按 RETURN 时,只有最后一个 Label 显示数字。
#!/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
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()
以下代码按我的预期工作。当我在 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()
下面的代码没有按我预期的那样工作。当我在 Textinput 中输入数字并按 RETURN 时,只有最后一个 Label 显示数字。
#!/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
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()