Kivy: How to convert user input into int or float
When using TextInput in Kivy, everything that is entered is kept as a string, even when the input is restricted to characters that represent floats or ints using input_filter. How can TextInput.text be converted into an actual float or int type so that I can do calculations with it?
str_float = '123.456' flt_val = float(str_float)
int_val = int(str_int)
If the string is invalid integer, then it throws ValueError exception. So,
try: int_val = int(str_int) # or flt_val = float(str_float) except ValueError: # Handle exception.
Why couldn't you just grab the TextInput.text and convert it using float or int? ie text = TextInput.text for an int.. conversion = int(text)
In Kivy the TextInput defaults to unicode. So you will have to do lots of conversions. Mostly back to floats and strings.
entry = float(entry) entry = str(entry)