
| Sample Code |
Introduction to This Document
This guide explains how to develop a basic Python* application for the Moblin platform. We will guide you through writing and analyzing a Hildon application which was written in Python.
First we will provide a brief introduction to Moblin development. Then we will describe how to develop a basic Python application under the Moblin platform. We use a sample Hildon application, which can be found here.
Introduction to Python
Python is an interpreted language, and the interpreter can be used interactively, which makes it easy to write throw-away programs, or to test functions during bottom-up program development[1]. Python is powerful and fully defined on expressions, statements, data types, functions, modules, exceptions, and user-defined classes[1]. Python allows you to split your program into modules that can be reused in other Python programs. Python comes with a large collection of standard modules that you can use as the basis of your programs. Some of these modules provide things like file I/O, system calls, sockets, and even interfaces to graphical user interface (GUI) toolkits, like Gtk[1]. In general, if you'd like to write a small custom database, a specialized GUI application, or a simple game, Python is just the language for you.
For information about basic programming using Python, you may refer to
the Python Tutorial at
http://docs.python.org/tut or the Python documentation at
http://docs.python.org/.
Get Started
Python is a good language for quickly developing applications and prototyping. Python has modules which provides support for file I/O, system calls, threading, GTK (PyGTK), etc[3], so Python can be used to conveniently develop applications for the Moblin platform.
Before developing applications, you need to configure the development environment. We recommend that you develop in the target device's build-environment, which can be created using Moblin Image Creator. This SDK includes guides on installing and using Moblin Image Creator.
Once you have created the development environment, you must make sure you have the required libraries and tools installed. You must make sure Python, together with its core bindings (python-dbus, python-gtk2, python-hildon), are installed[4]. You can use apt or yum (depending on the package manager you are using) to install them. For example, when using apt to install, you can run apt-get install python python-dbus python-gtk2 python-hildon.
Below we provide a simple Python Hildon application, and introduce how to write and execute it on the Moblin platform.
Writing our first Moblin Python Hello World
First, we are going to write a simple hello world python application for the Moblin platform. You should use Moblin Image Creator and chroot into the target file-system. We will be adding all of our code to a script,
py-hello-world. First, we will write the script, which will launch a GUI app that displays "Hello World! (hildon)" and can quit. Then we will create a desktop entry file, and save that and the
Hello World icons to the appropriate folders. This allows the Hildon Desktop Environment to show the icon from the
Home Screen UI. You will be able to select the application's icon to launch the application.
Writing Hello World
Here we actually begin writing the script.
Let's explain the Hildon-specific scripts above, to see how to create a Hildon window in Python.
The required Python modules for a moblin-hildon applications are:We need to create a HildonHelloWorld class, that is derived from the HildonProgram class. The HildonProgram class is a programmer commodity used to apply program-wide settings to all Hildon windows used by the application (for example, this allows you to have a common menu and toolbar on all windows). In addition, HildonProgram also manages other program-wide issues, such as hibernating [4].
The HildonHelloWorld(hildon.Program) class:The Desktop Entry File & Icon Files
Create a desktop file py-hello-world.desktop associated with this Python application. The content of this file is:Add this desktop file into /usr/share/mobile-basic-flash/applications/ on the target.
Also, if you downloaded our sample code, add the py-hello-world.png icon files into /usr/share/icons/hicolor/16x16/apps/, /usr/share/icons/hicolor/32x32/apps/, and /usr/share/icons/hicolor/64x64/apps/ on the target.
Adding the D-Bus Service File
In this step, you should create a D-Bus service file, making
sure that the value of the "Name" tag is exactly the same as the
value of the "X-Osso-Service" tag in the desktop entry file we
created above. We will do more with the D-Bus in a
later section. For now you should
just create the service file associated with this Python
application, and call it org.moblin.pyhelloworld.service.
Then add this service file into /usr/share/dbus-1/services/ on the target.
Finally, add the Python script py-hello-world into /usr/bin/ on the target.
Now, restart the Moblin desktop and you will find the icon associated with py-hello-world.
Figure 3-1: A screenshot of Moblin desktop. You can see the Hello World desktop icon, "Hello World", on the bottom row.
Adding a GtkMenu
A Hildon-based application uses a standard GtkMenu widget to create its menu. You can add the menu to HildonProgram as a common menu which will appear in all the HildonWindow registered to this HildonProgram.
In the _add_menu method, we first create a GtkMenu widget, and add the Quit menu item. We need to connect the Quit item to the quit signal and add this menu item to the menu. We will add the menu to HildonHelloWorld as a common menu.
Then call this method in the __init__ method:Adding a GtkToolbar
The Hildon-based application uses a standard GtkToolbar widget to create its toolbar. You can add the toolbar to the HildonProgram as a common toolbar which will appear in all the HildonWindow registered to this HildonProgram.
In the _add_toolbar method, we created a GtkToolbar widget and the "Quit" toolbar item. Now we need to connect the quit signal to the "Quit" item and add this toolbar item to the toolbar. Then we need to add the toolbar to HildonHelloWorld, as a common toolbar and display the GtkToolbar.
Then call this method in the __init__ method:From the Moblin desktop, click the Hello World icon to run our application. This is a screenshot of the Python application. Notice that the application has a menu at the top with a "Quit" button and a toolbar at the bottom.

Adding D-Bus to our Hello World
In the last step, we will add a D-Bus class to our Hello World application and create a D-Bus service file.
Exporting DBus Object
Moblin uses D-Bus as the primary interprocess communication
mechanism for applications. More detailed information on D-Bus can
be found at the document
D-Bus Programming. Here we describe how to initialize a D-Bus object in Python. One symptom of missing
this initialization is that the application will start from the task navigator, but
will close automatically after a while.
Create a DBusObject class derived from the dbus.service.Object class, which is a base class for exporting your own Objects across D-Bus. With the DBusObject class, the Python application is able to connect to the D-Bus session message bus, export methods, do the remote procedure call, etc. You can refer to the dbus.service help doc within Python for more detailed information.
We will continue working with our py-hello-world.py sample. To create the D-Bus object class, open the file and add the this class to the script:If you restart the Hello World Python application, registering it to the D-Bus will make sure that the task navigator will not kill it.
Python Binding Libraries
Besides standard modules, there are many Python bindings you can leverage to make your programs, such as Python GStreamer*, Python Gtk, Python D-Bus, and Python Banshee-Helix. If you want to see if there are Python bindings for an existing project, you can go to the web site for the project and check if there is a Python sub-project inside.
Reference Documents
The following documents were used as references in the writing of this document. Refer to these documents for further details.