Python Kivy,KV语言上的动态类
我试图在我的KV语言上使用一个规则来生成on上的类,但我总是会得到一个错误Python Kivy,KV语言上的动态类,python,python-3.x,kivy,kivy-language,Python,Python 3.x,Kivy,Kivy 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'
<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
:
方向:“垂直”
消息标签:消息
用户输入:输入
标签:
id:消息
text:root.message\u发送给用户
FloatInput:如果输入类型=='float'其他文本输入:
id:输入
焦点:正确
如果
input\u type
等于'float'
我想让我的input
类成为FloatInput
,否则TextInput
单凭kv
lang是不可能的。至少不是直接的。您有4种选择:
input\u type
:
TextInput:
hint_text: 'int'
input_type: 'int' if self.hint_text == 'int' else 'float'
input.input\u type
属性(如果差异只是输入类型)按钮的释放时添加
\uuuuu init\uuuu
中。这比在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'
好:
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'
这样的事情单靠
kv
lang是不可能的。至少不是直接的。您有4种选择:
input\u type
:
TextInput:
hint_text: 'int'
input_type: 'int' if self.hint_text == 'int' else 'float'
input.input\u type
属性(如果差异只是输入类型)按钮的释放时添加
\uuuuu init\uuuu
中。这比在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'
好:
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'