Development Team/Almanac/sugar.activity.activity

The sugar.activity.activity package includes several important classes that are needed to run a basic activity.

Class: Activity

What are activity id's? How do I obtain the activity id for an instance of my activity?

The activity id is sort of like the unix process id (PID). However, unlike PIDs it is only different for each new instance (with create_jobject = True set) and stays the same every time a user resumes an activity. This is also the identity of your Activity to other XOs for use when sharing.

You can use the get_id() method in the activity class to get the activity id for your activity. Since most activities should inherit from the Activity class, you can actually just get a handle on the top level object for your activity and use that to call get_id.

 ### TOOLBAREXAMPLE IS AN ACTIVITY THAT SUBCLASSES SUGAR'S ACTIVITY CLASS
 class ToolbarExample(activity.Activity):
     def __init__(self, handle):
         activity.Activity.__init__(self, handle)
         ...
         #store the activity id for this variable in the variable my_id
         my_id = self.get_id()
         ...

How do I create a new activity that is derived from the base Activity class?

All activities must implement a class derived from the 'Activity' class. The convention is to call it ActivitynameActivity, but this is not required as the activity.info file associated with your activity will tell the sugar-shell which class to start.

from sugar.activity import activity
...
class ToolbarExample(activity.Activity):

    def __init__(self, handle):
        activity.Activity.__init__(self, handle)

Class: ActivityToolbox

What is the standard toolbox needed in most activities and how do I create it?

The ActivityToolbox is the standard toolbox that should be a part of any sugar activity. This toolbox starts out with an ActivityToolbar, which contains standard controls for saving to the journal, closing the activity and other basic activity tasks.

To create a standard ActivityToolbox for your activity, place the following code in your activity's __init__ method:

       #### CREATE TOOLBOX
       # 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)
       self.set_toolbox(toolbox)
       toolbox.show()


How do I get a handle on the standard activity toolbar given an ActivityToolbox object?

Use the get_activity_toolbar() method in ActivityToolbox to get a handle ont he activity toolbar.

       #Get the activity toolbar from our ActivityToolbox object
       activityToolbar = toolbox.get_activity_toolbar()

Class: ActivityToolbar

Class: EditToolbar

How do I add a standard edit toolbar to my activity?

The activity package has a standard edit toolbar with the following members:

  • undo -- the undo button
  • redo -- the redo button
  • copy -- the copy button
  • paste -- the paste button
  • separator -- A separator between undo/redo and copy/paste

You can create a standard edit tool bar using code similar to the following in the __init__ method of your activity's class after you have created a toolbox:

       #### EDIT TOOLBAR
       # Create the edit toolbar:
       self._edit_toolbar = activity.EditToolbar()
       # Add the edit toolbar:
       toolbox.add_toolbar(_('Edit'), self._edit_toolbar)
       # And make it visible:
       self._edit_toolbar.show()


How do I hide a button in the edit toolbar that is not needed in my activity?

The following code shows how to hide the undo button. You can also apply similar code to hide the redo, copy and paste buttons.

       #hide the undo button
       self._edit_toolbar.undo.props.visible=False


How do I disable and enable a button on the edit toolbar?

Some buttons, such as copy, may need to be disabled under certain circumstances (eg. there is nothing to copy). This can be done by changing the sensitivity of the widget, as the following code shows:

       #disable the use of the copy button for now. 
       self._edit_toolbar.copy.set_sensitive(False)

You can enable buttons by simply passing True to the set_sensitive() method.