tutorfree

imc calculator

Jan 31st, 2016 (edited)
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.69 KB | None | 0 0
  1. #!/usr/bin/envi python
  2. # -*- coding: utf-8 -*-
  3. #Author: Antoine Paul
  4. #Date: 2016-JAN-31
  5. #Filename: imc.py
  6.  
  7. try:
  8.     from tkinter import *
  9. except:
  10.     from Tkinter import *
  11.  
  12.  
  13. janela = Tk()
  14. janela.title("IMC Calculator")
  15.  
  16.  
  17. def Limpar():
  18.    
  19.     try:
  20.         peso.delete(0, END)
  21.         altura.delete(0, END)
  22.         lbDivisao["text"] = ""
  23.         defaultbg = janela.cget('bg')
  24.         lbP1.configure(bg = defaultbg)
  25.         lbP2.configure(bg = defaultbg)
  26.         lbP3.configure(bg = defaultbg)
  27.         lbP4.configure(bg = defaultbg)
  28.         lbP5.configure(bg = defaultbg)
  29.         lbP6.configure(bg = defaultbg)
  30.         lbP7.configure(bg = defaultbg)
  31.  
  32.     except ValueError:
  33.         pass
  34.  
  35. def campo_vazio(peso, altura):
  36.    
  37.     if Entry.get(peso) == '' and Entry.get(altura) == '':
  38.         return True
  39.     else:
  40.         return False
  41.  
  42. def e_numero(peso, altura):
  43.    
  44.     if Entry.get(peso).replace(',', '', 1).isdigit() == True and \
  45.        Entry.get(altura).replace(',', '', 1).isdigit() == True:
  46.         return True
  47.     else:
  48.         return False
  49.  
  50.  
  51. def Calcular():
  52.  
  53.         cor = ['Aquamarine', 'MediumAquamarine', 'LightGreen', 'yellow',
  54.                'salmon', 'Tomato', 'red']
  55.        
  56.         if campo_vazio(peso, altura) == False and e_numero(peso, altura) == True:
  57.            
  58.             p = float(Entry.get(peso).replace(',', '.', 1))
  59.             a = float(Entry.get(altura).replace(',', '.', 1))
  60.             imc = float('%.2f' %(p / a**2))
  61.  
  62.             lbDivisao["text"] = str(imc).replace('.', ',', 1)
  63.             if imc <= 17:
  64.                 lbP1["bg"] = cor[0]
  65.             elif 17 < imc <=18.49:
  66.                 lbP2["bg"] = cor[1]
  67.             elif 18.5 <= imc <=24.99:
  68.                 lbP3["bg"] = cor[2]
  69.             elif 25 <= imc <=29.99:
  70.                 lbP4["bg"] = cor[3]
  71.             elif 30 <= imc <= 34.99:
  72.                 lbP5["bg"] = cor[4]
  73.             elif imc >= 35 and imc <=39.99:
  74.                 lbP6["bg"] = cor[5]
  75.             else:
  76.                 lbP7["bg"] = cor[6]
  77.                 lbP7["fg"] = cor[3]
  78.         else:
  79.             lbDivisao["text"] = "Campo(s) Vazio(s) ou fora do padrão!"
  80.  
  81. if __name__ == "__main__":
  82.    
  83.     lbPeso = Label(janela, text="Peso: (Ex: 82,1)")
  84.     lbPeso.place(x=50, y=40)
  85.    
  86.     lbAltura = Label(janela, text="Altura: (Ex: 1,72)")
  87.     lbAltura.place(x=50, y=70)
  88.    
  89.     peso = Entry(janela)
  90.     peso.place(x=145, y=40, width=185)
  91.     peso.focus_set()
  92.    
  93.     altura = Entry(janela)
  94.     altura.place(x=145, y=70, width=185)
  95.    
  96.     btCalcular = Button(janela, text="Calcular", width=7, command=Calcular)
  97.     btCalcular.place(x=110, y=110)
  98.    
  99.     btLimpar = Button(janela, text="Limpar", width=7, command=Limpar)
  100.     btLimpar.place(x=213, y=110)
  101.    
  102.     lbDivisao = Label(janela, text="", foreground="blue", font="-weight bold")
  103.     lbDivisao.place(x=50, y=150)
  104.    
  105.     lbP1 = Label(janela, text="Abaixo de 17 - Muito abaixo do peso ideal")
  106.     lbP1.place(x=50, y=180)
  107.    
  108.     lbP2 = Label(janela, text="Entre 17 e 18,49 - Abaixo do peso ideal")
  109.     lbP2.place(x=50, y=200)
  110.    
  111.     lbP3 = Label(janela, text="Entre 18,5 e 24,99 - Peso normal")
  112.     lbP3.place(x=50, y=220)
  113.    
  114.     lbP4 = Label(janela, text="Entre 25 e 29,99 - Acima do peso")
  115.     lbP4.place(x=50, y=240)
  116.    
  117.     lbP5 = Label(janela, text="Entre 30 e 34,99 - Obesidade (nível I)")
  118.     lbP5.place(x=50, y=260)
  119.    
  120.     lbP6 = Label(janela, text="Entre 35 e 39,99 - Obesidade severa (nível II)")
  121.     lbP6.place(x=50, y=280)
  122.    
  123.     lbP7 = Label(janela, text="Acima de 39,99 - Obesidade mórbida (nível III)")
  124.     lbP7.place(x=50, y=300)
  125.    
  126.     janela.geometry("390x340+200+200")
  127.     janela.mainloop()
  128.  
Add Comment
Please, Sign In to add comment