我面临着以下问题 - 每当我试图让一个事件侦听器的任何我的按钮,我得到了以下错误:获取按钮事件的错误在wxPython中
self.button_compute.Bind(wx.EVT_BUTTON, self.on_click)
AttributeError: 'BMICalculator' object has no attribute 'button_compute'
我尝试了建议的解决方案在这个线程:AttributeError: 'module' object has no attribute 'PyScrolledWindow' in wxPython,但它不适合我。
程序是计算BMI和下面的代码:
import wx
class BMICalculator(wx.Frame):
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id, "BMI calculator", (600, 800))
panel = wx.Panel(self)
self.Centre()
#Creating the buttons and text fields
static_text_height = wx.StaticText(panel, -1, "Height", (170, 10))
height = wx.SpinCtrl(panel, -1, pos=(164, 40), size=(60, -1))
static_text_weight = wx.StaticText(panel, -1, "Weight", (170, 100))
weight = wx.SpinCtrl(panel, -1, pos=(164, 130), size=(60, -1))
button_compute = wx.Button(panel, label="Compute", pos=(110, 180), size=(60, -1))
button_cancel = wx.Button(panel, label="Cancel", pos=(210, 180), size=(60, -1))
result_text = wx.StaticText(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):
result_text.SetValue(compute_BMI(height.GetValue(), weight.GetValue()))
def main():
app = wx.App()
frame = BMICalculator(None, -1)
frame.Show()
app.MainLoop()
enter code here
if __name__ == '__main__':
main()
任何帮助将不胜感激!
看起来大部分错误都可以通过更好地理解类属性和实例属性来清除。 [Here's](http://www.python-course.eu/python3_class_and_instance_attributes.php)关于这个主题的快速教程。 (很多失踪的“自我”) – Aaron