-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkivytest22.py
More file actions
61 lines (47 loc) · 2 KB
/
Copy pathkivytest22.py
File metadata and controls
61 lines (47 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import kivy
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
# Configurar a aplicação Kivy
kivy.require('2.0.0')
class IMCCalculatorApp(App):
def build(self):
# Criar o layout da aplicação
layout = BoxLayout(orientation='vertical', spacing=10, padding=10)
# Adicionar o rótulo de título
title_label = Label(text='Calculadora de IMC',
font_size=24,
bold=True)
layout.add_widget(title_label)
# Adicionar a entrada de peso
weight_input = TextInput(hint_text='Peso (kg)')
layout.add_widget(weight_input)
# Adicionar a entrada de altura
height_input = TextInput(hint_text='Altura (m)')
layout.add_widget(height_input)
# Adicionar o rótulo de resultado
result_label = Label(text='', font_size=24)
layout.add_widget(result_label)
# Adicionar o botão de cálculo
calculate_button = Button(text='Calcular', size_hint=(1, 0.5))
calculate_button.bind(on_press=lambda x: self.calculate_imc(weight_input.text, height_input.text, result_label))
layout.add_widget(calculate_button)
# Retornar o layout como o widget principal da aplicação
return layout
def calculate_imc(self, weight, height, result_label):
try:
# Converter os inputs de peso e altura para float
weight = float(weight)
height = float(height)
# Calcular o IMC
imc = weight / (height ** 2)
# Exibir o resultado no rótulo de resultado
result_label.text = f'Seu IMC é: {imc:.2f}'
except ValueError:
# Lidar com erros de conversão de tipo
result_label.text = 'Erro: Peso e altura devem ser números'
# Inicializar e executar a aplicação
if __name__ == '__main__':
IMCCalculatorApp().run()