Native UI Elements – Dialogs, Menus and Selection
Lists
Let’s now look in detail at some of the native UI elements or dialogs thatPyS60 offers.
Single-Field Dialog: query
Syntax: query(label, type[, initial value ])
Example code:
appuifw.query(u"Type a word:", "text", u"Foo")
This function shows a single-field dialog. The dialog can include some
instruction text that is passed as a string (by putting the u in front of the
string) to the parameter, label. The type of the dialog is defined by
the parameter, type. The value of type can be any of the following
strings: "text", "number", "date", "time", "code", "query" or
"float"
An initial value can be included, so the dialog shows a default input
value when it appears on the phone screen. This is done simply by adding
the initial value as an input parameter to the function, for example:
appuifw.query(u"Type a word:", "text", u"Foo")
The return value of the appuifw.query() function depends on the
type parameter:
• For text fields (types of ‘text’ and ‘code’), the return value is a Unicode
string.
• For number fields, it is an integer.
• For date fields, it is the number of seconds since the epoch (0:00 on
1 January 1970, UTC) rounded down to the nearest local midnight.
Exercise
To see how the various single-input dialogs appear on your phone, typethe lines of code in Example 2 into your text editor and save the file with
a name ending .py. Transfer the .py file to your phone or to the emulator
as described in Chapter 2. Start the PyS60 interpreter on the phone and
select ‘Options’. Select ‘Run script’, choose your script from the list, select
‘OK’ and see what happens.
You should see all the single-input dialogs as in Figure 3.1 appear on
your phone’s screen one by one. Each dialog will wait for you to type
something in and select ‘OK’.
Example 2: Various dialogs
If you want, change the instruction text (the label parameter) of thequery function. As you can see, we need to import the appuifw module
at the beginning of the script to make this work.
import appuifw
appuifw.query(u"Type a word:", "text")
appuifw.query(u"Type a number:", "number")
appuifw.query(u"Type a date:", "date")
appuifw.query(u"Type a time:", "time")
appuifw.query(u"Type a password:", "code")
appuifw.query(u"Do you like PyS60", "query")
Since Symbian OS and the S60 platform are used all over the world,
they need to be able to show text written in various languages and writing
systems. Unicode provides a consistent way to encode text written in
any writing system. In order to be globally compatible, all text-related
functionalities in S60 and Symbian OS accept only Unicode strings. In
Python, the ‘u’ character in front of a string denotes that the text is written
in Unicode. If you see strange boxes on the screen instead of characters,
you have probably forgotten to put ‘u’ in front of the corresponding text.
We talk more about Unicode conversions in Section 6.2.
No comments:
Post a Comment