Returning values from event handler function wxPython
Question: So in normal python scripts you can do something like this:

    
    
    def func():
        i = 1
        return i
    i = func()
    

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.

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:

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

I have looked at these [Call functions from within a wxPython event
handler](http://stackoverflow.com/questions/10567726/call-functions-from-
within-a-wxpython-event-handler\]) , [Return value from wxpython main
frame](http://stackoverflow.com/questions/20674937/return-value-from-wxpython-
main-frame\]) but they don't really address what I want to do here


Answer: Yes, you can call event handler independently, why not?

    
    
    def OnClick (self, event=None):
        if event==None: return self.LastMousePosition
        self.LastMousePosition = event.GetMousePosition()
    
    pos = OnClick()
    

And, returning value from an event handler doesn't make sense. Returned value
may only indicate something to a caller. For instance, if handler returns True
something is continuing, if False, process stops.

Handlers are called only when events are triggered, either by GUI input or you
can post an event manually, thus simulating it.

What you wrote in your answer is the way of passing values out of the event
handlers. That is how it is done.

There is no "returning values from event handlers".

Maybe, but only maybe, somewhere exists an event driven system that stores
returns onto a stack or somewhere for you to pick them up later. WX does not
do this.

