GUI changed the way a program communicates with the user. It made applications interactive. Interactivity is the norm of the day. Mobiles are no exceptions. PyS60 makes creating GUI based applications easy. It can be termed as on of the RAD environment for Symbian OS. In this discussion I will focus on the basics of using UI library provided by PyS60. The first section will focus on the types of the controls. The second section will be about three basic controls – note and query. In the third section, a simple application will be developed that uses the controls discussed in second section. That’s the agenda for this discussion.
Types of Controls:
PyS60 provides controls or widgets (including dialogs) in two forms. They are:
1. Functions
2. Python types
Controls or widgets under the former mostly are methods where as those in latter category are Python objects implemented in C. Here are the details.
1. Functions:
Many of the dialogs are implemented as functions. In PyS60, dialogs take precedence over the other controls such as textboxes, list-boxes etc. This means if a control and a dialog needs to be shown, then dialog will be shown on top of the control, thus hiding the control. The examples of dialogs implemented as functions are note, query, multi_query etc.
2. Python types
The controls such as textboxes are implemented in C and directly accessed in PyS60. Their precedence is lower than UI implemented as Functions. Text, Listbox and Canvas are examples of Python types. These controls are displayed the instant they are set as the part of the application’s body. In other words, these controls are not displayed until they have been registered as the part of the application.
One of the controls is a dialog that has been implemented as a Python type. It is the Form control. That completes the discussion about the types of UI controls lets move onto next section.
UI Controls – Entry and Notes:
Query and Notes controls are the most commonly used of all the controls. They are used to take input and display messages. Here are the details:
1. Query:
Query is a dialog type. It is used to gather input from the user. It presents a single line text box with a label to the user. Since query is a dialog, so it is implemented as a function. There are three parameters to the function. They are:
i. label
ii. type
iii. initial value
The first two are mandatory parameters whereas the third is optional. Here are the details
i. label
It is the question or prompt that is shown to the user when the dialog box is displayed. The value for this argument is an Unicode string. For example, to show the “Enter name” as the prompt, one will use the following as the value of the argument:
U”Enter name”
ii. type
One can compare it with standard input dialog box that is common on PC. Since it is a dialog, it can be of different dialog types. The type is decided by the type parameter of the query function. The different types are:
a. text – it is a simple text. The text is of Unicode type.
b. code -
c. number – in this case, the input box will accept only numbers and not
decimals
d. date – the input is just date
e. time – to take time as input type needs to be set to time.
f. float – to accept only decimals, this type can be used
All these values are string type. For example to set the type to number, the value passed will be
‘number’
iii. initial value
This is an optional parameter. It sets the initial value shown to the user. However, for the type float, setting this value does not have any effect. For text fields i.e. type having value either ‘text’ or ‘code’, the value for this argument is Unicode. If the type is ‘number’, the value that can be passes is numeric values only. If the type is date, then initial value will be seconds since epoch rounded to the nearest local midnight. For example, to set the initial value to 3, the value passed will be
3
The return value of query is the value provided by the user and the type of the input box. Therefore, to display a query with “Enter any number between 1 and 9” as message, with number as the only acceptable data and 0 as initial value, the statement will be
guess=query(u“Enter any number between 1 and 9”, ‘number’,0)
2. note
note is another control implemented as a dialog. It is used to display messages to the user. This is, again, one of the most common controls in the toolkit of PyS60 as it can be used to display different messages. These messages can be anything from simple message such as information about the size of current file or critical messages such as low battery. The note function takes three arguments. They are
a. text
b. type
c. global
Of these, only text is mandatory argument. The other two are optional. Here are the details.
a. text
It is the message to be shown. It is a Unicode string. For example, to show a message stating that the memory is full, the text argument will be of following format
u”Memory is full”
The u at the starting indicates that it is a Unicode text.
b. type
As stated earlier, the note function can be used to display different kinds of messages. The kind of the message is determined by the value passed for ‘type’ parameter. This parameter can accept the following string values:
i. info – it is the default value, if no value is passed. When type is set to info,
the message shown will be of simple kind. Hence, the icon will have
‘i’ as symbol.
ii. error – to show error messages, one can use this string as the value. If the
type is set to ‘error’, the icon will have ‘e’ as the symbol.
iii. conf – set the type to ‘conf’ when the message to be shown is related to
configuration. An example of configuration is selecting and setting
the fonts. Once a font has been set, a message whose type conf can
be shown to the user informing him/her the name of the new font.
c. global
The value of global argument decides whether, the note displayed is global or not. Global note is a note that will be displayed even if the application calling the note function is not in foreground. The global argument takes an integer value. Any value other than zero is used to make a note or the displayed message global. The default value for this argument is zero. For example, to show a non-global message to the user, the value passed to the global argument will be
0
For example to display a global error message stating that memory is full, the statement will be
note(u”Memory is full”, error, 1)
That brings us to the end of second section. Next section will be about an application using whatever has been discussed until now.
PyS60 in Real World
Lets now see how these controls can be used within an application. The application going to be developed is a simple guessing game. It will perform following tasks
1. Choose a random number
2. Ask the user to enter his or her guess
3. Compare and show the result
4. Ask if he or she will like to continue
So lets start. First the imports
from appuifw import *
As you already know the above statement imports all the classess within appuifw module. Next the application has to choose a random number. However, since this will be a repeating process until the user chooses to stop, choosing the random number will be within a loop. The loop will terminate only when user says he or she will like to stop. The code is as follows
from appuifw import *
continue_guess=true
while continue_guess:
guess_no=random()
The loop will continue till continue_guess becomes false. Next, the application needs to accept user’s guess. To accept the value, query function will be used. The message will be “Enter your guess”, the type will be number (since decimals will not be allowed) and no initial value will be displayed. The value will be saved in user_guess variable. Here is the code
from appuifw import *
continue_guess=true
while continue_guess:
guess_no=random()
user_guess=query(u”Enter your guess”, ‘number’)
Next comes the comparison and displaying the result functionality. First, the user_guess will be checked for validity i.e. if user clicked cancel, it will be null. If it is not null, then it will be compare with the guess_no and appropriate no. will be displayed. If user pressed cancel, a “You opted out” will be shown and the continue_guess will be set to false. Here is the code
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”)
Next comes the code that asks user whether to continue or not. The query box is shown to the user with “Enter Y to continue and N quit”. If Y is entered, the loop is continued else the application is exited.
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
That completes the application. In this discussion, the focus was on simple UI controls. From the next part, complex UI controls such as forms. Till then…

Just want to say what a great blog you got here!
I’ve been around for quite a lot of time, but finally decided to show my appreciation of your work!
Thumbs up, and keep it going!
Cheers
Christian, iwspo.net
Thanks
A+ would read again
I just added your website to my blog roll, I hope you would give some thought to doing the same.