Problem with function

24 views
Skip to first unread message

Leandro Davila

unread,
Mar 9, 2018, 7:41:50 PM3/9/18
to kivy-...@googlegroups.com
Hey guys, I'm trying to learn Kivy by myself and I'm a self-taught Python student, Sorry if my question is stupid. 

I'm making a Body Mass Index Calculator but there is a problem with the calculate() function : 

import kivy 
from kivy.app import App 
from kivy.uix.label import Label 
from kivy.uix.screenmanager import ScreenManager,Screen 
from kivy.lang import Builder 
from kivy.uix.button import Button 
from kivy.properties import StringProperty
from kivy.uix.textinput import TextInput 
from kivy.uix.boxlayout import BoxLayout


Builder.load_string("""
<Input>:
BoxLayout : 
orientation : 'vertical' 
TextInput: 
id : weight 
multiline : False 
text : 'Weight in kg'
TextInput: 
id : height
multiline : False 
text : 'Height in centimeters'
Button : 
text: 'Calculate the result '
on_press: root.calculate()
Label : 
id : result 
text : ''
""")


class DataInput(Screen): 
result = StringProperty('')
        def calculate(self, *args):

result = str(float(self.ids.weight.text) / (float(self.ids.height.text)/100) * (float(self.ids.height.text)/100))
self.ids.result.text = result

class BMIApp(App) : 
def build(self): 
return DataInput()

if __name__ == '__main__':
BMIApp().run()

The function doesn't calculate at all , it just returns the value of the first TextInput as a float. 
What is happening ?

ZenCODE

unread,
Mar 10, 2018, 12:12:37 PM3/10/18
to Kivy users support
Well, it's not returning anything. Note the lack of a "return" statement. But should you kv file not be

<DataInput>:

instead of

<Input>:

?

Leandro Davila

unread,
Mar 10, 2018, 7:41:28 PM3/10/18
to kivy-...@googlegroups.com
I've made some changes before paste the code here to make it more simple to understand , that's why the error in the kivy file, but in the original code it is correct . I've added the return statement after the function before, but it just returned  the first input value, that's why I don't understand whats is happening. 

ZenCODE

unread,
Mar 11, 2018, 6:50:06 AM3/11/18
to Kivy users support
You will need to be more specific. With the code below, if I enter 1 and 2 in the text boxes, the answer is displayed on the screen. What happens on your side? Please give the exact message.

import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.properties import StringProperty
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout

Builder.load_string("""
<DataInput>:

Leandro Davila

unread,
Mar 12, 2018, 1:52:14 PM3/12/18
to kivy-...@googlegroups.com
This is the code I'm working on . The screen manager is there  because I'm thinking to expand the input buttons and add another screens, that's why I have changed the first code, just to make it less confusing. Copy it and try it yourself to see what I'm talking about . The first textinput value is the weight in kg, the second one is the height in centimeters.  The output I'm looking for is the solution of the formula for Body Mass  Index  : result = weight / height * height . Thanks  


import kivy 
from kivy.app import App 
from kivy.uix.label import Label 
from kivy.uix.screenmanager import ScreenManager,Screen 
from kivy.lang import Builder 
from kivy.uix.button import Button 
from kivy.properties import StringProperty
from kivy.uix.textinput import TextInput 
from kivy.uix.boxlayout import BoxLayout


Builder.load_string("""
<Input>:
id : calculator
BoxLayout : 
id : calculator01  
orientation : 'vertical' 
TextInput: 
id : peso 
multiline : False 
text : 'Digite o peso do paciente em Kg'
TextInput: 
id : altura 
multiline : False 
text : 'Digite a altura do paciente em cm'
Button : 
text: 'Calcular o resultado'
on_press: root.calculate(peso.text, altura.text)
##on_release: root.manager.get_screen('resultado').label_resultado.text= 'result'
Label : 
id : result 
text : root.resultado
##<Output>:
##name: 'resultado'
##Label : 
##id : label_resultado
##text :''
""")


class Input(Screen): 
resultado = StringProperty('')
def calculate(self, *args):

resultado = str(float(self.ids.peso.text) / (float(self.ids.altura.text)/100) * (float(self.ids.altura.text)/100))
self.ids.result.text = resultado  
return 0


class Output(Screen):
pass


sm = ScreenManager()
sm.add_widget(Input(name='input'))
sm.add_widget(Output(name='output'))


class BMIApp(App) : 
def build(self): 
return sm

if __name__ == '__main__':
BMIApp().run()

ZenCODE

unread,
Mar 12, 2018, 2:57:11 PM3/12/18
to Kivy users support
Well, the maths seems to be wrong. Try

        resultado = str(
            float(self.ids.peso.text) / (float(self.ids.altura.text) ** 2.0))

Leandro Davila

unread,
Mar 12, 2018, 4:00:31 PM3/12/18
to kivy-...@googlegroups.com
It works perfectly now ! Being a newbie in Python and Kivy,  I don't understand why Python doesn't accept the other expression , since its just multiplication of the same data.  
Thanks for that man ! 
Reply all
Reply to author
Forward
0 new messages