Search code examples
pythonkivykivy-language

How to convert a TextInput to a float for a mapview with Kivy


I want to convert a textinput to float please..

Currently i've this code :

TextInput:
    id: latitude1
    multiline: False
    pos_hint: {"x":0.16, "top":0.5}
    input_filter:"float"

.....

Button:
    pos_hint: {"x":0.08, "top":0.8}
    text: "Modify map"
    on_press: mapview1.center_on(longitude1,latitude1)

I have already replaced the parameters mapview1.center_on(longitude1,latitude1) by mapview1.center_on(46.02,3.02) in order to test if the button works well, and it works perfectly and the map refresh with the news coords!

On the other hand, when the parameters are like: mapview1.center_on(longitude1,latitude1), I get this error:

File "kivy\weakproxy.pyx", line 70, in kivy.weakproxy.WeakProxy.__richcmp__
TypeError: '>' not supported between instances of 'TextInput' and 'float'.

I don't understand how to convert TextInput directly to Float.


Solution

  • The latitude1 is a reference to the TextInput Widget. The actual text will be latitude1.text. To convert it to a float, use the float() function:

    float(latitude1.text)
    

    So your on_press should be something like:

    on_press: mapview1.center_on(float(longitude1.text),float(latitude1.text))