为什么我的 wxPython 程序不工作? (里面的代码)

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

所以我要写一个BMI计算器。我无法让它工作。

在我添加 class 之前,它工作正常但为了使计算按钮工作,我不得不使用 class。一旦我做到了,它就坏了。

你能告诉我我做错了什么吗?

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()

呃,问题是为什么这一切都有效...

问题(可能不完整):

  1. 没有正确初始化 class BMI(参见 a tutorial 如何正确使用 wx.Frame class)
  2. 使用 SpinCtrl 要么不允许输入以米为单位的身高,要么将人们的身高限制为 100 厘米(顺便说一句,体重不得超过 100 公斤)。改为使用 TextCtrl 并用 float 解析值(或修改 SpinCtrl 的 range/resolution)
  3. 您对对象属性执行 self.weigth.GetValue(),但用户输入转到 self.weightset
  4. 错误的 BMI 公式(在 wiki 上查找),链接到错误的单位

您需要解决 class 的 __init__
使用 spinctrl 值而不是文本对象。
将重量定义为浮点数或说明要输入的值以厘米为单位。
公式是体重(Kgs)/(身高(M)*身高)除非你想引发过多不必要的速成饮食;)
您可能想要添加在公制和英制值之间进行选择的功能

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()