pissed off at the Menu widget


(Charlls) #1

Hi folks!

according to the python docs, which otherwise are very clear, are totally screw at this widget, it says that the format of the Menu registering is

Men = Menu(“Title%t | Option1%x1 | …| Optionn½xn”,ev, x , y ,w ,h, Men.val )

well, i would add to that ‘format’ some tokens like [email protected]#$!!?¿%CK…

isnt only that this format doesnt explain nothing, but that the resulting vals have NOTHING to do with the Option strings…

i tried to do something like…

Men=Create(“ZFILL”)

Men=Menu(" ZFILL | ZUSE | ZTEST | ZNONE " , 5 , x , y , w , h , Men.val )

well, this scheme happen to not work, it complains at the registering of the widget as

#error: expecting a number

well, then checking some of the posts here in elysiun about the topic, someone posted an example with options " a | b | c | d " but it Created it with Create(1),
but, guess what? this returns just a number 1, 2, 3…etc…

so, at this point im starting to guess that i should do if /elif clauses and duplicate all the string cases, but if im wrong, please let me know

Regards,


(eeshlo) #2

from Blender.BGL import *
from Blender.Draw import *

# the variable that will contain the value
# refering to the chosen menu option
testmenu = Create(1)	# option 1 is the initial value

# descriptive variable to hold the menu event number
evt_menu = 1	# any number you want

def draw():
	# variable must be global to be able to change it inside function
	global testmenu

	# clear screen
	glClearColor(0.5, 0.5, 0.5, 1)
	glClear(GL_COLOR_BUFFER_BIT)
	
	# define menu
	testmenu = Menu('Title%t|Option 1%x1|Option 2%x2|Option 3%x3',
		evt_menu, 100, 300, 200, 20, testmenu.val, 'Choose any option')

	# display the returned value on screen
	glColor3f(0, 0, 0)
	glRasterPos2i(100, 280)
	Text("Your choice: %d" % testmenu.val)


def event(evt, val):
	# Q-Key pressed? exit the script
	if (evt == QKEY and not val):
		Exit()


def bevent(evt):
	# if menu option was chosen, redraw to display the choice
	if evt == evt_menu:
		Redraw()

Register(draw, event, bevent)


(theeth) #3

Men = Create("ZFILL")
Men = Menu(" ZFILL | ZUSE | ZTEST | ZNONE " , 5 , x , y , w , h , Men.val ) 

should be


Men = Create(1)
Men = Menu(" ZFILL%1 | ZUSE%2 | ZTEST%3 | ZNONE%4 " , 5 , x , y , w , h , Men.val ) 

The %1* parts defines the index of the option. That’s why Men has to be a number, as it holds the index of the option. The book is not wrong, it’s just not clearly explained.

*: you can omit these. In that case, Blender should automaticly increment the index for each option. I’m not sure if the index starts counting at 1 or 0…

Martin