Line 1: |
Line 1: |
− | {{OLPC}}
| |
| {{Translations}} | | {{Translations}} |
| + | |
| + | The [[Development_Team/Almanac|Sugar Almanac]] also has lots of code snippets. |
| + | |
| + | == Smooth animation == |
| + | |
| + | [[PyGTK/Smooth_Animation_with_PyGTK]] |
| + | |
| + | == WebKit WebView == |
| + | |
| + | from gi.repository import WebKit |
| + | from gi.repository import Gtk |
| + | |
| + | WINDOW_WIDTH, WINDOW_HEIGHT = 700, 500 |
| + | |
| + | # create window: |
| + | |
| + | def _destroy_cb(widget, data=None): |
| + | Gtk.main_quit() |
| + | |
| + | window = Gtk.Window() |
| + | window.resize(WINDOW_WIDTH, WINDOW_HEIGHT) |
| + | window.connect("destroy", _destroy_cb) |
| + | window.show() |
| + | |
| + | # create and add scrolled window: |
| + | s = Gtk.ScrolledWindow() |
| + | window.add(s) |
| + | s.show() |
| + | |
| + | # create and add webview: |
| + | v = WebKit.WebView() |
| + | s.add(v) |
| + | v.show() |
| + | |
| + | # load google page: |
| + | v.load_uri('http://wiki.laptop.org') |
| + | |
| + | Gtk.main() |
| + | |
| == Toolbar == | | == Toolbar == |
| | | |
| === Toolbar icons === | | === Toolbar icons === |
| | | |
− | button = sugar.graphics.toolbutton.ToolButton("some-icon-name") | + | Setting a standard Sugar toolbar. |
− | button.show() | + | from sugar.activity import activity |
− | toolbar.insert(button,-1)
| + | ... |
| + | class MyActivity(activity.Activity): |
| + | ... |
| + | toolbox = activity.ActivityToolbox(self) |
| + | self.set_toolbox(toolbox) |
| + | toolbox.show() |
| | | |
− | In addition to the standard icons in /usr/share/icons/sugar/scalable/ (eg, "go-next"), you can create additional icons by putting an svg in your activity's icons/ directory. Eg, | + | Adding a custom toolbar and icons. |
| + | from sugar.graphics.toolbutton import ToolButton |
| + | from sugar.activity import activity |
| + | ... |
| + | class MyActivity(activity.Activity): |
| + | ... |
| + | mytoolbox = gtk.Toolbar() |
| + | helpbut = ToolButton('help') #Stock help icon |
| + | helpbut.set_tooltip(_("Get help")) |
| + | helpbut.connect('clicked', self.help_button_pressed) |
| + | mytoolbox.insert(helpbut, -1) |
| + | helpbut.show() |
| + | mytoolbox.show() |
| + | |
| + | toolbox = activity.ActivityToolbox(self) |
| + | toolbox.add_toolbar("mytoolbar",mytoolbox) |
| + | self.set_toolbox(toolbox) |
| + | toolbox.show() |
| + | |
| + | In addition to the standard icons in /usr/share/icons/sugar/scalable/ (eg, "go-next"), you can create additional icons by putting an svg in your activity's icons/ directory. E.g., |
| icons/my-icon.svg | | icons/my-icon.svg |
− | | + | And then use it. |
− | === Other notes === | + | mybut = ToolButton('my-icon')# without extension |
− | *Do ''not'' use sugar.graphics.toolbar and its Toolbar. It is old broken left-over code, now deleted. [[User:MitchellNCharity|MitchellNCharity]] 17:06, 21 June 2007 (EDT)
| |
| | | |
| == Files == | | == Files == |
Line 21: |
Line 82: |
| from sugar.activity import activity | | from sugar.activity import activity |
| bundle_path = activity.get_bundle_path() | | bundle_path = activity.get_bundle_path() |
| + | |
| + | This snippet shows how to get [http://lists.laptop.org/pipermail/devel/2008-March/011799.html a path to an activity's writable directories] (i.e. the SUGAR_ACTIVITY_ROOT environment variable; see also [[Activity_DBus_API#Security | the activity DBus API]]): |
| + | |
| + | class WebActivity(activity.Activity): |
| + | def __init__(self, handle): |
| + | activity.Activity.__init__(self, handle) |
| + | temp_path = os.path.join(self.get_activity_root(), 'instance') |
| | | |
| == Images == | | == Images == |
Line 81: |
Line 149: |
| hashed_key = util.printable_hash(key_hash) | | hashed_key = util.printable_hash(key_hash) |
| | | |
− | * Avoid memory exhaustion from large dead pgtk objects by using | + | * Avoid memory exhaustion from large dead pygtk objects by using |
| | | |
| import gc | | import gc |
Line 161: |
Line 229: |
| [[Category:HowTo]] | | [[Category:HowTo]] |
| [[Category:Sugar]] | | [[Category:Sugar]] |
| + | [[Category:Developer]] |