How to pass a parameter in a lambda function in python? -
i want set colors of object, , don't want create 10 functions every color. so, want declare colors , create 10 buttons , 1 function. error message is:
<lambda>() missing 1 required positional argument: 'green'
the code:
from tkinter import * green=["#5bd575","#55c76d"] #different colors should follow here root=tk() btn=button(text="trigger lambda", command=lambda green: printfunction(green)) btn.pack() def printfunction(colorset): print(colorset)
it not need lambda function, question just, how can call printfunction
argument clicking button?
the command
callable doesn't take any arguments. if want pass green
list printfunction
, omit argument, lambda doesn't need it:
btn=button(text="trigger lambda", command=lambda: printfunction(green))
now green
inside lambda refers global.
if wanted call printfunction
pre-defined argument, use functools.partial()
function; pass function called plus arguments need passed in, , when it's return value called, it'll that; call function arguments specified:
from functools import partial btn=button(text="trigger lambda", command=partial(printfunction, green))
Comments
Post a Comment