![](/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.