Difference between revisions of "Development Team/Almanac/Code Snippets"

From Sugar Labs
Jump to navigation Jump to search
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
Other snippets might be found on the [http://dev.laptop.org/wiki/Code development wiki].
+
{{OLPC}}
  
 
= Toolbar =
 
= Toolbar =
Line 9: Line 9:
 
  import gtk
 
  import gtk
 
   
 
   
 +
from gettext import gettext as _
 +
 
  from sugar.activity import activity
 
  from sugar.activity import activity
 
  from sugar.graphics.toolbar import Toolbar
 
  from sugar.graphics.toolbar import Toolbar
Line 24: Line 26:
 
     def __init__(self, handle):
 
     def __init__(self, handle):
 
         activity.Activity.__init__(self, handle)
 
         activity.Activity.__init__(self, handle)
 
        canvas = hippo.Canvas()
 
        self.add(canvas)
 
        canvas.show()
 
 
   
 
   
 
         vbox = hippo.CanvasBox()
 
         vbox = hippo.CanvasBox()
         canvas.set_root(vbox)
+
         self.set_root(vbox)
 
   
 
   
 
         toolbar = Toolbar()
 
         toolbar = Toolbar()
Line 70: Line 68:
 
  from sugar.activity import activity
 
  from sugar.activity import activity
 
  bundle_path = activity.get_bundle_path()
 
  bundle_path = activity.get_bundle_path()
 
= Profiling =
 
 
:''See [[Sugar Performance]] for a profiling snippet''
 
  
 
[[Category:HowTo]]
 
[[Category:HowTo]]
 
[[Category:Sugar]]
 
[[Category:Sugar]]

Revision as of 20:30, 6 March 2007

Template:OLPC

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 gettext import gettext as _
from sugar.activity import activity
from sugar.graphics.toolbar import Toolbar
from sugar.graphics.iconbutton import IconButton
from sugar.graphics.entry import Entry
from sugar.graphics.optionmenu import OptionMenu
from sugar.graphics.menu import MenuItem

class FooActivity(activity.Activity):

    _ACTION_ANYTHING = 1
    _ACTION_APPLES = 2
    _ACTION_ORANGES = 3

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

        vbox = hippo.CanvasBox()
        self.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)

        entry = Entry()
        button.connect("activated", self._entry_activated_cb)
        toolbar.append(entry)

        option_menu = OptionMenu()
        option_menu.add_item(MenuItem(self._ACTION_ANYTHING, _('Anything')))
        option_menu.add_item(MenuItem(self._ACTION_APPLES, _('Apples'),
                                      'theme:stock-close'))
        option_menu.add_item(MenuItem(self._ACTION_ORANGES, _('Oranges')))
        option_menu.add_separator()
        toolbar.append(option_menu)

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

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

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

    def _entry_activated_cb(self, entry):
        logging.debug('FooActivity._entry_activated_cb')

Files

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

from sugar.activity import activity
bundle_path = activity.get_bundle_path()