functiontkinterbuttonlambdabinding

Why use command=lambda : somefunction() vs command=somefunction in button commands?


I've seen this a lot online but with no discernable rhyme or reason. When creating an event binding, why call "lambda: thefunction() " instead of just passing " thefunction"? -- especially when nothing needs passed to said function?

ie. self.button = tk.Button(root, command= lambda:arb_func_call() vs. self.button = tk.Button(root, command= arb_func_call

is this a matter of style or is there a reason to use x over y?


Solution

  • There is no reason to use lambda for the command attribute of a button, unless you need the command to pass one or more arguments. The lambda with no arguments only serves to make the code more complicated than it needs to be.