I am making a simple GUI calculateur by using wxGlade.
After creating the GUI through wxGlade and having the code generated, I modified and entered some particular code so that the calculateur functions like it should be.
Briefly, user need to choose either they are adding, subtracting or multiplying both of the value, entered by the user at the begining, by choosing the radio button that represents the task that the user want.
By choosing the button addition, the user will select the addition task. Below is the code:
def add(self, event): # wxGlade: MyFrame.<event_handler>
self.sommation = True
self.moins = False
self.multiply = False
event.Skip()
The same goes to the subtracting,
def moins(self, event): # wxGlade: MyFrame.<event_handler>
self.moins = True
self.sommation = False
self.multiply = False
event.Skip()
and the multiplication
def multiply(self, event): # wxGlade: MyFrame.<event_handler>
self.multiply = True
self.sommation = False
self.moins = False
event.Skip()
after choosing the task, the user click on a button called calcul to have the results. below is the code for the calcul task :
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()
The problem is that, I keep having the problem of the attribute error cited on the title.
p/s this is the full program
#!/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()
You'll get this error if someone clicks the calculate button before entering any numbers; since you are only creating the variable in your button handlers.
To avoid this, add self.sommation = None
to the __init__
method, so it has a default value.