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
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 set which toolbar is active when my activity starts up?
The "set_current_toolbar()" method in the ToolBox class allows you to programmatically set which toolbar is active. Using this, you can include code in your __init__() method to ensure that a particular toolbar is selected upon startup.
#Set the active toolbar using an integer index assigned to each toolbar in your activity. self.toolbox.set_current_toolbar(1);
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.