详情请参阅说明和示例。
TextInput-输入过滤器
使用input_filter: 'float'。使用input_filter,您不必检查方法中的“init”或“float”。input_filter
Filters the input according to the specified mode, if not None. If
None, no filtering is applied.
input_filter is an ObjectProperty and defaults to None. Can be one of
None, ‘int’ (string), or ‘float’ (string), or a callable. If it is
‘int’, it will only accept numbers. If it is ‘float’ it will also
accept a single period. Finally, if it is a callable it will be called
with two parameters; the string to be added and a bool indicating
whether the string is a result of undo (True). The callable should
return a new substring that will be used instead.
kv文件
定义了两个根
在kv文件中,为同一根小部件定义了根规则AddKalibrasieForm:和类规则:。因为您没有使用def build()方法,所以请删除kv文件中的类规则:。
文本输入-音量框
添加以下内容:multiline: False # disable multiline
hint_text: "Slegs nommers" # Numbers only
input_filter: "float"
Python代码class AddKalibrasieForm(BoxLayout):
calculated_results = ObjectProperty(None)
val_lewerha = ObjectProperty(None)
def convert_calvariables(self):
if len(self.val_lewerha.text) > 0: # if text is not empty
print(format(self.val_lewerha.text))
else:
print("Error: Empty string")To declare properties, you must declare them at the class level.
示例
在主.py在
^{pr2}$
在校准.kv在#:kivy 1.11.0
AddKalibrasieForm: # root rule
orientation: "vertical"
val_lewerha: volha_box
calculated_results: calculated_results_table
BoxLayout:
height: "40dp"
size_hint_y: None
Label:
text: "Lewering per ha"
size_hint_x: 25
TextInput:
id: volha_box # TextInput object name
size_hint_x: 50
hint_text: "Slegs nommers"
multiline: False
input_filter: "float"
Label:
text: "liter"
size_hint_x: 25
text_size: self.size
halign: "left"
BoxLayout:
height: "60dp"
size_hint_y: None
Label:
size_hint_x: 35
Button:
text: "Bereken"
size_hint_x: 30
on_press: root.convert_calvariables() # method called on_press
Label:
size_hint_x: 35
ListView:
id: calculated_results_table
table_items: []
输出