I have been teaching myself Python and wanted to create something useful while learning. I am using wxPython to create the interface. The app works if I use the mouse to click the buttons. I've been trying to get it to recognize operation key-ins like addition using the num pad +. It will register the keystrokes but the binding for the event handler to run the operation commands isn't working. I know this isn't the cleanest written code and would also appreciate any advice to clean it. I have the code on github.
The calculator uses RPN similar to an HP48 series calculator and has the display appearance similar to an HP48 as well.
This is the section of code I think is not functioning the way I thought it should for recognizing the operation key-ins. The for loop for buttons1 generates the number pad. The for loop for buttons2 generates the buttons for the calculator operations.
`for row in buttons1: for label in row: b = wx.Button(self, label=label, size=(40,-1)) gsizer1.Add(b) b.Bind(wx.EVT_BUTTON, self.OnButton) sizer.Add(gsizer1, (6,0),(1,1), wx.EXPAND|wx.LEFT|wx.BOTTOM|wx.ALIGN_BOTTOM, 10) for row in buttons2: for label in row: b = wx.Button(self, label=label, size=(60,-1)) gsizer2.Add(b) b.Bind(wx.EVT_BUTTON, self.OnButton) b.Bind(wx.EVT_KEY_DOWN, self.OnKeyPress) sizer.Add(gsizer2, (6,1),(1,1), wx.EXPAND|wx.LEFT|wx.RIGHT|wx.BOTTOM, 10) self.SetSizerAndFit(sizer) def OnKeyPress(self, event): keycode = event.GetKeyCode() if keycode == wx.WXK_RETURN or wx.WXK_NUMPAD_ENTER: self.enter() elif keycode == wx.WXK_ADD or wx.WXK_NUMPAD_ADD: self.add()`
It is probably something simple. I just don't have the experience yet to know what it is.