Shared script to createPopupMenu

Hi Guys,

If I do something like this on a components mouseReleased event handler I get what I expect (a popup menu when the component is right clicked):

def function1():
pass

def rightClickPopup(component):
menu = system.gui.createPopupMenu([“Item1”], [function1])
menu.show(event)

rightClickPopup(event.source)

I would like to move this to a shared script named myfuncs:

def function1():
pass

def rightClickPopup(component):
menu = system.gui.createPopupMenu([“Item1”], [function1])
menu.show(event)

When a call the function on a mouseReleased event handler on a component like this:

shared.myfuncs.rightClickPopup(event.source)

I get this error:

Traceback (most recent call last):

File “event:mouseReleased”, line 1, in

File “module:shared.myfuncs”, line 19, in rightClickPopup

NameError: global name ‘event’ is not defined

Ignition v7.9.9 (b2018081621)
Java: Oracle Corporation 1.8.0_171

Any help would be much appreciated.

The working examples are taking advantage of jython’s closures – variables in an outer scope referenced by inner scopes are captured at definition time. No event objects or anything else defined in an event or custom method exist in a project script’s scope. You will need to pass the event as an argument to your project script.

1 Like

Thanks Phil. Another quick question - can I pass other arguments to the createPopupMenu functions?

No, you cannot. If you’re working with popup menus, functools.partial is your friend.

1 Like

Hi Paul, thanks for the spark! For anyone wanting an example to pass more arguments to createPopupMenu:

from functools import partial

def function(x, event):
print x

y = 1
function_partial = partial(function, y)
menu = system.gui.createPopupMenu([“text”], [function_partial])
menu.show(event)

2 Likes