2016-03-17 34 views
0

我正在使用wxGlade進行簡單的GUI計算。AttributeError:'MyFrame'對象沒有屬性'sommation'

通過wxGlade創建GUI並生成代碼後,我修改並輸入了一些特定的代碼,以便像它這樣的計算函數應該是。

簡而言之,用戶需要選擇它們是增加,減少或乘以用戶在開始時輸入的值,方法是選擇代表用戶所需任務的單選按鈕。

通過選擇按鈕添加,用戶將選擇添加任務。下面是代碼:

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

的問題是,我一直具有屬性的錯誤的問題,引用的標題。

以下是錯誤: error message

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

'MyFrame'有方法'add(self,event)'而不是'addition(...)'。你是否將錯誤的方法與事件聯繫起來? – Chickenmarkus

+0

實際上,addition是我創建的一個變量,用於表示用戶想要添加 – maximus

+0

在上面發佈的代碼中,屬性'addition'既沒有被定義也沒有被引用,也沒有異常回溯,該屬性是預期的。請至少發佈我們可以看到發生異常的回溯。 – Chickenmarkus

回答

0

你,如果有人輸入任何數字前點擊計算按鈕得到這個錯誤;因爲你只是在你的按鈕處理程序中創建變量。

爲避免這種情況,請將self.sommation = None添加到__init__方法中,以便它具有默認值。

+0

Yerp,我只是說這個問題。謝謝您的幫助 – maximus