If you bind GUI event to a function you use a callback function, alas, callback functions are not allowed to have arguments. To remedy situation you can use currying or lambda. Here is an examples of both:
# currying allows GUI callback functions to have arguments
# an alternative is lambda
from Tkinter import *
class Curry:
"""adds arguments to callback function"""
def __init__(self, callback, *args, **kwargs):
self.callback = callback
self.args = args
self.kwargs = kwargs
def __call__(self):
# needs Python23 or higher
return self.callback(*self.args, **self.kwargs)
def callback(what=None):
print "callback =", what # for testing
if what == 'red':
b1.config(bg="#ff0000")
if what == 'blue':
b2.config(bg="#0000ff")
root = Tk()
# uses class Curry, since command=callback("red") will not work
b1 = Button(root, text="red", width=6, command=Curry(callback, "red"))
b1.pack(side=LEFT, padx=2, pady=2)
# uses lambda instead of Curry
b2 = Button(root, text="blue", width=6, command=lambda: callback("blue"))
b2.pack(side=LEFT, padx=2, pady=2)
mainloop()
Editor's Note:
command uses a function object rather then a function call, so arguments have to be added with a lambda or curry wrapper.