I'm trying to use a rule on my KV language to generate classes on the on, but I always get an error.
<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
What can I do to make this works, if input_type
is equal 'float'
I want my input
class to be a FloatInput
, else a TextInput
.
Such thing isn't possible with kv
lang alone. At least not directly. You have ~4 options:
Set input_type
according to a property of a widget:
TextInput: hint_text: 'int' input_type: 'int' if self.hint_text == 'int' else 'float'
Change input.input_type
property from outside (if the difference is only input type)
<parent>.add_widget(Factory.FloatInput())
on some event, let's say on_release
of a Button
__init__
when building the layout. It's easier than messing around trying to implement something that's not there or looking for the right event to use for adding a widget in kv
. It's more flexible. Although there's probably mentioned in the docs the thing that everything after :
behaves like a casual Python, but that applies to widget properties & events, not to widgets themselves:
bad:
v--rule-- : v------------ not Python -------------v
FloatInput: if input_type == 'float' else TextInput:
good:
TextInput:
text: 'int'
# property: v-------------- Python ---------------v
input_type: 'int' if self.text == 'int' else 'float'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.