0

I can't figure out how to return a value from a function called by a bound button.

I want the equivalent of this:

call_function = function()

def function():
    return 'this'

print call_function

and have call_function = 'this'

but to work with something like this:

call_function_button.Bind(wx.EVT_BUTTON, class_object.function)
2
  • "but to work with something like this:" Can you elaborate on what the broader problem you are trying to solve? Commented Dec 3, 2013 at 17:04
  • Trying to clean up some code, I currently have several functions in the same class as my panel, it's getting messy and I want to move them. As it stands I call one function and it sets a value, when I call the next function it uses that value. Commented Dec 3, 2013 at 17:35

1 Answer 1

1

You're not really thinking in an event oriented way. When the button event is called, it doesn't return something useful. Instead, you'll want to make the call_function variable into an instance variable: self.call_function. Then you can set it inside the button handler. Here's one way to do it:

import random
import wx

########################################################################
class Panel(wx.Panel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent)

        self.call_function = None
        self.choices = ["this", "that", "something", "other", "python"]

        btn = wx.Button(self, label="Change Variable Value")
        btn.Bind(wx.EVT_BUTTON, self.onButton)

    #----------------------------------------------------------------------
    def onButton(self, event):
        """
        Change the value of self.call_function
        """
        self.call_function = random.choice(self.choices)
        print self.call_function

########################################################################
class Frame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="Return value")
        panel = Panel(self)
        self.Show()

#----------------------------------------------------------------------
if __name__ == "__main__":
    app = wx.App(False)
    frame = Frame()
    app.MainLoop()

Note that I'm randomly assigning various values to it instead of just the one. You can assign whatever you want to it. Then in your next function call, you can access it via self.call_function

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

2 Comments

Will this work if I have the function stored in another class?
You would have to instantiate the other class in the event handler...but that would always return the same value. Or are you talking about passing data between classes? If the latter, then you probably want to look at pubsub. See blog.pythonlibrary.org/2013/09/05/… for an example.

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.