I am trying to validate a TextInput in Kivy before doing calculations on the value of the Input before Updating the calculated data to a ListView. But when I test the first TextInput value by printing it out there is nothing, no error and no result. I have referenced the on_press root.calvariable method AddKalibrasieForm Class in my Kivy file, but still nothing. Can someone please give me a clue to what I am doing incorrectly?
Edit: I have noticed what I did wrong: I imported TextInput without declaring the Class (it has been removed) and did not declare the val_lewerha TextInput object in the correct method (fixed it), so it prints to the console. My question has changed to can you validate an users Input on Input? Is this called on_focus? e.g. of what method I think should achieve the desired result:
def validate_input(self):
if isinstance(self.textinput, (int, float)):
accept self.textinput
else:
make self.textinput color red as incorrect data type
2nd Edit: I must have missed it, but another two Stack Overflow Q&A's got me to the correct answer, Stack_Overflow_Answer1; Stack_Overflow_Answer2. I also skimmed past the Kivy Documentation that showed an example of only allowing floating point numbers and one point in a TextInput on inserting text Kivy1.11.0_TextInput_Documentation. So I will be able to solve it. @eyllanesc: I only want to allow a user to insert floating point '0-9' in the TextInput, no strings. Thanks. How do I mark this as answered?
Here is my Python3 code as is:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
class AddKalibrasieForm(BoxLayout):
calculated_results = ObjectProperty()
def convert_calvariables(self):
val_lewerha = ObjectProperty()
not_filled_in = ""
flowha = float(self.val_lewerha.text)
if flowha != not_filled_in and isinstance(flowha, (int, float)):
print(format(self.val_lewerha.text))
else:
print("Error")
class CalibrationApp(App):
pass
if __name__ == '__main__':
CalibrationApp().run()
Here is a part of my Kivy file: calibration.kv
AddKalibrasieForm:
<AddKalibrasieForm>:
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
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: []