AttributeError : 'MyFrame' object has no attribute 'sommation'

AttributeError : 'MyFrame' object has no attribute 'sommation'

我正在使用 wxGlade 制作一个简单的 GUI 计算器。

通过 wxGlade 创建 GUI 并生成代码后, 我修改并输入了一些特定的代码,以便计算器发挥应有的作用。

简而言之,用户需要通过选择代表用户想要的任务的单选按钮来选择他们是加、减还是乘以用户在开始时输入的值。

通过选择添加按钮,用户将select添加任务。下面是代码:

def add(self, event):  # wxGlade: MyFrame.<event_handler>
        self.sommation = True 
        self.moins = False
        self.multiply = False
        event.Skip()

减法也是一样,

def moins(self, event):  # wxGlade: MyFrame.<event_handler>
        self.moins = True 
        self.sommation = False
        self.multiply = False
        event.Skip()

和乘法

def multiply(self, event):  # wxGlade: MyFrame.<event_handler>
        self.multiply = True
        self.sommation = False
        self.moins = False
        event.Skip()

选择任务后,用户单击名为 calcul 的按钮以获得结果。下面是计算任务的代码:

def calcul(self, event):  # wxGlade: MyFrame.<event_handler>

        if self.sommation == True :
            self.c = int(self.text_ctrl_1.GetValue()) + int(self.text_ctrl_2.GetValue())
            self.somation == False 
        elif self.moins == True : 
            self.c = int(self.text_ctrl_1.GetValue()) - int(self.text_ctrl_2.GetValue())
            self.moins = False 
        elif self.multiply == True :
            self.c = int(self.text_ctrl_1.GetValue()) * int(self.text_ctrl_2.GetValue())
            self.multiply = False
        self.button_1.SetLabel(str(self.c))
        event.Skip()

问题是,我一直遇到标题引用属性错误的问题

错误如下:

p/s这是完整的程序

#!/usr/bin/env python
# -*- coding: CP1252 -*-
#
# generated by wxGlade 0.6.8 (standalone edition) on Thu Mar 17 07:56:19 2016
#

import wx

# begin wxGlade: dependencies
import gettext
# end wxGlade

# begin wxGlade: extracode
# end wxGlade


class MyFrame(wx.Frame):

    def __init__(self, *args, **kwds):
        # begin wxGlade: MyFrame.__init__
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.radio_btn_1 = wx.RadioButton(self, wx.ID_ANY, _("addition\n"), style=wx.RB_GROUP)
        self.text_ctrl_1 = wx.TextCtrl(self, wx.ID_ANY, "")
        self.radio_btn_2 = wx.RadioButton(self, wx.ID_ANY, _("subtraction"))
        self.text_ctrl_2 = wx.TextCtrl(self, wx.ID_ANY, "")
        self.label_1 = wx.StaticText(self, wx.ID_ANY, _("="), style=wx.ALIGN_CENTRE)
        self.button_2 = wx.Button(self, wx.ID_ANY, "")
        self.radio_btn_3 = wx.RadioButton(self, wx.ID_ANY, _("multiply"))
        self.button_1 = wx.Button(self, wx.ID_ANY, _("calcul"))

        self.__set_properties()
        self.__do_layout()

        self.Bind(wx.EVT_RADIOBUTTON, self.add, self.radio_btn_1)
        self.Bind(wx.EVT_RADIOBUTTON, self.moins, self.radio_btn_2)
        self.Bind(wx.EVT_RADIOBUTTON, self.multiply, self.radio_btn_3)
        self.Bind(wx.EVT_BUTTON, self.calcul, self.button_1)
        # end wxGlade

    def __set_properties(self):
        # begin wxGlade: MyFrame.__set_properties
        self.SetTitle(_("frame_1"))
        self.SetFont(wx.Font(20, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, ""))
        self.label_1.SetFont(wx.Font(16, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, ""))
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: MyFrame.__do_layout
        sizer_1 = wx.GridSizer(3, 5, 0, 0)
        sizer_1.Add((0, 20), 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL | wx.SHAPED, 0)
        sizer_1.Add(self.radio_btn_1, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL, 0)
        sizer_1.Add((20, 20), 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL | wx.SHAPED, 0)
        sizer_1.Add((20, 20), 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL | wx.SHAPED, 0)
        sizer_1.Add((20, 20), 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL | wx.SHAPED, 0)
        sizer_1.Add(self.text_ctrl_1, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL | wx.SHAPED, 0)
        sizer_1.Add(self.radio_btn_2, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL, 0)
        sizer_1.Add(self.text_ctrl_2, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL | wx.SHAPED, 0)
        sizer_1.Add(self.label_1, 0, wx.EXPAND | wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL | wx.SHAPED, 0)
        sizer_1.Add(self.button_2, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL | wx.SHAPED, 0)
        sizer_1.Add((20, 20), 0, wx.ALIGN_BOTTOM | wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL | wx.SHAPED, 0)
        sizer_1.Add(self.radio_btn_3, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL, 0)
        sizer_1.Add((20, 20), 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL | wx.SHAPED, 0)
        sizer_1.Add((20, 20), 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL | wx.SHAPED, 0)
        sizer_1.Add(self.button_1, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL | wx.SHAPED, 0)
        self.SetSizer(sizer_1)
        sizer_1.Fit(self)
        self.Layout()
        # end wxGlade

    def add(self, event):  # wxGlade: MyFrame.<event_handler>
        self.sommation = True 
        self.moins = False
        self.multiply = False
        event.Skip()

    def moins(self, event):  # wxGlade: MyFrame.<event_handler>
        self.moins = True 
        self.sommation = False
        self.multiply = False
        event.Skip()

    def multiply(self, event):  # wxGlade: MyFrame.<event_handler>
        self.multiply = True
        self.sommation = False
        self.moins = False
        event.Skip()

    def calcul(self, event):  # wxGlade: MyFrame.<event_handler>

        if self.sommation == True :
            self.c = int(self.text_ctrl_1.GetValue()) + int(self.text_ctrl_2.GetValue())
            self.somation == False 
        elif self.moins == True : 
            self.c = int(self.text_ctrl_1.GetValue()) - int(self.text_ctrl_2.GetValue())
            self.moins = False 
        elif self.multiply == True :
            self.c = int(self.text_ctrl_1.GetValue()) * int(self.text_ctrl_2.GetValue())
            self.multiply = False
        self.button_1.SetLabel(str(self.c))
        event.Skip()

# end of class MyFrame
if __name__ == "__main__":
    gettext.install("app") # replace with the appropriate catalog name

    app = wx.PySimpleApp(0)
    wx.InitAllImageHandlers()
    frame_1 = MyFrame(None, wx.ID_ANY, "")
    frame_1 = MyFrame(None, wx.ID_ANY, "")
    app.SetTopWindow(frame_1)
    frame_1.Show()
    app.MainLoop()

如果有人在输入任何数字之前点击计算按钮,您将收到此错误;因为您只是在按钮处理程序中创建变量。

为避免这种情况,将 self.sommation = None 添加到 __init__ 方法中,使其具有默认值。