Dropdown list separator

I was trying to do the same thing! I found this post and it works great (click on the link below):

2 Likes

Here’s a practical example:

#Place this code in the 'mousePressed' event
from functools import partial
from javax.swing import JMenuItem

def myFunc(event, parm1, parm2):
	msg = 'Selected value: %s\nSelected label: %s' %(parm1, parm2)
	system.gui.messageBox(msg)

if event.getButton() == 1: #This prevents right-click from triggering script
	itemList = ['Item 1', 'Item 2', 'Item 3']
	
	f1 = partial(myFunc, parm1 = 1, parm2 = itemList[0])
	f2 = partial(myFunc, parm1 = 2, parm2 = itemList[1])
	f3 = partial(myFunc, parm1 = 3, parm2 = itemList[2])
	
	itemNames = [itemList[0], '', itemList[1], itemList[2]]
	itemFunct = [f1, None, f2, f3]
	
	menu = system.gui.createPopupMenu(itemNames, itemFunct)
	menu.show(event, 2, event.source.height - 2)
5 Likes

Late to the party… but you can pass arguments using a lambda function, or if you need to pass something more complex, by defining a function that creates the function you want to call and passes in the arguments you want to use, and then returns the function itself. Better with an example…

def make_addToTrend(event, tag):
	def addToTrend(event=event, tag=tag):
		system.gui.messageBox("Adding tag '%s' to trend." % tag, "Trend")
	return addToTrend
items = ['Running', 'Faulted', 'Mode']
functions = [make_addToTrend(event, items[i]) for i in range(len(items))]

menu = system.gui.createPopupMenu(items, functions)
menu.show(event, 0,0)

Explanation:
Normally, when you add functions into the ‘functions’ variable with arguments, you’re actually executing this function and passing in the return value, instead of passing in the reference to the function itself.
Therefore, you must pass in a function that creates the function to add into the list, and which returns the function object itself.

You could use:

	def make_setHOA(event, value):
		def setHOA(event=event, value=value):
			system.tag.write("deviceHOA", value)
		return setHOA
	
	names = ["Auto", None, "Off", "Hand"]
	functions = [setHOA(1), None, setHOA(0), setHOA(2)]
	menu = system.gui.createPopupMenu(names, functions)
	menu.show(event, event.x, event.y)