By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,222 Members | 2,374 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,222 IT Pros & Developers. It's quick & easy.

wx Python problem; attribute error

P: 3
im try to activate a tutorial code for wxPython,
the calculator.py...but it gives me the next message

File "C:\Users\AOFU\Desktop\A-PYTHON\calculator.py", line 184, in <module>
app = MyApp(0)
File "C:\Python27\lib\site-packages\wx-2.8-msw-unicode\wx\_core.py", line 7978, in __init__
self._BootstrapApp()
File "C:\Python27\lib\site-packages\wx-2.8-msw-unicode\wx\_core.py", line 7552, in _BootstrapApp
return _core_.PyApp__BootstrapApp(*args, **kwargs)
File "C:\Users\AOFU\Desktop\A-PYTHON\calculator.py", line 179, in OnInit
frame = MyFrame(None, -1, 'calculator.py')
File "C:\Users\AOFU\Desktop\A-PYTHON\calculator.py", line 17, in __init__
wx.EVT_MENU(self, 22, self.OnClose)
AttributeError: 'MyFrame' object has no attribute 'OnClose'

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/python
  2.  
  3.     # calculator.py
  4.  
  5. import wx
  6.  
  7. class MyFrame(wx.Frame):
  8.         def __init__(self, parent, id, title):
  9.  
  10.            wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, wx.Size(300, 250))
  11.            self.formula = False
  12.            menubar = wx.MenuBar()
  13.            file = wx.Menu()
  14.            file.Append(22, '&Quit', 'Exit Calculator')
  15.            menubar.Append(file, '&File')
  16.            self.SetMenuBar(menubar)
  17.            wx.EVT_MENU(self, 22, self.OnClose)
  18.            sizer = wx.BoxSizer(wx.VERTICAL)
  19.            self.display = wx.TextCtrl(self, -1, '',  style=wx.TE_RIGHT)
  20.            sizer.Add(self.display, 0, wx.EXPAND | wx.TOP | wx.BOTTOM, 4)
  21.  
  22.            gs = wx.GridSizer(4, 4, 3, 3)
  23.            gs.AddMany([(wx.Button(self, 20, 'Cls'), 0, wx.EXPAND),
  24.                            (wx.Button(self, 21, 'Bck'), 0, wx.EXPAND),
  25.                            (wx.StaticText(self, -1, ''), 0, wx.EXPAND),
  26.                            (wx.Button(self, 22, 'Close'), 0, wx.EXPAND),
  27.                            (wx.Button(self, 1, '7'), 0, wx.EXPAND),
  28.                            (wx.Button(self, 2, '8'), 0, wx.EXPAND),
  29.                            (wx.Button(self, 3, '9'), 0, wx.EXPAND),
  30.                            (wx.Button(self, 4, '/'), 0, wx.EXPAND),
  31.                            (wx.Button(self, 5, '4'), 0, wx.EXPAND),
  32.                            (wx.Button(self, 6, '5'), 0, wx.EXPAND),
  33.                            (wx.Button(self, 7, '6'), 0, wx.EXPAND),
  34.                            (wx.Button(self, 8, '*'), 0, wx.EXPAND),
  35.                            (wx.Button(self, 9, '1'), 0, wx.EXPAND),
  36.                            (wx.Button(self, 10, '2'), 0, wx.EXPAND),
  37.                            (wx.Button(self, 11, '3'), 0, wx.EXPAND),
  38.                            (wx.Button(self, 12, '-'), 0, wx.EXPAND),
  39.                            (wx.Button(self, 13, '0'), 0, wx.EXPAND),
  40.                            (wx.Button(self, 14, '.'), 0, wx.EXPAND),
  41.                            (wx.Button(self, 15, '='), 0, wx.EXPAND),
  42.                            (wx.Button(self, 16, '+'), 0, wx.EXPAND) ])
  43.  
  44.            sizer.Add(gs, 1, wx.EXPAND)
  45.  
  46.            self.SetSizer(sizer)
  47.            self.Centre()
  48.  
  49.            self.Bind(wx.EVT_BUTTON, self.OnClear, id=20)
  50.            self.Bind(wx.EVT_BUTTON, self.OnBackspace, id=21)
  51.            self.Bind(wx.EVT_BUTTON, self.OnClose, id=22)
  52.            self.Bind(wx.EVT_BUTTON, self.OnSeven, id=1)
  53.            self.Bind(wx.EVT_BUTTON, self.OnEight, id=2)
  54.            self.Bind(wx.EVT_BUTTON, self.OnNine, id=3)
  55.            self.Bind(wx.EVT_BUTTON, self.OnDivide, id=4)
  56.            self.Bind(wx.EVT_BUTTON, self.OnFour, id=5)
  57.            self.Bind(wx.EVT_BUTTON, self.OnFive, id=6)
  58.            self.Bind(wx.EVT_BUTTON, self.OnSix, id=7)
  59.            self.Bind(wx.EVT_BUTTON, self.OnMultiply, id=8)
  60.            self.Bind(wx.EVT_BUTTON, self.OnOne, id=9)
  61.            self.Bind(wx.EVT_BUTTON, self.OnTwo, id=10)
  62.            self.Bind(wx.EVT_BUTTON, self.OnThree, id=11)
  63.            self.Bind(wx.EVT_BUTTON, self.OnMinus, id=12)
  64.            self.Bind(wx.EVT_BUTTON, self.OnZero, id=13)
  65.            self.Bind(wx.EVT_BUTTON, self.OnDot, id=14)
  66.            self.Bind(wx.EVT_BUTTON, self.OnEqual, id=15)
  67.            self.Bind(wx.EVT_BUTTON, self.OnPlus, id=16)
  68.  
  69. def OnClear(self, event):
  70.         self.display.Clear()
  71.  
  72. def OnBackspace(self, event):
  73.            formula = self.display.GetValue()
  74.            self.display.Clear()
  75.            self.display.SetValue(formula[:-1])
  76.  
  77. def OnClose(self, event):
  78.            self.Close()
  79.  
  80. def OnDivide(self, event):
  81.            if self.formula:
  82.                return
  83.            self.display.AppendText('/')
  84.  
  85. def OnMultiply(self, event):
  86.            if self.formula:
  87.                return
  88.            self.display.AppendText('*')
  89.  
  90. def OnMinus(self, event):
  91.            if self.formula:
  92.                return
  93.            self.display.AppendText('-')
  94.  
  95. def OnPlus(self, event):
  96.            if self.formula:
  97.                return
  98.            self.display.AppendText('+')
  99.  
  100. def OnDot(self, event):
  101.           if self.formula:
  102.               return
  103.           self.display.AppendText('.')
  104.  
  105. def OnEqual(self, event):
  106.           if self.formula:
  107.               return
  108.           formula = self.display.GetValue()
  109.           self.formula = False
  110.           try:
  111.               self.display.Clear()
  112.               output = eval(formula)
  113.               self.display.AppendText(str(output))
  114.           except StandardError:
  115.               self.display.AppendText("Error")
  116.  
  117. def OnZero(self, event):
  118.           if self.formula:
  119.               self.display.Clear()
  120.               self.formula = False
  121.           self.display.AppendText('0')
  122.  
  123. def OnOne(self, event):
  124.           if self.formula:
  125.               self.display.Clear()
  126.               self.formula = False
  127.           self.display.AppendText('1')
  128.  
  129. def OnTwo(self, event):
  130.           if self.formula:
  131.               self.display.Clear()
  132.               self.formula = False
  133.           self.display.AppendText('2')
  134.  
  135. def OnThree(self, event):
  136.           if self.formula:
  137.               self.display.Clear()
  138.               self.formula = False
  139.           self.display.AppendText('3')
  140.  
  141. def OnFour(self, event):
  142.           if self.formula:
  143.               self.display.Clear()
  144.               self.formula = False
  145.           self.display.AppendText('4')
  146.  
  147. def OnFive(self, event):
  148.           if self.formula:
  149.               self.display.Clear()
  150.               self.formula = False
  151.           self.display.AppendText('5')
  152.  
  153. def OnSix(self, event):
  154.           if self.formula:
  155.               self.display.Clear()
  156.               self.formula = False
  157.           self.display.AppendText('6')
  158.  
  159. def OnSeven(self, event):
  160.           if self.formula:
  161.               self.display.Clear()
  162.               self.formula = False
  163.           self.display.AppendText('7')
  164.  
  165. def OnEight(self, event):
  166.           if self.formula:
  167.               self.display.Clear()
  168.               self.formula = False
  169.           self.display.AppendText('8')
  170.  
  171. def OnNine(self, event):
  172.           if self.formula:
  173.               self.display.Clear()
  174.               self.formula = False
  175.           self.display.AppendText('9')
  176.  
  177. class MyApp(wx.App):
  178.       def OnInit(self):
  179.           frame = MyFrame(None, -1, 'calculator.py')
  180.           frame.Show(True)
  181.           self.SetTopWindow(frame)
  182.           return True
  183.  
  184. app = MyApp(0)
  185. app.MainLoop()
  186.  
  187.  
Apr 11 '11 #1
Share this Question
Share on Google+
4 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
Your indentation is wrong. The functions after the class MyFrame block must be at the same leval as __init__().
Apr 11 '11 #2

P: 3
ok sorry,...it works...thx...but the calculator doesnt appear
Apr 11 '11 #3

bvdet
Expert Mod 2.5K+
P: 2,851
After correcting your indentation, the application appears to work fine.
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/python
  2.  
  3. # calculator.py
  4.  
  5. import wx
  6.  
  7. class MyFrame(wx.Frame):
  8.     def __init__(self, parent, id, title):
  9.  
  10.         wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition, wx.Size(300, 250))
  11.         self.formula = False
  12.         menubar = wx.MenuBar()
  13.         file = wx.Menu()
  14.         file.Append(22, '&Quit', 'Exit Calculator')
  15.         menubar.Append(file, '&File')
  16.         self.SetMenuBar(menubar)
  17.         wx.EVT_MENU(self, 22, self.OnClose)
  18.         sizer = wx.BoxSizer(wx.VERTICAL)
  19.         self.display = wx.TextCtrl(self, -1, '',  style=wx.TE_RIGHT)
  20.         sizer.Add(self.display, 0, wx.EXPAND | wx.TOP | wx.BOTTOM, 4)
  21.  
  22.         gs = wx.GridSizer(4, 4, 3, 3)
  23.         gs.AddMany([(wx.Button(self, 20, 'Cls'), 0, wx.EXPAND),
  24.                        (wx.Button(self, 21, 'Bck'), 0, wx.EXPAND),
  25.                        (wx.StaticText(self, -1, ''), 0, wx.EXPAND),
  26.                        (wx.Button(self, 22, 'Close'), 0, wx.EXPAND),
  27.                        (wx.Button(self, 1, '7'), 0, wx.EXPAND),
  28.                        (wx.Button(self, 2, '8'), 0, wx.EXPAND),
  29.                        (wx.Button(self, 3, '9'), 0, wx.EXPAND),
  30.                        (wx.Button(self, 4, '/'), 0, wx.EXPAND),
  31.                        (wx.Button(self, 5, '4'), 0, wx.EXPAND),
  32.                        (wx.Button(self, 6, '5'), 0, wx.EXPAND),
  33.                        (wx.Button(self, 7, '6'), 0, wx.EXPAND),
  34.                        (wx.Button(self, 8, '*'), 0, wx.EXPAND),
  35.                        (wx.Button(self, 9, '1'), 0, wx.EXPAND),
  36.                        (wx.Button(self, 10, '2'), 0, wx.EXPAND),
  37.                        (wx.Button(self, 11, '3'), 0, wx.EXPAND),
  38.                        (wx.Button(self, 12, '-'), 0, wx.EXPAND),
  39.                        (wx.Button(self, 13, '0'), 0, wx.EXPAND),
  40.                        (wx.Button(self, 14, '.'), 0, wx.EXPAND),
  41.                        (wx.Button(self, 15, '='), 0, wx.EXPAND),
  42.                        (wx.Button(self, 16, '+'), 0, wx.EXPAND) ])
  43.  
  44.         sizer.Add(gs, 1, wx.EXPAND)
  45.  
  46.         self.SetSizer(sizer)
  47.         self.Centre()
  48.  
  49.         self.Bind(wx.EVT_BUTTON, self.OnClear, id=20)
  50.         self.Bind(wx.EVT_BUTTON, self.OnBackspace, id=21)
  51.         self.Bind(wx.EVT_BUTTON, self.OnClose, id=22)
  52.         self.Bind(wx.EVT_BUTTON, self.OnSeven, id=1)
  53.         self.Bind(wx.EVT_BUTTON, self.OnEight, id=2)
  54.         self.Bind(wx.EVT_BUTTON, self.OnNine, id=3)
  55.         self.Bind(wx.EVT_BUTTON, self.OnDivide, id=4)
  56.         self.Bind(wx.EVT_BUTTON, self.OnFour, id=5)
  57.         self.Bind(wx.EVT_BUTTON, self.OnFive, id=6)
  58.         self.Bind(wx.EVT_BUTTON, self.OnSix, id=7)
  59.         self.Bind(wx.EVT_BUTTON, self.OnMultiply, id=8)
  60.         self.Bind(wx.EVT_BUTTON, self.OnOne, id=9)
  61.         self.Bind(wx.EVT_BUTTON, self.OnTwo, id=10)
  62.         self.Bind(wx.EVT_BUTTON, self.OnThree, id=11)
  63.         self.Bind(wx.EVT_BUTTON, self.OnMinus, id=12)
  64.         self.Bind(wx.EVT_BUTTON, self.OnZero, id=13)
  65.         self.Bind(wx.EVT_BUTTON, self.OnDot, id=14)
  66.         self.Bind(wx.EVT_BUTTON, self.OnEqual, id=15)
  67.         self.Bind(wx.EVT_BUTTON, self.OnPlus, id=16)
  68.  
  69.     def OnClear(self, event):
  70.         self.display.Clear()
  71.  
  72.     def OnBackspace(self, event):
  73.         formula = self.display.GetValue()
  74.         self.display.Clear()
  75.         self.display.SetValue(formula[:-1])
  76.  
  77.     def OnClose(self, event):
  78.         self.Close()
  79.  
  80.     def OnDivide(self, event):
  81.         if self.formula:
  82.             return
  83.         self.display.AppendText('/')
  84.  
  85.     def OnMultiply(self, event):
  86.         if self.formula:
  87.             return
  88.         self.display.AppendText('*')
  89.  
  90.     def OnMinus(self, event):
  91.         if self.formula:
  92.             return
  93.         self.display.AppendText('-')
  94.  
  95.     def OnPlus(self, event):
  96.         if self.formula:
  97.             return
  98.         self.display.AppendText('+')
  99.  
  100.     def OnDot(self, event):
  101.         if self.formula:
  102.             return
  103.         self.display.AppendText('.')
  104.  
  105.     def OnEqual(self, event):
  106.         if self.formula:
  107.             return
  108.         formula = self.display.GetValue()
  109.         self.formula = False
  110.         try:
  111.             self.display.Clear()
  112.             output = eval(formula)
  113.             self.display.AppendText(str(output))
  114.         except StandardError:
  115.             self.display.AppendText("Error")
  116.  
  117.     def OnZero(self, event):
  118.         if self.formula:
  119.             self.display.Clear()
  120.             self.formula = False
  121.         self.display.AppendText('0')
  122.  
  123.     def OnOne(self, event):
  124.         if self.formula:
  125.             self.display.Clear()
  126.             self.formula = False
  127.         self.display.AppendText('1')
  128.  
  129.     def OnTwo(self, event):
  130.         if self.formula:
  131.             self.display.Clear()
  132.             self.formula = False
  133.         self.display.AppendText('2')
  134.  
  135.     def OnThree(self, event):
  136.         if self.formula:
  137.             self.display.Clear()
  138.             self.formula = False
  139.         self.display.AppendText('3')
  140.  
  141.     def OnFour(self, event):
  142.         if self.formula:
  143.             self.display.Clear()
  144.             self.formula = False
  145.         self.display.AppendText('4')
  146.  
  147.     def OnFive(self, event):
  148.         if self.formula:
  149.             self.display.Clear()
  150.             self.formula = False
  151.         self.display.AppendText('5')
  152.  
  153.     def OnSix(self, event):
  154.         if self.formula:
  155.             self.display.Clear()
  156.             self.formula = False
  157.         self.display.AppendText('6')
  158.  
  159.     def OnSeven(self, event):
  160.         if self.formula:
  161.             self.display.Clear()
  162.             self.formula = False
  163.         self.display.AppendText('7')
  164.  
  165.     def OnEight(self, event):
  166.         if self.formula:
  167.             self.display.Clear()
  168.             self.formula = False
  169.         self.display.AppendText('8')
  170.  
  171.     def OnNine(self, event):
  172.           if self.formula:
  173.               self.display.Clear()
  174.               self.formula = False
  175.           self.display.AppendText('9')
  176.  
  177. class MyApp(wx.App):
  178.     def OnInit(self):
  179.         frame = MyFrame(None, -1, 'calculator.py')
  180.         frame.Show(True)
  181.         self.SetTopWindow(frame)
  182.         return True
  183.  
  184. app = MyApp(0)
  185. app.MainLoop()
Apr 11 '11 #4

P: 3
thx.it worked..i was doing that thing wrong..soo wrong...
Apr 11 '11 #5

Post your reply

Sign in to post your reply or Sign up for a free account.