【问题标题】:Getting an error on button events in wxPythonwxPython中的按钮事件出错
【发布时间】:2017-09-07 07:49:38
【问题描述】:

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

    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 我在该主题上找到的快速教程。 (很多丢失的“自我”。)

标签: python events wxpython


【解决方案1】:

当您创建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()

【讨论】:

  • 感谢您的回复。我尝试了您的解决方案,但我不断收到相同的错误:/
  • 当我注释掉这两个按钮时程序运行
  • @SamuelLJackson 那是因为你会跳过所有使用这些值的地方,但你想使用它们对吗?
  • @SamuelLJackson 我强烈建议对类与实例属性和 python 命名空间组织进行一些阅读。我已经编辑了我认为需要实例属性的所有地方,但我无法测试,因为我有 Qt 而不是 wx
  • 如果您创建为局部变量,小部件将照常创建,但稍后您将无法访问它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-15
  • 2012-11-08
  • 2015-02-03
  • 2014-06-29
  • 2017-11-05
  • 2011-06-25
  • 1970-01-01
相关资源
最近更新 更多