Sowik
Sowik

Reputation: 104

Why is my wxPython program not working? (Code inside)

So I have to write a BMI calculator. I cannot get it to work tho.

Before I added the class and the methods it was working fine but in order to make the Compute button working I had to use a class. And once I did it broke.

Can you tell me what I am doing wrong?

import wx


class BMI(wx.Frame):

def InitUI(self):
    window = wx.Frame(self, title="wx.SpinCtrl", size=(400, 300))
    panel = wx.Panel(window)
    label = wx.StaticText(panel, label="Body Mass Index", pos=(20, 10))
    self.weight = wx.StaticText(panel, label="weight:", pos=(20, 70))
    self.height = wx.StaticText(panel, label="height:", pos=(20, 140))
    weightset = wx.SpinCtrl(panel, value='0', pos=(100, 70))
    heightset = wx.SpinCtrl(panel, value='0', pos=(100, 140))
    result = wx.StaticText(panel, label="BMI:", pos=(300, 110))
    result2 = wx.StaticText(panel, label=" ", pos=(335, 110))
    computeButton = wx.Button(panel, label='Compute', pos=(40, 200))
    closeButton = wx.Button(panel, label='Close', pos=(250, 200))
    computeButton.Bind(wx.EVT_BUTTON, self.ComBMI)
    closeButton.Bind(wx.EVT_BUTTON, self.OnClose)

def ComBMI(self, e):
    teglo = self.weight.GetValue()
    vis = self.height.GetValue()
    bmi = teglo * (pow(vis, 2))
    self.result2.SetLabel(str(bmi))

def OnClose(self, e):
    self.Close(True)


def main():

 app = wx.App()
 ex = BMI(None)
 ex.Show()
 app.MainLoop()


 if __name__ == '__main__':
      main()

Upvotes: 0

Views: 121

Answers (2)

Rolf of Saxony
Rolf of Saxony

Reputation: 22438

You need to resolve the __init__ of your class.
Use the spinctrl values not the text objects.
Define the weight to be a float or explain the value to be input is in Centimetres.
The formula is weight(Kgs) / (height (M) * height) unless you want to set off a plethora of un-necessary crash diets ;)
You may want to add the ability to choose between Metric and Imperial values

import wx
import wx.lib.agw.floatspin as FS

class BMI(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title='BMI Calculatot')
        panel = wx.Panel(self)
        label = wx.StaticText(panel, label="Body Mass Index", pos=(20, 10))
        weightT = wx.StaticText(panel, label="weight (Kgs):", pos=(20, 70))
        heightT = wx.StaticText(panel, label="height (M):", pos=(20, 140))
        self.weight = wx.SpinCtrl(panel, value='0', min=0, max=500, pos=(100, 70))
        #self.height = wx.SpinCtrl(panel, value='0', min=100, max=250, pos=(100, 140))

        self.height = FS.FloatSpin(panel, -1, min_val=1.00, max_val=2.50, increment=0.01, pos=(100, 140))
        self.height.SetFormat("%f")
        self.height.SetDigits(2)

        resultT = wx.StaticText(panel, label="BMI:", pos=(300, 110))
        self.result = wx.StaticText(panel, label=" ", pos=(335, 110))
        computeButton = wx.Button(panel, label='Compute', pos=(40, 200))
        closeButton = wx.Button(panel, label='Close', pos=(250, 200))
        computeButton.Bind(wx.EVT_BUTTON, self.ComBMI)
        closeButton.Bind(wx.EVT_BUTTON, self.OnClose)

    def ComBMI(self, e):
        teglo = self.weight.GetValue()
        vis = self.height.GetValue()
        bmi = teglo / (pow(vis, 2))
        self.result.SetLabel(str(round(bmi,3)))

    def OnClose(self, e):
        self.Close(True)

if __name__ == '__main__':
    app = wx.App()
    ex = BMI()
    ex.Show()
    app.MainLoop()

enter image description here

Upvotes: 0

nepix32
nepix32

Reputation: 3177

Uhh, question is why is any of this even working...

Issues (probably incomplete):

  1. No proper initializsation of class BMI (see a tutorial how to use a wx.Frame class properly)
  2. Use of SpinCtrl will either not allow entering height in meter or limit people to 100 cm in heigth (and 100 kg in weight, by the way). Use as TextCtrl instead and parse the value with float (or modify the range/resolution of the SpinCtrl)
  3. You do a self.weigth.GetValue() on a object attribute, but user input goes to self.weightset
  4. Wrong BMI formula (look it up on wiki), linked to that wrong units

Upvotes: 1

Related Questions