Sugar Code Snippets: Difference between revisions

From OLPC
Jump to navigation Jump to search
m (Added profiling link)
No edit summary
Line 9: Line 9:
import gtk
import gtk
from sugar.activity.Activity import Activity
from sugar.activity import activity
from sugar.graphics.toolbar import Toolbar
from sugar.graphics.toolbar import Toolbar
from sugar.graphics.button import Button
from sugar.graphics.iconbutton import IconButton
class FooActivity(Activity):
class FooActivity(activity.Activity):
def __init__(self):
def __init__(self, handle):
Activity.__init__(self)
activity.Activity.__init__(self, handle)
canvas = hippo.Canvas()
canvas = hippo.Canvas()
Line 27: Line 27:
vbox.append(toolbar)
vbox.append(toolbar)
button = Button('theme:stock-close')
button = IconButton(icon_name='theme:stock-close')
button.connect("activated", self._button_activated_cb)
button.connect("activated", self._button_activated_cb)
toolbar.append(button)
toolbar.append(button)

Revision as of 12:49, 24 February 2007

Other snippets might be found on the development wiki.

Toolbar

This snippet shows how an activity would have a toolbar with a button and a gtk.TextView widget embedded in a hippo Canvas:

import logging
import hippo
import gtk

from sugar.activity import activity
from sugar.graphics.toolbar import Toolbar
from sugar.graphics.iconbutton import IconButton

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

        canvas = hippo.Canvas()
        self.add(canvas)
        canvas.show()

        vbox = hippo.CanvasBox()
        canvas.set_root(vbox)

        toolbar = Toolbar()
        vbox.append(toolbar)

        button = IconButton(icon_name='theme:stock-close')
        button.connect("activated", self._button_activated_cb)
        toolbar.append(button)

        textViewWidget = hippo.CanvasWidget()
        vbox.append(textViewWidget, hippo.PACK_EXPAND)

        textView = gtk.TextView()
        textView.get_buffer().set_text('Write here!', -1)
        textViewWidget.props.widget = textView

    def _button_activated_cb(self, button):
        logging.debug('FooActivity._button_activated_cb')

Files

This snippet shows how to get a path to files in the running Activity:

from sugar import env
activity_path = env.get_bundle_path()

Profiling

See Sugar Performance for a profiling snippet