Reputation: 227
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
Upvotes: 1
Views: 4418
Reputation: 4236
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.
Upvotes: 1
Reputation: 227
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).
Upvotes: 0