Build Apps

3
Building mobile applications with Python over X-platforms: Part I Victor Miclovich Appfrica Labs [email protected] September 14, 2009 Abstract This paper exists just because it’s much more fun to build something than to actually learn theoretic crap, right? What I try to put off in this article is just a way to learn Python interactively. We shall be looking at the Python basics and Graphical User Interfaces ===> [look out for other papers on Messaging, video interaction, camera APIs, networking, and many other topics.] 1 Python basics Programming languages are a way in which we can talk and make computers do things for us in a log- ical (or destructive) way. Like any language (both natural and formal), there’s got to be a grammar of some kind, doesn’t there? The grammar in this case shall be known as a syntax . Let’s think about the human language (English, French, ...) for a while. We see that there’s a lot of structure, especially, logical structure. Sentences are usually phased in particular ways that convey the meaning a speaker intends to give the intended audience. Isn’t that just so right? I think so. This is what we call having semantics . I want you think of this as just meaning of something. 1.1 Data types Data types defines the quality of ”things” a computer has to interface with. This is includes things like numbers and text. In essence, a computer will only interact with numbers (binary i.e. {0, 1}). Images will in any case, always be converted into numbers. A screen lights up a particular coordinate (pixel) and construct an image with more advanced image analysis and signal processing (we’ll cover this if we’ve got time). Below is a list of data types we can expect to encounter in Python: integers (int) floating point (float) strings or text (str) Data types are quite easy to understand; we shall discuss ways we can handle this kind of data throught mathematical processes like addition, subtraction and division. Integers integers are numbers that don’t have any fractional parts! This refers to numbers like 1, 2, 3, 4, ..., etc. floats floating point are numbers with fractional parts or simply decimal numbers Strings Strings are just textual parts of data; they could be alphabetic symbols, numbers or al- phanumeric 2 Python for Symbian OS The Symbian OS is a mobile operating system. Many such installations are found in many modern Nokia, Samsung, LG phones. We shall be using Python to illustrate the design and development 1

Transcript of Build Apps

Page 1: Build Apps

Building mobile applications with Python over

X-platforms: Part I

Victor MiclovichAppfrica Labs

[email protected]

September 14, 2009

Abstract

This paper exists just because it’s much more funto build something than to actually learn theoreticcrap, right? What I try to put off in this article isjust a way to learn Python interactively.We shall be looking at the Python basics andGraphical User Interfaces ===> [look out for otherpapers on Messaging, video interaction, cameraAPIs, networking, and many other topics.]

1 Python basics

Programming languages are a way in which we cantalk and make computers do things for us in a log-ical (or destructive) way. Like any language (bothnatural and formal), there’s got to be a grammarof some kind, doesn’t there? The grammar in thiscase shall be known as a syntax .Let’s think about the human language (English,French, ...) for a while. We see that there’s a lotof structure, especially, logical structure. Sentencesare usually phased in particular ways that conveythe meaning a speaker intends to give the intendedaudience. Isn’t that just so right? I think so. Thisis what we call having semantics. I want youthink of this as just meaning of something.

1.1 Data types

Data types defines the quality of ”things” acomputer has to interface with. This is includesthings like numbers and text. In essence, acomputer will only interact with numbers (binaryi.e. {0, 1}). Images will in any case, always be

converted into numbers. A screen lights up aparticular coordinate (pixel) and construct animage with more advanced image analysis andsignal processing (we’ll cover this if we’ve got time).

Below is a list of data types we can expect toencounter in Python:

• integers (int)

• floating point (float)

• strings or text (str)

Data types are quite easy to understand; we shalldiscuss ways we can handle this kind of datathrought mathematical processes like addition,subtraction and division.

Integers integers are numbers that don’t have anyfractional parts! This refers to numbers like1, 2, 3, 4, ..., etc.

floats floating point are numbers with fractionalparts or simply decimal numbers

Strings Strings are just textual parts of data; theycould be alphabetic symbols, numbers or al-phanumeric

2 Python for Symbian OS

The Symbian OS is a mobile operating system.Many such installations are found in many modernNokia, Samsung, LG phones. We shall be usingPython to illustrate the design and development

1

Page 2: Build Apps

of mobile applications. And while we are at it,ready yourselves with either a phone emulator orthe actual phone

We shall use Nokia smartphones as our platformfor learning Python in a more interesting way.Below are instructions on how to install Python ina Nokia (S60) phone:

• Download the Installation files, you will findthem at SourceForge’s PyS60 project page,http://sourceforge.net/projects/pys60. Get arecent and compartable version with yourphone.

– the PyS60 interpreter. (these are usuallysigned and have a .sis extension).

– the pythonscript shell (there are versionnumbers included, you should be mindfulof that).

• Download the Python SDK (software develop-ment kit); this is is at http://forum.nokia.com.

• A phone memory/data card (you will need aplace to store the programs you run for yourphone).

• You will need a computer that runs either Win-dows, Linux or Mac OS X.

• A USB cable or bluetooth to connect with thecomputer.

As we move on, you will experience the use of bothyour phone and computer; you can search for moretips; I will explain certain features practically.

3 PyS60: your first program

This program consists of three lines of code; I willexplain what each line does, but first what resultdo we want from the program???

1. We want it to display a text input field; a placewhere the phone’s user can type in stuff!

2. Display a pop-up note that says somethig like”Greetings from:” followed by whatever theuser typed into the input field.

The code is:

import appuifwname = appuifw.query(u"Type yourname:","text")appuifw.note(u"Hello World! Greetingsfrom: "+str(name),"info")

In the first line of code, we import the appuifwmodule, which handles user interface elementssuch as text input fields and popup notes.

In the second line of code, we create a single-fielddialog using the query() function (think of thisas f(x, y)) which is found inside the appuifwmodule with two parameters: label and type. Thelabel is the text we want printed out and typerefers to whether the text should appear as awarning popup, info popup, danger popup or someother definitive standard. You may have noticed acertain character u that we placed before our stringbecause the only text our phone understands isunicode: a standard way by which textual infor-mation is portrayed and that has internationalizedsupport (this means many natural languages aresupported e.g. Russian, Chinese, Arabic, etc.).

Other types are supported, not just ”text” type.They include ”number”, ”date”, ”time”, ”query”,and ”code”.

Introduction to GUIs

This is the easiest thing you will learn about inPyS60 (our code name for Python in mobile). Inthis section I demonstrate the following stuff youwill interface with your phone in any case.

• note - popup notes

• query - single-field input dialog

• multi-query - two-field text input dialog

• popup menu - simple menu

• selection list - simple list with find pane (likea search)

• multi-selection list - list to make the multipleselections

2

Page 3: Build Apps

Example 1: Text input field andpopup note

Native UI (User Interface) elments that PyS60 of-fers are accessible through a module called: ap-plication user interface framework which in codeis written appuifw. It is an interface to the S60UI application framework that takes care of all UIfunctionalities on the S60 platform. But first anintroduction to what a module is in Python.

Python lesson: module

A module is a file that contains a collection ofrelated functions and data grouped together.PyS60 comes with a rich set of modules, forexample messaging to handle SMS functionalites,camera for taking photos, and appuifw, whichprovides ready-to-use UI elements.

The modules’ contents are described in detail inthe Python library reference and Python for S60API documentation.To use a module (group of functionalities or abili-ties) in your code, it must be imported at the be-ginning of the script, for example:

import appuifw

And to pick off functionalities from your module,we write the module name, a dot and then name offunction that we want!

appuifw.query(label,type)

Here, appuifw is the module name and query isthe function you want to use.You may import many modules using a single im-port statement:

import appuifw, e32 This imports two modules:appuifw and e32.

In the first example several sections ago we used thequery() and note() functions... (remember just likef(x)) and the functions belong to appuifw module.These functions generate UI elements, dialogs, thatare displayed on the when the PyS60 interpreterexecutes the script. They become visible on thephone screen as soon as the corresponding Pythonfunction is called.

Dialogs, Menus, and Selectionlists

Single-Field Dialog: query

Its syntax is query(label,type[, initialvalue])Code hack:

appuifw.query(u"Type a word: ", "text",u"Foo")

This fuction shows a single-file dialog. The dialogcan include some instruction text that is passed asa string (by putting the u in front of the string ””)

Exercise

The aim of this section is to allow you guys to prac-tice! You can practice with your phone or emulator.

import appuifwappuifw.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 likePyS60","query")

3