Development Team/Almanac/Code Snippets: Difference between revisions

Wade (talk | contribs)
Manuq (talk | contribs)
 
(18 intermediate revisions by 14 users not shown)
Line 1: Line 1:
{{OLPC}}
{{Translations}}
{{Translations}}
The [[Development_Team/Almanac|Sugar Almanac]] also has lots of code snippets.


== Smooth animation ==
== Smooth animation ==


[PyGTK/Smooth_Animation_with_PyGTK]
[[PyGTK/Smooth_Animation_with_PyGTK]]
 
== WebKit WebView ==


== WebView ==
from gi.repository import WebKit
Use the WebView in a widget.
from gi.repository import Gtk
  import os
   
  import hulahop
  WINDOW_WIDTH, WINDOW_HEIGHT = 700, 500
  from sugar import env
   
  hulahop.startup(os.path.join(env.get_profile_path(), 'gecko'))
  # create window:
   
   
  from hulahop.webview import WebView
  def _destroy_cb(widget, data=None):
    Gtk.main_quit()
   
   
  import gtk
  window = Gtk.Window()
window.resize(WINDOW_WIDTH, WINDOW_HEIGHT)
window.connect("destroy", _destroy_cb)
window.show()
   
   
  win = gtk.Window(gtk.WINDOW_TOPLEVEL)
  # create and add scrolled window:
  win.set_size_request(800,600)
  s = Gtk.ScrolledWindow()
  win.connect('destroy', gtk.main_quit)
  window.add(s)
wv = WebView()
  s.show()
wv.load_uri('http://wiki.laptop.org/go/Guido_van_Robot')
  wv.show()
   
   
  win.add(wv)
  # create and add webview:
v = WebKit.WebView()
s.add(v)
v.show()
   
   
  win.show()
  # load google page:
  gtk.main()
  v.load_uri('http://wiki.laptop.org')
   
   
=== Notes ===
Gtk.main()
* I'm not sure what the purpose is of the call to hulahop.startup as the documentation is rather sparse. I only knows it's needed to get it to work [[User:Stas_z|Stas Zytkiewicz]] 2 Dec 2007


== Toolbar ==
== Toolbar ==
Line 51: Line 58:
  class MyActivity(activity.Activity):
  class MyActivity(activity.Activity):
  ...
  ...
     mytoolbox = gtk.Toolbar(self)
     mytoolbox = gtk.Toolbar()
     helpbut = ToolButton('help')#Stock help icon
     helpbut = ToolButton('help') #Stock help icon
     helpbut.set_tooltip(_("Get help"))
     helpbut.set_tooltip(_("Get help"))
     helpbut.connect('clicked', self.help_button_pressed)
     helpbut.connect('clicked', self.help_button_pressed)
Line 75: 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 215: Line 229:
[[Category:HowTo]]
[[Category:HowTo]]
[[Category:Sugar]]
[[Category:Sugar]]
[[Category:Developer]]