JanosK
JanosK

Reputation: 1

How to convert textinput to float and back with kivy

I'm new to python and kivy. I'm trying to make a simple calculator program, but the problem is that with textinputs, i am not able to do any calculations and textinput won't convert to string that i could then convert to float, do the calculation and convert it back to string and show it when button is pressed. I've tried to do it in quite many ways, but can't seem to get around this problem. Any suggestion how to do this? And maybe an easier way than what i'm currently trying to do?

I get this error after on_press: "line 47, in calculate f_ROE = float(s_ROE) ValueError: could not convert string to float: '<kivy.uix.textinput.TextInput object at 0x000002D874407C80>'

Here is my code:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.core.window import Window


class Stonks(App):
    def build(self):
        layout = BoxLayout(orientation="vertical", spacing = 10, padding = 40)

        Instruction = Label(text = "Stock value calculator. Input values\nto get the intrinsic value of a stock\nbased on price to book value. Best \nused for value stocks.", bold = True, font_size = "20sp", )

        ROE_Label = Label(text = "What is company ROE?")
        self.ROE_input = TextInput(multiline = False)

        Growth_Label = Label(text = "What is company growth rate per annum?")
        self.Growth_input = TextInput(multiline = False)

        Rorr_Label = Label(text = "What is you rate of required return %?")
        self.Rorr_input = TextInput(multiline = False)

        self.Stock_Value = Button(text = "Stock value €", on_press = self.calculate)
        self.Stock_Value_2 = Label (text = "")

        layout.add_widget(Instruction)
        layout.add_widget(ROE_Label)
        layout.add_widget(self.ROE_input)
        layout.add_widget(Growth_Label)
        layout.add_widget(self.Growth_input)
        layout.add_widget(Rorr_Label)
        layout.add_widget(self.Rorr_input)
        layout.add_widget(self.Stock_Value)
        layout.add_widget(self.Stock_Value_2)
        return layout

    def calculate(self, instance):
        ROE = self.ROE_input
        Growth = self.Growth_input
        Rorr = self.Rorr_input

        s_ROE = str(ROE)
        s_Growth = str(Growth)
        s_Rorr = str(Rorr)

        f_ROE = float(s_ROE)
        f_Growth = float(s_Growth)
        f_Rorr = float(s_Rorr)
        f_value = ((f_ROE) - (f_Growth)) / ((f_Rorr - f_Growth))

        string_value = str(f_value)

        self.Stock_Value_2.text = string_value


Stonks().run()

Upvotes: 0

Views: 158

Answers (1)

Guinther Kovalski
Guinther Kovalski

Reputation: 1909

from the error log, you are passing an instance of an object instead of what you expect.

try to:

print(dir(self.ROE_input))

or

print(self.ROE_input.__dir__())

to look at what attribute of this object you are trying to use.

Upvotes: 0

Related Questions