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?

Answers


Try cast

str_float = '123.456'
flt_val = float(str_float)

for int

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)

Need Your Help

Boost serialization unregistered class errors

c++ serialization boost c++11

I cannot seem to get boost::serialization to work well. I have sprinkled the class .cpp files with BOOST_CLASS_EXPORT_GUID macros, after including the class and archive headers, but I still get

How to load images from the Documents folder that have been saved dynamically?

iphone

I have problem with loading images from the Documents folder of iPhone application into the tableView.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.