Advertisement
Koragg

BMI Calculator [Python {wxPython}]

Apr 6th, 2018
563
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.82 KB | None | 0 0
  1. import wx
  2. import time
  3.  
  4. class BMICalculator(wx.Frame):
  5.     def __init__(self, parent, id):
  6.         wx.Frame.__init__(self, parent, id, "BMI Calculator", (600, 800))
  7.         self.panel = wx.Panel(self)
  8.         self.panel.SetBackgroundColour("#FFFFFF")
  9.         self.Centre()
  10.         self.info_text = wx.StaticText(self.panel, -1, "Enter your height and weight and press compute to see your BMI.", (20, 15))
  11.         self.label_text = wx.StaticText(self.panel, -1, "Body Mass Index: ", (20, 50))
  12.         self.result_text = wx.StaticText(self.panel, -1, "... kg/(m*m)", (120, 50))
  13.         self.error_label = wx.StaticText(self.panel, -1, "Error(s): ", (290, 50))
  14.         self.error_text = wx.StaticText(self.panel, -1, "", (269, 65))
  15.         self.static_text_height = wx.StaticText(self.panel, -1, "Height:", (20, 90))
  16.         self.height = wx.SpinCtrlDouble(self.panel, -1, pos = (65, 87), size = (60, -1), min = 0, max = 300)
  17.         self.static_text_height_extra = wx.StaticText(self.panel, -1, "(in centimeters)", (130, 90))
  18.         self.static_text_weight = wx.StaticText(self.panel, -1, "Weight:", (20, 130))
  19.         self.weight = wx.SpinCtrlDouble(self.panel, -1, pos = (65, 127), size = (60, -1), min = 0, max = 300)
  20.         self.static_text_weight_extra = wx.StaticText(self.panel, -1, "(in kilograms)", (130, 130))
  21.         self.bmi_underweight = wx.StaticText(self.panel, -1, "Underweight = < 18.5", (253, 90))
  22.         self.bmi_normal_weight = wx.StaticText(self.panel, -1, "Normal weight = 18.5-24.9", (239, 110))
  23.         self.bmi_overweight = wx.StaticText(self.panel, -1, "Overweight = 25-29.9", (254, 130))
  24.         self.bmi_obesity = wx.StaticText(self.panel, -1, "Obesity = > 30", (274, 150))
  25.         self.button_compute = wx.Button(self.panel, label = "Compute", pos = (40, 170), size = (60, -1))
  26.         self.button_compute.SetBackgroundColour((0, 255, 0))
  27.         self.button_compute.Bind(wx.EVT_BUTTON, self.onCompute)
  28.         self.button_refresh = wx.Button(self.panel, label = "Refresh", pos = (122, 170), size = (60, -1))
  29.         self.button_refresh.SetBackgroundColour((255, 165, 0))
  30.         self.button_refresh.Bind(wx.EVT_BUTTON, self.onRefresh)
  31.         self.button_theme = wx.Button(self.panel, label = "Theme", pos = (203, 170), size = (60, -1))
  32.         self.button_theme.SetBackgroundColour((0, 255, 255))
  33.         self.button_theme.Bind(wx.EVT_BUTTON, self.onThemeChange)
  34.         self.button_cancel = wx.Button(self.panel, label = "Close", pos = (285, 170), size = (60, -1))
  35.         self.button_cancel.SetBackgroundColour((255, 0, 0))
  36.         self.button_cancel.Bind(wx.EVT_BUTTON, self.onClose)
  37.         self.isBlack = False
  38.    
  39.     def onCompute(self, event):
  40.         if (self.compute_BMI(self.height.GetValue(), self.weight.GetValue()) == None):
  41.             self.result_text.SetLabel("... kg/(m*m)")
  42.         else:
  43.             self.result_text.SetLabel(str(self.compute_BMI(self.height.GetValue(), self.weight.GetValue())) + " kg/(m*m)")
  44.         self.button_compute.SetBackgroundColour((0, 0, 255))
  45.         time.sleep(0.1)
  46.         self.button_compute.SetBackgroundColour((0, 255, 0))
  47.    
  48.     def onRefresh(self, event):
  49.         self.result_text.SetLabel("... kg/(m*m)")
  50.         self.error_text.SetLabel("")
  51.         self.height.SetValue(0)
  52.         self.weight.SetValue(0)
  53.        
  54.     def onThemeChange(self, event):
  55.         if (self.isBlack == False):
  56.             self.panel.SetBackgroundColour("Black")
  57.             self.info_text.SetForegroundColour((255, 255, 255))
  58.             self.label_text.SetForegroundColour((255, 255, 255))
  59.             self.result_text.SetForegroundColour((255, 255, 255))
  60.             self.error_label.SetForegroundColour((255, 255, 255))
  61.             self.error_text.SetForegroundColour((255, 255, 255))
  62.             self.static_text_height.SetForegroundColour((255, 255, 255))
  63.             self.static_text_height_extra.SetForegroundColour((255, 255, 255))
  64.             self.static_text_weight.SetForegroundColour((255, 255, 255))
  65.             self.static_text_weight_extra.SetForegroundColour((255, 255, 255))
  66.             self.bmi_underweight.SetForegroundColour((255, 255, 255))
  67.             self.bmi_normal_weight.SetForegroundColour((255, 255, 255))
  68.             self.bmi_overweight.SetForegroundColour((255, 255, 255))
  69.             self.bmi_obesity.SetForegroundColour((255, 255, 255))
  70.             self.button_compute.SetForegroundColour((255, 255, 255))
  71.             self.button_refresh.SetForegroundColour((255, 255, 255))
  72.             self.button_theme.SetForegroundColour((255, 255, 255))
  73.             self.button_cancel.SetForegroundColour((255, 255, 255))
  74.             self.isBlack = True
  75.         elif (self.isBlack == True):
  76.             self.panel.SetBackgroundColour("White")
  77.             self.info_text.SetForegroundColour((0, 0, 0))
  78.             self.label_text.SetForegroundColour((0, 0, 0))
  79.             self.result_text.SetForegroundColour((0, 0, 0))
  80.             self.error_label.SetForegroundColour((0, 0, 0))
  81.             self.error_text.SetForegroundColour((0, 0, 0))
  82.             self.static_text_height.SetForegroundColour((0, 0, 0))
  83.             self.static_text_height_extra.SetForegroundColour((0, 0, 0))
  84.             self.static_text_weight.SetForegroundColour((0, 0, 0))
  85.             self.static_text_weight_extra.SetForegroundColour((0, 0, 0))
  86.             self.bmi_underweight.SetForegroundColour((0, 0, 0))
  87.             self.bmi_normal_weight.SetForegroundColour((0, 0, 0))
  88.             self.bmi_overweight.SetForegroundColour((0, 0, 0))
  89.             self.bmi_obesity.SetForegroundColour((0, 0, 0))
  90.             self.button_compute.SetForegroundColour((0, 0, 0))
  91.             self.button_refresh.SetForegroundColour((0, 0, 0))
  92.             self.button_theme.SetForegroundColour((0, 0, 0))
  93.             self.button_cancel.SetForegroundColour((0, 0, 0))
  94.             self.isBlack = False
  95.         self.panel.Refresh()
  96.        
  97.     def onClose(self, event):
  98.         self.Close(True)
  99.         self.button_cancel.SetBackgroundColour((0, 0, 255))
  100.         time.sleep(0.1)
  101.         self.button_cancel.SetBackgroundColour((255, 0, 0))
  102.    
  103.     def errorZeroDivision(self):
  104.         self.error_text.SetLabel("Division by zero")
  105.    
  106.     def compute_BMI(self, height, weight):
  107.         self.error_text.SetLabel("")
  108.         height_m = float(height) / 100
  109.         if ((height_m * height_m) == 0):
  110.             self.errorZeroDivision()
  111.             return None
  112.         BMI = weight / (height_m * height_m)
  113.         return BMI
  114.    
  115. def main():
  116.     app = wx.App()
  117.     frame = BMICalculator(None, -1)
  118.     frame.Show()
  119.     app.MainLoop()
  120.    
  121. if __name__ == "__main__":
  122.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement