Dynamic command on Menu items?

J

Joe S

(Python newbie alert..only 1 week's experience)

I want to create several menu items based upon a config file. In this
config file are label, function and a parameter. I want to show label
in the menu, then when it's chosen, call function with the given
parameter.

How can I do the correct equivalent of the following (which definitely
doesn't work)?

l='Zooks'
f='func'
p='Got Zooks'
M.menu.add_command(label=l, command=lambda: f(p))

so that when I pick 'Zooks' from the menu, func gets called with 'Got
Zooks'.


Joe
 
D

Diez B. Roggisch

You could do it like this (untested):

class FunClosure:
def __init__(self, f, args):
self.f = f
self.args = args
def __call__(self):
self.f(self.args)

M.menu.add_command(label=l, command=FunClosure(f, [p]))
 
J

Joe S

Diez B. Roggisch said:
You could do it like this (untested):

class FunClosure:
def __init__(self, f, args):
self.f = f
self.args = args
def __call__(self):
self.f(self.args)

M.menu.add_command(label=l, command=FunClosure(f, [p]))


Sorry...no work. It says str object is not callable.

I would def. appreciate any refinement. If not, thanks anyway for trying.


Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,204
Messages
2,571,063
Members
47,670
Latest member
micheljon

Latest Threads

Top