'ValueError: could not convert string to float: '<kivy.uix.textinput.TextInput object at 0x0000011D49678430>'
I'm new at python and right now i try to make similar calculator with user input. I rewrote my code but its still dont work fine. Cant convert string property to a float. All time i just get "ValueError: could not convert string to float: '<kivy.uix.textinput.TextInput object at 0x0000011D49678430>'" Can someone help me with fix this issue and give me some advices? My code :
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.properties import NumericProperty
class calculate(GridLayout):
def __init__(self, **kwargs):
super(calculate, self).__init__(**kwargs)
self.cols = 2
self.rows = 5
self.label1 = Label(text='Width in mm : ')
self.add_widget(self.label1)
self.user_data1 = TextInput(multiline=False)
self.add_widget(self.user_data1)
self.label2 = Label(text='Height in mm : ')
self.add_widget(self.label2)
self.user_data2 = TextInput(multiline=False)
self.add_widget(self.user_data2)
self.label3 = Label(text='Mass in gr : ')
self.add_widget(self.label3)
self.user_data3 = TextInput(multiline=False)
self.add_widget(self.user_data3)
self.label4 = Label(text='Mass in kg : ')
self.add_widget(self.label4)
self.user_data4 = TextInput(multiline=False)
self.add_widget(self.user_data4)
self.button = Button(text='Calculate')
self.button.bind(on_press=self.on_user_press)
self.add_widget(self.button)
self.calculate = Label(text='')
self.add_widget(self.calculate)
def on_user_press(self, event):
s_kg = str(self.user_data4)
s_width = str(self.user_data1)
s_height = str(self.user_data2)
s_gr = str(self.user_data3)
f_kg = float(s_kg)
f_width = float(s_width)
f_height = float(s_height)
f_gr = float(s_gr)
value = f_kg / (f_width / 1000) / (f_height / 1000) / (f_gr / 1000)
string_value = str(value)
self.calculate.text = string_value
class Calculator(App):
def build(self):
return calculate()
if __name__ == "__main__":
Calculator().run()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|