繁体   English   中英

Kivy,KV语言的dynimic课程

[英]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个选择:

  1. 根据窗口小部件的属性设置input_type

     TextInput: hint_text: 'int' input_type: 'int' if self.hint_text == 'int' else 'float' 
  2. 从外部更改input.input_type属性(如果差异仅为输入类型)

  3. 在某些事件上动态添加正确的小部件,例如<parent>.add_widget(Factory.FloatInput()) ,比如说Button on_release
  4. 在构建布局时,尤其是在__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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM