Mobile Programming in Python using PyS60: UI Controls

In this discussion I will focus on the basics of using a UI library provided by PyS60. The first section will focus on the types of controls. The second and third sections will be about two basic controls – query and note. In the fourth section, a simple application will be developed that uses the controls discussed in the second and third sections. That’s the agenda for this discussion.

Graphic user interfaces (GUIs) changed the way a program communicates with the user. It made applications interactive. Today, interactivity is the norm. Mobiles are no exceptions. PyS60 makes creating GUI-based applications easy. It can be thought of as one of the RAD environments for the Symbian OS.

Types of Controls

PyS60 provides controls or widgets (including dialogs) in two forms. They are Functions and Python types. Controls or widgets under the former are mostly methods, where as those in the latter category are Python objects implemented in C. Here are the details.

Functions: Many of the dialogs are implemented as functions. In PyS60, dialogs take precedence over the other controls, such as text boxes, list-boxes, etc. This means if a control and a dialog both need to be shown, then the 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.

Python types: The controls, such as text boxes, are implemented in C and directly accessed in PyS60. Their precedence is lower than the UI implemented as Functions. Text, Listbox, and Canvas are examples of Python types. These controls are displayed the instant they are set as part of the application’s body. In other words, these controls are not displayed until they have been registered as 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 our discussion about the types of UI controls. Let’s move on to next section.

{mospagebreak title=UI Controls: Query 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:

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, it is also implemented as a function. There are three parameters to the function. They are label, type, and initial value. The first two are mandatory parameters, whereas the third is optional. Here are the details:

The label is the question or prompt that is shown to the user when the dialog box is displayed. The value for this argument is a Unicode string. For example, to show “Enter name” as the prompt, one will use the following as the value of the argument:

U”Enter name”

Type can be compared with the standard input dialog box that is common on the 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:

  • Text – is a simple text. The text is of Unicode type.
  • Code 
  • Number – in this case, the input box will accept only numbers and not decimals.
  • Date – the input is just a date. 
  • Time – to take time as input, the type needs to be set to time.
  • Float – to accept only decimals, this type can be used.


All of these are string values. For example, to set the type to number, the value passed will be:

number’

The initial value 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. the type having the value of either ‘text’ or ‘code’), the value for this argument is Unicode. If the type is ‘number,’ the value that can be passed is a numeric value only. If the type is date, then the initial value will be seconds, since an epoch is 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 the 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 a message, with number as the only acceptable data type and 0 as the initial value, the statement will be:

guess=query(u“Enter any number between 1 and 9”, ‘number’,0)

This is the end of the second section. In the next section, we will discuss the note control.

{mospagebreak title=The Note Control}

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. It can be used to display different messages. These messages can be anything from simple messages, such as information about the size of the current file or critical messages, such as low battery. The note function takes three arguments. They are text, type, and global. Of these, only text is a mandatory argument. The other two are optional. Here are the details.

Text 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 in the following format:

u”Memory is full”

The u at the start indicates that it is a Unicode text.

Now we’ll discuss type. As stated earlier, the note function can be used to display different kinds of messages. The kind of message is determined by the value passed for ‘type’ parameter. This parameter can accept the following string values:

  • Info – is the default value, if no value is passed. When the type is set to info, the message shown will be simple. Hence, the icon will have ‘i’ as a symbol.
  • 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.
  • 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 is conf can be shown to the user informing him or her of the name of the new font.

The global argument decides whether the note displayed is global or not. A global note is a note that will be displayed even if the application calling the note function is not in the 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 the memory is full, the statement will be:

note(u”Memory is full”, error, 1)

That brings us to the end of the third section. The next section will be about building an application using what we’ve discussed in this article.

{mospagebreak title=PyS60 in Real World}

Let’s see how these controls can be used within an application. The application that’s going to be developed is a simple guessing game. It will perform the 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 let’s start. First the imports:

from appuifw import *

As you already know, the above statement imports all the classes within the 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 the user says he or she would like to stop. The code is as follows:

from appuifw import *


continue_guess=true


while continue_guess:

guess_no=random()

The loop will continue until continue_guess becomes false. Next, the application needs to accept the user’s guess. To accept the value, the 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 the user clicked cancel, it would be null). If it is not null, then it will be compared with the guess_no, and the appropriate number will be displayed. If the 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 the user whether or not to continue. The query box is shown to the user with “Enter Y to continue and N quit.” If Y is entered, the loop is continued, otherwise 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. The next part will discuss complex UI controls, such as forms. Till then…

[gp-comments width="770" linklove="off" ]
antalya escort bayan antalya escort bayan