在Python中为TextInput分配变量值(Kivy)

时间:2013-12-17 21:10:00

标签: android python kivy

在使用Kivy库中的TextInput时,我在为变量赋值时遇到问题。

首先,我试图在Kivy上创建一个租赁计算器。我已经创建了计算器,但现在我尝试将其转换为Kivy框架,以便我可以在Android设备上使用它。

我试图减去两个值(租约和里程数)时遇到困难。我试过了:
milesleft = int(lease) - int(mileage)但它告诉我" int()参数必须是字符串或数字,而不是' TextInput'"

我很困惑,一直在寻找解决方案。请提供任何帮助或建议!


leaseapp.py

import kivy
kivy.require('1.7.2')

from datetime import datetime, timedelta
import time

from kivy.core.window import Window
from kivy.uix.textinput import TextInput
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.scatter import Scatter
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.config import Config
from kivy.base import runTouchApp

if __name__ == '__main__':

root = BoxLayout(orientation='vertical', padding=20, spacing=10)


lease = TextInput(multiline=False, hint_text="Lease allowance per year", input_type='number')
lease.add_widget(TextInput(size_hint=(1, None)))
root.add_widget(lease)

mileage = TextInput(multiline=False, hint_text="Current mileage", input_type='number')
mileage.add_widget(TextInput(size_hint=(1, None)))
root.add_widget(mileage)

milesleft = int(lease) - int(mileage)

submitbutton = Button(text='Submit')
root.add_widget(submitbutton)

runTouchApp(root)

1 个答案:

答案 0 :(得分:2)

尝试

milesleft = int(lease.text) - int(mileage.text)