More Tkinter menu problems
Timothy R Evans
tre17 at cosc.canterbury.ac.nz
Thu Jun 3 23:30:22 EDT 1999
"John Michelsen" <john.michelsen at gte.net> writes:
> > self.menu.add_command(label = new_name,
> > command = lambda temp_color = new_color:
> > self.select_color(temp_color))
> >
> >but I get a Name Error on "self". So now "self" is out of scope when
> >the menu call happens.
>
>
> try:
> self.menu.add_command(label = new_name,
> command = lambda c=new_color, f=self.select_color: f(c))
>
> John
Another possible way of implemented getting the same result, without
resorting to unreadable lambda expressions is to create a class
something like...
class Command:
def __init__(self, func, *args, **kw):
self.func = func
self.args = args
self.kw = kw
def __call__(self, *args, **kw):
args = self.args + args
kw.update(self.kw)
apply(self.func, args, kw)
then...
command = Command(self.select_color, new_color)
This costs one extra function call when it is used, but I think the
decrease in complexity of the expression make it worth it.
--
Tim Evans
More information about the Python-list
mailing list