wxpython 按钮事件_在wxPython中获取按钮事件的错误

创建button_compute时,将其作为函数的局部变量。当尝试绑定事件时,然后尝试从尚未创建的实例属性中读取。实际上,在很多情况下您都会这样做,但是您的脚本在第一种情况下出错了。将self.xxx添加到赋值中,使实例属性而不是局部变量成为函数。在import wx

class BMICalculator(wx.Frame):

def __init__(self, parent, id):

wx.Frame.__init__(self, parent, id, "BMI calculator", (600, 800))

self.panel = wx.Panel(self)

self.Centre()

#Creating the buttons and text fields

self.static_text_height = wx.StaticText(self.panel, -1, "Height", (170, 10))

self.height = wx.SpinCtrl(self.panel, -1, pos=(164, 40), size=(60, -1))

self.static_text_weight = wx.StaticText(self.panel, -1, "Weight", (170, 100))

self.weight = wx.SpinCtrl(self.panel, -1, pos=(164, 130), size=(60, -1))

self.button_compute = wx.Button(self.panel, label="Compute", pos=(110, 180), size=(60, -1))

self.button_cancel = wx.Button(self.panel, label="Cancel", pos=(210, 180), size=(60, -1))

self.result_text = wx.StaticText(self.panel, -1, "Enter your height and weight and press compute", (68, 220))

#Adding the events for the buttons (Where I get the error)

self.button_compute.Bind(wx.EVT_BUTTON, self.on_click)

self.button_cancel.Bind(wx.EVT_CLOSE, self.click_close)

def compute_BMI(height, weight):

#BMI = x KG / (y M * y M)

height_m = height/100

BMI = weight/(height_m * height_m)

return BMI

def click_close(self, event):

self.Close(True)

def on_click(self, event):

self.result_text.SetValue(self.compute_BMI(self.height.GetValue(), self.weight.GetValue()))

def main():

app = wx.App()

frame = BMICalculator(None, -1)

frame.Show()

app.MainLoop()

#enter code here

if __name__ == '__main__':

main()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值