![](/img/trans.png)
[英]kv Language id linked with another class Object as ObjectProperty kivy
[英]Kivy, dynimic class on KV language
我正试图在我的KV语言上使用规则来生成on,但我总是得到一个错误。
<SimpleInputLayout>:
orientation: 'vertical'
message_label: message
user_input: input
Label:
id: message
text: root.message_to_user
FloatInput: if input_type == 'float' else TextInput:
id: input
focus: True
如果input_type
等于'float'
我该怎么做才能使它工作'float'
我希望我的input
类是FloatInput
,否则是TextInput
。
单独使用kv
lang是不可能的。 至少不是直接的。 你有~4个选择:
根据窗口小部件的属性设置input_type
:
TextInput: hint_text: 'int' input_type: 'int' if self.hint_text == 'int' else 'float'
从外部更改input.input_type
属性(如果差异仅为输入类型)
<parent>.add_widget(Factory.FloatInput())
,比如说Button
on_release
__init__
中使用Python。 比试图实现不存在的东西或寻找用于在kv
添加小部件的正确事件更容易。 它更灵活。 虽然在文档中可能会提到以下所有内容:
行为类似于休闲Python,但这适用于小部件属性和事件,而不是小部件本身:
坏:
v--rule-- : v------------ not Python -------------v
FloatInput: if input_type == 'float' else TextInput:
好:
TextInput:
text: 'int'
# property: v-------------- Python ---------------v
input_type: 'int' if self.text == 'int' else 'float'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.