Tuesday, 15 January 2013

Selection List: selection list


Selection List: selection list

Syntax: appuifw.selection list(choices, search field)
Example code:
appuifw.selection_list(colors, 1)
The selection list() function executes a dialog that allows the
user to select a list item. It works similarly to the popup menu() function
except that it has no label on the top. Instead, it has an option to display
a built-in search field (a find pane) at the bottom (see Figure 3.5).
Compared to the popup menu, the selection list is more suitable
for presenting long lists, especially because of the search field that lets
you find items in the list character by character. Setting the parameter
search field to 1 enables the find pane. It is disabled by default. If
the find pane is enabled, it appears only after you press a letter key.
The selection list() function returns the index of the chosen
item or None if the selection is cancelled by the user.

Exercise

To see how the selection list appears on your phone, create a script
(see Example 6) that displays a selection list() dialog with four
entries – ‘red’, ‘green’ ‘blue’ and ‘brown’, as in Figure 3.5.
When running the script, type the letter ‘b’. The find pane should
appear at the bottom of the list and the entries ‘blue’ and ‘brown’ should
appear. Select ‘blue’ and press the OK button. Text should appear, saying

‘blue is correct!’. If you select something else, for instance red, a text
should appear stating ‘Bzz! red is not correct’.
Example 6: Selection list
import appuifw
colors = [u"red", u"green", u"blue", u"brown"]
index = appuifw.selection_list(colors, 1)
if index == 2:
print "blue is correct!"
elif index != None:
print "Bzz! " + colors[index] + " is not correct"
We create a list with four colors that are specified as Unicode strings.
Each element represents an entry in our selection list dialog. The list is
assigned to the variable colors.
In the next line, we use the selection list() function and pass
colors to it. We also enable the find pane by setting 1 as the second
parameter. When our script is executed – and the user makes a selection
– this function returns an index that refers to an element inside our
list of colors.
Now, we need some code that determines whether the user has
selected the third item in the list or not. We compare the value of the
index variable with the correct index, 2. If the condition is true, we
print the text ‘blue is correct!’ to the screen. Otherwise, if the user did not
cancel the dialog, our script enters the elif block.


There, we combine another string to be printed out on the screen.
We pick the chosen color name from the colors list using the returned
index in the variable index and put it in the middle of the string to be
printed out.
In Python, you can print out almost any value, including lists and
tuples, and see the contents on the screen in a human-readable format.
This makes the print statement especially useful for debugging. If you
want to know more about debugging at this point, you should look
at Appendix B C where we explain the easiest ways to debug PyS60
programs.







No comments:

Post a Comment