Popup menu with indirect parameters

def myScript(event,mybit):
	self.myscript(mybit)

query = "select * from table"
result = system.db.runQuery(query)
if len(result)>0:
	names = []
	functions=[]
	for row in result:
		id = row["id"]
		names.append("Mon menu : "+ str(id))
		functions.append(myScript(event,id))
	menu = system.gui.createPopupMenu(names, functions)
	menu.show(event)

On “onPopupTrigger” event, then myScript start 4 times (i have 4 rows in table) and then have a popup menu with no entry.

I try these :

def myScript(event,mybit):
	self.myscript(mybit)

query = "select * from table"
result = system.db.runQuery(query)
if len(result)>0:
	names = []
	functions=[]
	for row in result:
		id = row["id"]
		names.append("Mon menu : "+ str(id))
		functions.append(myScript)
	menu = system.gui.createPopupMenu(names, functions)
	menu.show(event)

The popup was create but when i click on 1 entry, i have these message :

ERROR [WindowUtilities$JyPopupMenu-AWT-EventQueue-2] Error executing script.
TypeError: myScript() takes exactly 2 arguments (1 given)

Can you help me please ?

Hi Maxime,
There’s a couple things going on here. The first is the limitation on event scripts to only take one argument, the event object. The other is the context change while running that leaves you with just the event object in the action script. (‘self’ won’t be there for you.) Fortunately, the event object has the ‘source’ property, which you can use in place of ‘self’. The argument limitation requires a bit more effort: you must define a function nested in your pop-up generation function that carries any additional information as default values of extra arguments. Since the event system won’t set them at call-time, the nested function will get those values. With these two structural changes, your code will end up something like this:query = "select * from table" result = system.db.runQuery(query) if len(result)>0: names = [] functions=[] for row in result: id = row["id"] names.append("Mon menu : "+ str(id)) def myNestedScript(event, innerId=id): event.source.myscript(innerId) functions.append(myNestedScript) menu = system.gui.createPopupMenu(names, functions) menu.show(event)Notice that the myScript() routine is gone. The nested script will directly call the custom component method you want. The innerId gets the id value when it is defined (when the menu is created), and holds it as a default until it is run (when you pick that menu item).
Also note that myNestedScript is added to the list of menu functions without it’s own parentheses. That’s important: you don’t want to run it at that point.

One more note: Each run through the for loop makes a new ‘myNestedScript’ function, with its own default value. That’s how multiple menu items will work with what seems to be a single function.

Thank you.