button1 = tkinter.Button(frame, text="Say hi", command=print)
button2 = tkinter.Button(frame, text="foo", command=print)
button3 = tkinter.Button(frame, text="bar", command=print)
You’ve probably spotted the hole in my program: print can’t specify arguments. This renders the whole thing useless and faulty. Obviously, having something like
command=print("foo")
will call that function when the object is actually instantiated and make command the return value (if any) of that function call. (Not what I want)
So, how can I specify arguments in the above mentioned scenario, and avoid having to define seperate command functions for each of the buttons?
A simple solution is to use lambda, which lets you create anonymous functions.
Another choice is to use functools.partial, which is explained a bit in this answer: https://stackoverflow.com/a/2297423/7432