In the last post, the topic of discussion was about basic UI controls that PyS60 provides. These controls are useful when the solution to be developed is simple in terms of interaction. However, if a scenario presents itself where interaction becomes complex, then the basic controls would not suffice. For such situations, advanced controls need to be used that can abstract out the complexities of the interaction to the user as well as provide simple and consistent interface for the developer to work with. PyS60 has many such controls that a developer can use. In this discussion, the focus will be on three of the most commonly used controls – selection list, multi-selection list and text. The first section would focus on the whys and wherefores of these controls. In the last section, the application developed in the previous part will be enhanced using the controls being discussed in this section. That is the agenda for this discussion.
Lists and Text Controls – Whys and Wherefores
There are times when providing a list of choices is a better option than asking the user to enter data in an entry box. In such circumstances, providing a list of choices is a better option. To provide lists, PyS60 has two UI controls
1. Selection List
2. Multi-selection List
The former is a good choice when only one item needs to be chosen whereas when several items need to be chosen then the later is the control of choice. Here are the details.
1. Selection List:
As the name suggests, it displays a list to the user from which he or she can choose. The selection list provided by PyS60 also contains a search field that helps the user to narrow down the choices. It is displayed to the user using a function called selection_list(). It comes under the category of dialogs wrapped in functions. It takes two arguments:
a. choices
b. search_field
The latter is an optional argument.
a. choices:
It is a list of Unicode strings containing the options to be displayed to the user.
b. search_field:
It accepts a ‘0’ or ‘1’ as the value. The value decides whether the search field will be shown or not. If the value is ‘1’, the search field is shown. The default value is ‘0’ i.e. the search field is off by default. If enabled, the search field is shown after the first key press occurs.
The value returned by the selection_list() function is the index of the selected item. For example, to show a list with ‘egg’, ‘spam’ and ‘butter’ to choose from without any search field, the statements will be
list = [u”egg”, u”spam”, u”butter”]
selection_list(list)
2. Multi-selection List:
There are cases where a single selection is not a solution. This is where multi-selection list comes handy. Like selection list, multi-selection list is also a function that wraps up a dialog and displays the dialog when it is executed. The function that brings up a Multi-selection List is multi_selection_list(). It accepts the following arguments
a. choices
b. style
c. search_field
The first and third are similar to the selection_list() arguments. The second argument is unique to multi_selection_list(). Here are the details
a. choices:
Just as in the case of selection_list, the value accepted by this argument is a list of Unicode strings.
b. style:
This is optional. The value of this parameter decides how the list will be displayed. There are two valid values for the style parameter:
i. checkbox – It is the default value. If ‘checkbox’ is given as the value of the
style, the list presented to the user will be a list containing a
checkbox against each list item. Empty checkboxes indicate
selectable item.
ii. checkmark – It is the other acceptable value for the style argument. If
‘checkmark’ is used as the value, then the list presented to
the user doesn’t have a visual clue as to which item to be
selected. However, once selected, a checkmark will appear
against that item.
c. search_field:
This argument does the same thing that search_field does for selection_list()
function. If value of 1 is passed, the search field is displayed and if value of 0 is
given, the search field is not displayed.
The value returned by the function is tuple containing selected values. If no values are selected, then an empty tuple is returned. For example, the following statements display a list from which multiple items can be selected having the checkmark style, having a search field and the returned values are displayed.
list = [u”egg”, u”spam”, u”butter”]
result = multi_selection_list(list, ‘checkmark’, 1)
print result
Next, let us move onto the control that provides the text editor functionalities.
Text:
It provides text editor with almost all the text formatting functionalities. The control is provided by Text type. Unlike selection or multi-selection list, text is a type. The functionalities provided by text type are categorized under the following:
a. Attributes
b. Methods
Since the text is a type the functionalities are exposed either as a attribute or a method. Here are the details
a. Attributes
Following are the most commonly used attributes of Text:
i. color:
It defines the color of the text. The valid values are all the colors supported by
graphic models in graphics module.
ii. font:
This attribute determines the font family of the text. It can be set using the
supported Unicode name.
iii. style:
It affects the style of the text. The valid values for this attribute are defined as
flags. The valid flags are provided by appuifw module. Some of the commonly
used flags are
- STYLE_BOLD – Makes the text bold
- STYLE_UNDERLINE – Underlines a displayed text
- STYLE_ITALIC – Makes the text italic
For example, to use a Text type with LatinPlain12 as font’ value with bold text, the statements will be
t = appuifw.Text()
t.font = u”LatinPlain12″ # sets font to Latin Plain 12
t.style = appuifw.STYLE_BOLD
b. Methods
Text type provides various methods to perform common operations on the text
held by the editor. The most common methods are:
i. add() : It accepts Unicode string as argument. This method appends a text to
the existing text. It can also be used to insert the
passed argument at current cursor position.
ii. set() : Just like add() method, tt accepts Unicode string as argument. It sets
the passed Unicode string as argument. It replaces any text that
may be present in the editor.
iii. delete() : It accepts two arguments – position and length and deletes length of
characters in the editor starting from the position passed as
argument.
For example, to set a string having value as “This is a text” as the text in the editor, the statement is
t.set(u”This is a text”)
This brings us to the end of this section. In the next section, the guessing game application that was developed in last part will be enhanced using single selection list.
PyS60 in Real World:
In the last discussion, a guessing game was developed. The code was as follows
from appuifw import *
continue_guess=true
while continue_guess:
guess_no=random()
user_guess=query(u”Enter your guess”, ‘number’)
if user_guess is Null:
note(u”You have opted out”)
continue_guess=false
elif user_guess<guess_no:
note(u”Your guess is lesser than the goal”)
elif user_guess>guess_no:
note(u”Your guess is higher than the goal”)
else:
note(u”Congrats for excellent guess”)
user_choice=query(u”Enter Y to continue or N to quit”)
if user_choice is Null or user_choice==’N’:
continue_guess=false
else:
continue_guess=true
Now, let us change the display part for displaying the choices. Instead of taking user’s input using a query dialog, let us show a selection list from which the user can select a value. The following code
user_guess=query(u”Enter your guess”, ‘number’)
needs to be changed to
list = [guess-100, guess, guess*200, u”Quit”]
user_guess = selection_list(list)
first, a list is created using the guess no. and its combination. Then the list is given as an argument to selection_list() method. The returned index, which is the value selected by the user, is then stored in the user_guess variable.
Next, let us change how the result is processed. To do so, the following code
if user_guess is Null:
note(u”You have opted out”)
continue_guess=false
elif user_guess<guess_no:
note(u”Your guess is lesser than the goal”)
elif user_guess>guess_no:
note(u”Your guess is higher than the goal”)
else:
note(u”Congrats for excellent guess”)
to the following
if guess_no < list [user_guess]:
note(u”Your guess is lesser than the goal”)
elif guess_no < list [user_guess]:
note(u”Your guess is higher than the goal”)
elif guess_no == list [user_guess]:
note(u”Congrats for excellent guess”)
else:
note(“Bye”)
continue_guess = false
The index returned by the selection_list() is used by the if condition to get the value selected by the user, from the list. If the index corresponds to the value lesser or greater than the goal value, then a note is displayed telling the user that he/she is either short of the goal or overshot the goal. Otherwise, user is congratulated. If user selected the option to quit, the continue_guess is set to false, thus ending ‘the game’. The complete code is as follows
from appuifw import *
continue_guess=true
while continue_guess:
guess_no=random()
list = [guess-100, guess, guess*200, u”Quit”]
user_guess = selection_list(list)
if guess_no < list [user_guess]:
note(u”Your guess is lesser than the goal”)
elif guess_no < list [user_guess]:
note(u”Your guess is higher than the goal”)
elif guess_no == list [user_guess]:
note(u”Congrats for excellent guess”)
else:
note(“Bye”)
continue_guess = false
That completes our application. This discussion focused on the UI controls. From the next discussion, the focus will be moving towards the third party libraries in PyS60. However, the next discussion will focus primarily on graphics module. Till then…
