gpt4 book ai didi

python - wxPython 中的按钮事件出现错误

转载 作者:太空宇宙 更新时间:2023-11-03 15:23:13 34 4
gpt4 key购买 nike

我面临以下问题 - 每当我尝试为任何按钮创建事件监听器时,我都会收到以下错误:

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

任何帮助将不胜感激!

最佳答案

当您创建 button_compute 时,您将其保留为函数的局部变量。当您尝试绑定(bind)事件时,您将尝试读取尚未创建的实例属性。实际上,您在很多情况下都会这样做,但您的脚本在第一个情况下就会出错。将 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()

关于python - wxPython 中的按钮事件出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43354151/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com