Validate a Kivy TextInput on Input as Data Type Integer Before Accepting Input
我正在尝试在将输入的值更新为ListView之前对Input的值进行计算之前在Kivy中验证TextInput。但是,当我通过打印输出来测试第一个TextInput值时,没有任何结果,没有错误,也没有结果。我在Kivy文件中引用了on_press root.calvariable方法AddKalibrasieForm类,但仍然没有。有人可以告诉我我做错了什么吗?
编辑:我注意到我做错了:我导入TextInput时未声明Class(已将其删除),并且未以正确的方法(已修复)声明val_lewerha TextInput对象,因此将其打印到控制台。我的问题已更改为可以验证用户的"输入时输入"功能吗?这叫on_focus吗?例如我认为应该采用哪种方法才能达到预期的结果:
1 2 3 4 5 | def validate_input(self): if isinstance(self.textinput, (int, float)): accept self.textinput else: make self.textinput color red as incorrect data type |
第二次编辑:我一定错过了,但是另外两个堆栈溢出Q
有关详细信息,请参见说明和示例。
TextInput-input_filter
使用
input_filter
1 input_filterFilters 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文件中,您为同一个根窗口小部件定义了根规则
TextInput-volha_box
添加以下内容:
-
multiline: False # disable multiline -
hint_text:"Slegs nommers" # Numbers only -
input_filter:"float"
Python代码
1 2 3 4 5 6 7 8 9 | 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.
例
main.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | from kivy.app import App from kivy.uix.boxlayout import BoxLayout from kivy.properties import ObjectProperty 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") class CalibrationApp(App): pass if __name__ == '__main__': CalibrationApp().run() |
标定
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | #: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: [] |
输出量