Super Tip: Pass Arguments in your Python Bindings using Lambda Functions

Problem: Help! How can I pass parameters in my Bindings?

Solution: By using lambda functions in python(Check your PL’s manual for anything similar), you would be able to create on-the-fly functions that returns your bindings with the extra parameters/arguments you want to pass.

Example 1: Passing an argument to a wx Button Event

btn = wx.Button(self, 10, “Button”, (10, 10))
self.Bind(wx.EVT_BUTTON, lambda event: self.OnClick(event, ‘passthis’))

def OnClick(self, event, somearg):
   print somearg

Example 2: Passing an argument to Pyhook’s HookManager.KeyUp Event

hook = pyHook.HookManager()
hook.KeyUp = lambda event: OnKeyboardEvent(event, ‘Pass Me.’)
hm.HookKeyboard()

def OnKeyboardEvent(event, myarg)
  print myarg

That’s It!

Related posts:

  1. Super Tip! How to Create that floating Facebook toolbar at the bottom of the page!
  2. Jquery Super Tip! Automatically highlight the links/menu of the current page with 1 line of code

Still can't see what you're looking for?



Leave a Reply

Anti-Spam Protection by WP-SpamFree