Generate menue in python


I have problems creating menu entries using Python… I use the following script, but TD interprets each character as a separate entry… where is my mistake?

parent = op('/project1')
if parent.op('SimpleUI'):
    parent.op('SimpleUI').destroy()
comp = parent.create(baseCOMP, 'SimpleUI')
page = comp.appendCustomPage('UI')
menu = page.appendMenu('Choice', label='Auswahl')
menu.menuNames   = 'ainz zwai trai'
menu.menuLabels  = "ainz,zwai,trai"
menu.default     = 'ainz'

Try adding [ ] and a comma separated list of words.

menu.menuNames = ['ainz', 'zwai', 'trai']
menu.menuLabels = ['ainz','zwai','trai']

It’s looking for a list. If you only give it one string python automatically assumes its a list of characters.
Cheers.

thanks … but:

I copy/pasted some of your code,

menu.menuNames = [‘ainz’, ‘zwai’, ‘trai’]

and it complained about the type of quotes, i.e. they should be matching single or double quotes, not the up/down style.

As I was curious as well about creating a menu programmatically, I ended up having to do:

menu.menuNames = ([‘foo’, ‘bar’],)
menu.menuLabels = ([‘FOO’, ‘BAR’],)

Not sure why it wouldn’t just take the list without it being inside a tuple.

Looks like the quotes were replaced by the forum editor here unfortunately.
Using preformatted text tag will fix that.

Yah matching single or double quotes.

Not sure I follow your example:

python >>> n.par.Menu.menuLabels = ('A','B','C')         #3 entries:  A, B and C
python >>> n.par.Menu.menuLabels = ['A','B','C']         #3 entries:  A, B and C
python >>> n.par.Menu.menuLabels = (['A','B','C'],)      #single entry:   ['A','B','C']
python >>> n.par.Menu.menuLabels = [('A','B','C'),]      #single entry:  ('A','B','C')

Did you get 3 entries or 1?

I was getting 3, but I now think that’s because in the original example, ‘menu’ is a ParGroup:

python >>> menu
type:ParGroup name:Choice owner:/project1/base1 value:(ainz)

whereas your example is accessing it via n.par. I did it via n.par and got behavior that matches yours.