buttond.Bind() and args

if i'm calling pourDrink via Button1.Bind(wx.EVT_BUTTON,pourDrink)
how can i pass etc arguments to pourDrink? eg. pourDrink(db)

Create a class...

class caller:
    def __init__(self, fcn, *args):
        self.fcn = fcn
        self.args = args
    def __call__(self, evt):
        return self.fcn(*args)

Button1.Bind(wx.EVT_BUTTON, caller(pourDrink, db))

Or you could make it a method...

class obj:
    def __init__(self, db):
        self.db = db
    def pourDrink(self, evt):
        #do what you need to

Or you could attach pourDrink to any pre-existing class that has access
to your db.

Remember, this is Python, with all of the object-oriented features of
Python.

- Josiah

···

Timothy Smith <timothy@open-networks.net> wrote:

if i'm calling pourDrink via Button1.Bind(wx.EVT_BUTTON,pourDrink)
how can i pass etc arguments to pourDrink? eg. pourDrink(db)

Or just use a lambda (unnamed function), it's far compact, warts or no warts:

Button1.Bind(wx.EVT_BUTTON, lambda e: pourDrink(db))

···

On 7/31/06, Josiah Carlson <jcarlson@uci.edu> wrote:

Timothy Smith <timothy@open-networks.net> wrote:
> if i'm calling pourDrink via Button1.Bind(wx.EVT_BUTTON,pourDrink)
> how can i pass etc arguments to pourDrink? eg. pourDrink(db)

Create a class...

class caller:
    def __init__(self, fcn, *args):
        self.fcn = fcn
        self.args = args
    def __call__(self, evt):
        return self.fcn(*args)

Button1.Bind(wx.EVT_BUTTON, caller(pourDrink, db))

Charl P. Botha wrote:

···

On 7/31/06, Josiah Carlson <jcarlson@uci.edu> wrote:

Timothy Smith <timothy@open-networks.net> wrote:
> if i'm calling pourDrink via Button1.Bind(wx.EVT_BUTTON,pourDrink)
> how can i pass etc arguments to pourDrink? eg. pourDrink(db)

Create a class...

class caller:
    def __init__(self, fcn, *args):
        self.fcn = fcn
        self.args = args
    def __call__(self, evt):
        return self.fcn(*args)

Button1.Bind(wx.EVT_BUTTON, caller(pourDrink, db))

Or just use a lambda (unnamed function), it's far compact, warts or no warts:

Button1.Bind(wx.EVT_BUTTON, lambda e: pourDrink(db))

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

lambda won't work because it supplies the event object

Of course it will work. "lambda e" means to define an unnamed
function that takes one parameter (in this case the event object).
You can use or ignore this parameter in the lambda "body".

I've used this contruct numerous times with WX events.

···

On 7/31/06, Timothy Smith <timothy@open-networks.net> wrote:

lambda won't work because it supplies the event object

Charl P. Botha wrote:

···

On 7/31/06, Timothy Smith <timothy@open-networks.net> wrote:

lambda won't work because it supplies the event object

Of course it will work. "lambda e" means to define an unnamed
function that takes one parameter (in this case the event object).
You can use or ignore this parameter in the lambda "body".

I've used this contruct numerous times with WX events.

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

ahh i see what i was doing wrong

i needed to use lambda e: pourDrink(e,db)
i needed to event object as well. silly me.

Does pourDrink do anything with the event object? No? Then you don't
need to define it or pass it. Similarly with the examples I provided,
they all took an event argument, but all effectively ignored it.

- Josiah

···

Timothy Smith <timothy@open-networks.net> wrote:

Charl P. Botha wrote:
> On 7/31/06, Timothy Smith <timothy@open-networks.net> wrote:
>> lambda won't work because it supplies the event object
>
> Of course it will work. "lambda e" means to define an unnamed
> function that takes one parameter (in this case the event object).
> You can use or ignore this parameter in the lambda "body".
>
> I've used this contruct numerous times with WX events.

ahh i see what i was doing wrong

i needed to use lambda e: pourDrink(e,db)
i needed to event object as well. silly me.

Josiah Carlson wrote:

···

Timothy Smith <timothy@open-networks.net> wrote:
  

Charl P. Botha wrote:
    

On 7/31/06, Timothy Smith <timothy@open-networks.net> wrote:
      

lambda won't work because it supplies the event object
        

Of course it will work. "lambda e" means to define an unnamed
function that takes one parameter (in this case the event object).
You can use or ignore this parameter in the lambda "body".

I've used this contruct numerous times with WX events.
      

ahh i see what i was doing wrong

i needed to use lambda e: pourDrink(e,db)
i needed to event object as well. silly me.
    
Does pourDrink do anything with the event object? No? Then you don't
need to define it or pass it. Similarly with the examples I provided,
they all took an event argument, but all effectively ignored it.

- Josiah

yeah i do need to use the event object :slight_smile: thats what made the penny drop for me.