以下代码可以正常运行。当我在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()
我需要一种动态创建标签的方法。我该怎么办?
答案 0 :(得分:0)
以下代码解决了这个问题。
#!/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()