如何使用Kivy将TextInput转换为mapview的浮点

2024-10-02 10:30:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我想将textinput转换为float,请

目前我有以下代码:

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)

我已经用mapview1.center_on(46.02,3.02)替换了参数mapview1.center_on(longitude1,latitude1),以测试按钮是否工作正常,是否工作正常,地图是否会随着新闻协调刷新

另一方面,当参数为:mapview1.center_on(longitude1,latitude1)时,我得到以下错误:

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

我不明白如何将TextInput直接转换为Float


Tags: 代码pos参数ontoptextinputfloatcenter
1条回答
网友
1楼 · 发布于 2024-10-02 10:30:00

latitude1是对TextInput{}的引用。实际文本将是latitude1.text。要将其转换为float,请使用float()函数:

float(latitude1.text)

所以你的on_press应该是这样的:

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

相关问题 更多 >

    热门问题