1

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 , Return value from wxpython main frame but they don't really address what I want to do here

4
  • I ask because you can't really call one of these event handler functions other than when you bind them to a button and if a program were to call this function it wouldn't be able to unless it is also binding it to a button. Commented Aug 3, 2015 at 19:20
  • Can you give some more context? What's the use case for wanting to do this? Commented Aug 3, 2015 at 20:26
  • @JHarris Well maybe I just didn't phrase the question correctly given the answer from Dalen below but the point was to be able to get information out of an event handler in a variable form, the point being if you modify something that was triggered by an event (say a toggle button was pressed and I set a variable inside that handler to True) and I want to get back the thing that was modified (So I want to know if the variable is True or not) and then in some other function somewhere use this to my leisure (say change the background colour of that button) Commented Aug 4, 2015 at 1:08
  • I know that what I said above can be accomplished in a much better way (and it has been) but I just used it as a quick example. Commented Aug 4, 2015 at 1:09

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Ok so this isn't really the answer to the question because it does not address return values but it accomplishes the same thing pretty much.

So the value that you want to return could be set as a class-wide feature using self (self.i for example), and in order to access it you can just say frame.i once you have created the MyForm object under if name == 'main' block and assign it to an outside variable (i.e. i = frame.i).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.