title: Returning values from event handler function wxPython
body: <p>So in normal python scripts you can do something like this:</p>
  
  <pre><code>def func():
      i = 1
      return i
  i = func()
  </code></pre>
  
  <p>Generally speaking, another python program would be able to import the file containing this and just say i = func() in their program and they would get the value of i.</p>
  
  <p>Now I want to do something similar but instead I need to do it when a user presses a button or activates an event handler from the GUI. Something like:</p>
  
  <pre><code>import wx
  
  class MyForm(wx.Frame):
  
      def __init__(self):
              wx.Frame.__init__(self, None, wx.ID_ANY, "Pressing dem keyz")
  
              # Add a panel so it looks the correct on all platforms
              panel = wx.Panel(self, wx.ID_ANY)
              self.btn = wx.ToggleButton(panel, label="TOGGLE")
              self.btn2 = wx.ToggleButton(panel, label="TOGGLE 2", pos = (85,0))
  
              self.btn.Bind(wx.EVT_CHAR_HOOK, self.onKeyPress)
              self.btn2.Bind(wx.EVT_CHAR_HOOK, self.onKeyPress)
  
      def onKeyPress(self, event):
              space = False
              keycode = event.GetKeyCode()
              print keycode
              if keycode == ord(' '): 
                      print "SPACEBAR!"
                      space = True
              self.btn.SetValue(space)
  
              if space == True:
                      print "Lemme return i please"
                      i = 1
                      print i
                      return i #ALSO PART OF WHAT I WANT TO DO
              elif keycode == wx.WXK_RETURN:
                      self.btn
              elif keycode == wx.WXK_LEFT:
                      self.btn2
                      print 'YOU MOVED LEFT'
              elif keycode == wx.WXK_RIGHT:
                      self.btn
                      print 'YOU MOVED RIGHT'
              elif keycode == wx.WXK_UP:
                      print 'YOU MOVED UP'
              elif keycode == wx.WXK_DOWN:
                      print 'YOU MOVED DOWN'
              elif keycode == wx.WXK_ESCAPE:
                    self.Destroy()
      #NOW HERE'S THE PART THAT I WANT TO IMPLEMENT
      return keycode 
      #I do not need the keycode necessarily but I want to do something similar to this along with the |return i| above
  
  # Run the program
  if __name__ == "__main__":
          app = wx.App(False)
          frame = MyForm()
          frame.Show()
          app.MainLoop()
  </code></pre>
  
  <p>I have looked at these <a href="https://stackoverflow.com/questions/10567726/call-functions-from-within-a-wxpython-event-handler]">Call functions from within a wxPython event handler</a> ,  <a href="https://stackoverflow.com/questions/20674937/return-value-from-wxpython-main-frame]">Return value from wxpython main frame</a> but they don't really address what I want to do here</p>