Tuesday, 15 January 2013

Popup Menu: popup menu


Popup Menu: popup menu

Syntax: popup menu(list[, label ])
Example code:
appuifw.popup menu(choices, u"Select:")
The popup menu() function displays a list of items in a popup dialog.
You need to give a list of Unicode strings in the parameter list. You
can also define a label which is the text that appears at the top of the list.
This function returns the index of the chosen item in the list. If the user
cancels the dialog by pressing Back, None is returned.

Exercise

Create a script (see Example 5) that displays a popup menu with three
entries, such as the one in Figure 3.4. After selecting any of the choices
and pressing ‘OK’, you should see a note appear with different text
depending on which selection you made. Use the phone’s up and down
keys to make the selection from the menu.



Example 5: Popup menu
import appuifw
choices = [u"Symbian", u"PyS60", u"MobileArt"]
index = appuifw.popup_menu(choices, u"Select:")
if index == 0:
appuifw.note(u"Symbian, aha")
elif index == 1:
appuifw.note(u"PyS60 - yeah")
elif index == 2:
appuifw.note(u"I love MobileArt")


First we import the module appuifw. In the next line, we create a
list with three elements [u"Symbian", u"PyS60", u"MobileArt"].
Each element represents an entry in the menu. The list is assigned to the
variable choices.
In the next line, we use the popup menu() function and pass the
variable choices as the first parameter to the function. We also pass a
Unicode string u"Select:" as the second parameter to the function.
When our script is executed, the function returns an index that refers
to one of the elements of our list – depending on the user’s selection. The
returned index – a number – is stored in a variable that we have named
index.
Now, we need some code that displays a note dialog with different
content depending on the user’s selection. This is done using the if
statement, simply by checking the returned list index and comparing it
to all possible options: 0, 1, 2. If the user cancels the dialog, none of the
conditions is true and no dialog is shown.










No comments:

Post a Comment