Tkinter Menu command targets function with arguments? Tkinter Menu command targets function with arguments? tkinter tkinter

Tkinter Menu command targets function with arguments?


Command takes a function as argument, but foo("spam") gives the return value of foo when called with argument "spam". As a solution, you can use an anonymous function which calls foo("spam") as argument:

command=lambda: foo("spam")


For this kind of stuff, especially event handlers and commands, an elegant solution is using the functools module's partial() method.

from functools import partial...command=partial(foo, "spam")

Partial is said to be faster than using lambda: Differences between functools.partial and a similar lambda?