[Tkinter-discuss] No Parameters fo Callback functions?

Martin Franklin mfranklin1 at gatwick.westerngeco.slb.com
Fri Nov 4 10:01:06 CET 2005


Stewart Midwinter wrote:
> Samuel, you can use lambda for this purpose.    Here's how you would
> use lambda in a menu to specify arguments to pass to a callback
> method:
> 
> command = lambda self=self, viewer=viewer:
> self.open_pw_display(viewer, self.pwDisplays[viewer])
> 
> Note that you have to pass in a reference to the class you are in,
> using 'self', or the lambda won't find your method.
> 
> HTH,

The 'standard alternative' to lambda is to use a callback class
like so :)

class Callback:
     def __init__(self, checkbutton, action):
         self.checkbutton = checkbutton
         self.action = action

     def __call__(self):
         self.action(self.checkbutton)


...

cb1 = Checkbutton(self, command=Callback("checkbutton1",
     self.actionFunction, etc...)
cb2 = Checkbutton(self, command=Callback("checkbutton2",
     self.actionFunction, etc...)


HTH
Martin



More information about the Tkinter-discuss mailing list