Tuesday, 15 January 2013

Writing a Program in Python for S60


Writing a Program in Python for S60

Example 1: First PyS60 program
We write a simple program consisting of three lines of code as our first
real example. It should do the following:
1. Display a text input field on the screen; the instruction in the text
field should say ‘Type your name’ .
2. Display a popup note stating ‘Greetings from:’ followed by whatever
the user typed into the text input field .


The code to do this is as follows:
import appuifw
name = appuifw.query(u"Type your name:", "text")
appuifw.note(u"Hello world! Greetings from: " + str(name), "info")


In the first line of code, we import the appuifw module, which
handles user interface elements such as text input fields and popup notes.
In the second line of code, we create a single-field dialog (Figure
2.11(a)) using the query() function of the appuifw module with
two parameters: label and type. For the first parameter, label, we put
the text u"Type your name:". The u is required because the phone
understands only text declared as Unicode and the quotation marks are
needed because the label parameter must be given as a string. As the
second parameter, type, we put "text", to declare the input field as
a text dialog. Other possible types are "number", "date", "time",
"query" and "code". The two parameters are separated by a comma.
This line of code also creates a variable called name which will receive
the result of the text input field after the user has typed something into
the dialog.
In the third line of code, we create a popup note (Figure 2.11(b))
using the note() function of the appuifw module with two parameters:
label and type. For the first parameter, label, we put the text u"Hello
world! Greetings from: "+ str(name). This is the text that will
appear inside the popup note. Again, the string must be given in quotation
marks but the popup note will also include the result that the user has
typed in, so we add the contents of the variable name to the string.
As the second parameter, type, we put "info", to declare the popup
note as information; that causes a green exclamation mark to appear
inside the popup note. Other possible types are "error", showing a
red exclamation mark, and "conf"(confirmation), showing an animated
check mark. The two parameters are separated with a comma.
Type this example into a file and upload it to your phone for testing,
by following the instructions.


No comments:

Post a Comment