Activity tutorial

Revision as of 19:56, 30 March 2008 by Tony37 (talk | contribs)
Jump to: navigation, search
  english | 日本語 | 한국어 | português | español HowTo [ID# 121594]  +/-  

<< Tutorials

This tutorial explains step by step how to create the Hello World Activity bundle. You can download a completed .xo package, though the package will not exactly match the below contents.

This tutorial assumes you have read the Developer's Manual, have installed a Sugar development environment and wish to use the PyGTK development approach (the standard approach to developing activities for the OLPC).

Getting started

  • Create the bundle directory structure:
mkdir -p HelloWorldActivity.activity/activity
  • Write the file, to describe your bundle in the activity sub-directory (e.g. HelloWorldActivity.activity/activity/ The Activity Bundles specification explain in detail the meaning of each field.
    • Note: in a joyride-1525 build, sugar could not locate the icon when I tried this unless I removed the '.svg' extension from the 'icon = activity-helloworld.svg' line.
name = HelloWorld
service_name = org.laptop.HelloWorldActivity
class = HelloWorldActivity.HelloWorldActivity
icon = activity-helloworld
activity_version = 1
show_launcher = yes
  • Design an icon for your activity by following the instructions on making icons for Sugar and place it in the activity sub-directory. The file name should match the icon file name specified in the info file (e.g. activity-helloworld.svg). The SVG for your icon will look something like:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
 "" [
  <!ENTITY stroke_color "#666666">
  <!ENTITY fill_color "#FFFFFF">
<svg xmlns="" width="55" height="55">
  <rect x="5" y="5" width="45" height="45"
  • Write the script in the top level directory (e.g. HelloWorldActivity.activity/, which in most cases will look like this:
#!/usr/bin/env python
	from sugar.activity import bundlebuilder
except ImportError:
	import os
	os.system("find ./ | sed 's,^./,HelloWorldActivity.activity/,g' > MANIFEST")
	os.system('rm HelloWorldActivity.xo')
	os.system('zip -r HelloWorldActivity.xo HelloWorldActivity.activity')
	os.system('mv HelloWorldActivity.xo ./HelloWorldActivity.activity')

  • Code your activity in Python. The name you specified in the .info file as "class" is the name of the class which runs your code. For the file above, we specify a top-level module named HelloWorldActivity.HelloWorldActivity (please note that the use of uppercase names in module names is considered poor Python style, feel free to use an activity name with more standard style names).
 from sugar.activity import activity
 import logging
 import sys, os
 import gtk
 class HelloWorldActivity(activity.Activity):
     def hello(self, widget, data=None):'Hello World')
     def __init__(self, handle):
         print "running activity init", handle
         activity.Activity.__init__(self, handle)
         print "activity running"
         # Creates the Toolbox. It contains the Activity Toolbar, which is the
         # bar that appears on every Sugar window and contains essential
         # functionalities, such as the 'Collaborate' and 'Close' buttons.
         toolbox = activity.ActivityToolbox(self)
         # Creates a new button with the label "Hello World".
         self.button = gtk.Button("Hello World")
         # When the button receives the "clicked" signal, it will call the
         # function hello() passing it None as its argument.  The hello()
         # function is defined above.
         self.button.connect("clicked", self.hello, None)
         # Set the button to be our canvas. The canvas is the main section of
         # every Sugar Window. It fills all the area below the toolbox.
         # The final step is to display this newly created widget.
         print "AT END OF THE CLASS"

The above file is called

  • Create a MANIFEST (e.g. HelloWorldActivity.activity/MANIFEST), containing the list of the files to include in the package. (Note: Be sure not to leave blank lines at the end of the file.) This script does that in linux:
find ./ | sed 's,^./,HelloWorldActivity.activity/,g' > MANIFEST

Your directory structure should now look like this:


Make sure that all your python files have the required permissions to be used.

chmod a+x
chmod a+x

Setup your bundle for development (must be user olpc when you do this) to become user olpc, type: su - olpc

If you are prompted for a password, trying using: su

python dev

(It appears this just creates a symlink in ~/Activities .)


If you now run sugar the activity icon should be visible on the frame. (You have to restart sugar to get it to pick up the change if you just installed it. Hit ctrl-alt-erase.)

You can also edit the code in your bundle directory directly. Note that the first time your Activity is launched, it leaves a process around even if you close the window, so you must kill the sugar-activity-factory to get it to reload when you click again.


Create a .xo package to distribute your bundle. (An .xo file is essentially a zip file built from the MANIFEST with some extra metadata, like a JAR file. It also has some localization ability, and in the future we expect to be able to sign these too.) The bundle name is automatically generated from the 'name' and 'activity_version' values found in the file, separated by a dash, with a .xo extension.

 ./ dist

To install the xo on a laptop you can use the installer script.

 sugar-install-bundle HelloWorld-1.xo

Trouble Shooting

You can find application logs in /home/olpc/.sugar/default/logs. If the sample fails to compile, sugar will show you the icon in the circle but remain forever at the "Starting..." tag when you hover your mouse over it. You'll have to restart sugar with <ctrl-alt-erase> in order to remove the icon. You can find the cause of your trouble in the the log named for the "service-name" in your file, in this case "org.laptop.HelloWorldActivity".

See also