Building mobile applications with Python over X-platforms: Part I

Victor Miclovich Appfrica Labs victor.miclovich@appfrica.org September 14, 2009

Abstract

converted into numbers. A screen lights up a particular coordinate (pixel) and construct an This paper exists just because it’s much more fun image with more advanced image analysis and to build something than to actually learn theoretic signal processing (we’ll cover this if we’ve got time). crap, right? What I try to put off in this article is just a way to learn Python interactively. Below is a list of data types we can expect to We shall be looking at the Python basics and encounter in Python: Graphical User Interfaces ===> [look out for other • integers (int) papers on Messaging, video interaction, camera APIs, networking, and many other topics.] • floating point (float)

1

Python basics

• 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 alphanumeric

Programming languages are a way in which we can talk and make computers do things for us in a logical (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

2

Python for Symbian OS

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 1

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

of mobile applications. And while we are at it, import appuifw ready yourselves with either a phone emulator or name = appuifw.query(u"Type your the actual phone name:","text") appuifw.note(u"Hello World! Greetings We shall use Nokia smartphones as our platform from: "+str(name),"info") for learning Python in a more interesting way. Below are instructions on how to install Python in In the first line of code, we import the appuifw module, which handles user interface elements a Nokia (S60) phone: such as text input fields and popup notes. • Download the Installation files, you will find them at SourceForge’s PyS60 project page, In the second line of code, we create a single-field http://sourceforge.net/projects/pys60. Get a dialog using the query() function (think of this recent and compartable version with your as f (x, y)) which is found inside the appuifw phone. module with two parameters: label and type. The label is the text we want printed out and type refers to whether the text should appear as a warning popup, info popup, danger popup or some – the pythonscript shell (there are version other definitive standard. You may have noticed a numbers included, you should be mindful certain character u that we placed before our string of that). because the only text our phone understands is unicode: a standard way by which textual infor• Download the Python SDK (software developmation is portrayed and that has internationalized ment kit); this is is at http://forum.nokia.com. support (this means many natural languages are • A phone memory/data card (you will need a supported e.g. Russian, Chinese, Arabic, etc.). place to store the programs you run for your Other types are supported, not just ”text” type. phone). They include ”number”, ”date”, ”time”, ”query”, • You will need a computer that runs either Win- and ”code”. dows, Linux or Mac OS X. – the PyS60 interpreter. (these are usually signed and have a .sis extension). • A USB cable or bluetooth to connect with the computer.

Introduction to GUIs

This is the easiest thing you will learn about in As we move on, you will experience the use of both PyS60 (our code name for Python in mobile). In your phone and computer; you can search for more this section I demonstrate the following stuff you tips; I will explain certain features practically. will interface with your phone in any case.

3

PyS60: your first program

• 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 (like a search) • multi-selection list - list to make the multiple selections 2

This program consists of three lines of code; I will explain what each line does, but first what result do we want from the program??? 1. We want it to display a text input field; a place where the phone’s user can type in stuff! 2. Display a pop-up note that says somethig like ”Greetings from:” followed by whatever the user typed into the input field. The code is:

Example 1: popup note

Text input field and Dialogs, Menus, and Selection

lists
Single-Field Dialog: query
Its syntax value]) Code hack: is query(label,type[, initial

Native UI (User Interface) elments that PyS60 offers are accessible through a module called: application user interface framework which in code is written appuifw. It is an interface to the S60 UI application framework that takes care of all UI functionalities on the S60 platform. But first an introduction to what a module is in Python. Python lesson: module

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

", "text",

This fuction shows a single-file dialog. The dialog can include some instruction text that is passed as A module is a file that contains a collection of a string (by putting the u in front of the string ””) related functions and data grouped together. PyS60 comes with a rich set of modules, for example messaging to handle SMS functionalites, Exercise camera for taking photos, and appuifw, which The aim of this section is to allow you guys to pracprovides ready-to-use UI elements. tice! You can practice with your phone or emulator. The modules’ contents are described in detail in the Python library reference and Python for S60 API documentation. To use a module (group of functionalities or abilities) in your code, it must be imported at the beginning 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 of function that we want! appuifw.query(label,type) Here, appuifw is the module name and query is the function you want to use. You may import many modules using a single import statement: import appuifw, e32 This imports two modules: appuifw and e32. In the first example several sections ago we used the query() and note() functions... (remember just like f (x)) and the functions belong to appuifw module. These functions generate UI elements, dialogs, that are displayed on the when the PyS60 interpreter executes the script. They become visible on the phone screen as soon as the corresponding Python function is called. 3 import appuifw appuifw.query(u"Type a appuifw.query(u"Type a appuifw.query(u"Type a appuifw.query(u"Type a appuifw.query(u"Type a appuifw.query(u"Do you PyS60","query") word:","text") number:","number") date:","date") time:","time") password:" "code") like

Sign up to vote on this title
UsefulNot useful