Here is a recipe to embed webkit in a wxPython app. It works with wxPython 2.6 and wxPython 2.8 - wxGTK port only

See code comments - and of couse the code itself :) - for more details

To try, copy and paste to a file named webkit_gtk.py

   1 import wx
   2 
   3 import gobject
   4 gobject.threads_init()
   5 
   6 import pygtk
   7 pygtk.require('2.0')
   8 import gtk, gtk.gdk
   9 
  10 # pywebkitgtk (http://code.google.com/p/pywebkitgtk/)
  11 import webkit
  12 
  13 '''
  14 As far as I know (I may be wrong), a wx.Panel is "composed" by a GtkPizza
  15 as a child of GtkScrolledWindow. GtkPizza is a custom widget created for
  16 wxGTK.
  17 
  18 WebKitGTK+ - the webkit port for GTK+ that has python bindings - wants a
  19 GtkScrolledWindow as parent.
  20 
  21 So all we need to embed webkit in wxGTK is find the wx.Panel's
  22 GtkScrolledWindow.
  23 This is acomplished using pygtk that is present in major distro's by
  24 default, at least those that use gnome as its main desktop environment.
  25 
  26 A last note is that for get a handle of a window in X, the window must be
  27 "realized" first, in other words, must already exists. So we must Show
  28 the wx.Frame before use this WKHtmlWindow class.
  29 
  30 '''
  31 class WKHtmlWindow(wx.Panel):
  32     def __init__(self, *args, **kwargs):
  33         wx.Panel.__init__(self, *args, **kwargs)
  34 
  35         # Here is where we do the "magic" to embed webkit into wxGTK.
  36         whdl = self.GetHandle()
  37         window = gtk.gdk.window_lookup(whdl)
  38 
  39         # We must keep a reference of "pizza". Otherwise we get a crash.
  40         self.pizza = pizza = window.get_user_data()
  41 
  42         self.scrolled_window = scrolled_window = pizza.parent
  43 
  44         # Removing pizza to put a webview in it's place
  45         scrolled_window.remove(pizza)
  46 
  47         self.ctrl = ctrl = webkit.WebView()
  48         scrolled_window.add(ctrl)
  49 
  50         scrolled_window.show_all()
  51 
  52     # Some basic usefull methods
  53     def SetEditable(self, editable=True):
  54         self.ctrl.set_editable(editable)
  55 
  56     def LoadUrl(self, url):
  57         self.ctrl.load_uri(url)
  58 
  59     def HistoryBack(self):
  60         self.ctrl.go_back()
  61 
  62     def HistoryForward(self):
  63         self.ctrl.go_forward()
  64 
  65     def StopLoading(self):
  66         self.ctrl.stop_loading()

And the demo... copy and paste into a file named, for example, demo.py next to webkit_gtk.py

   1 import wxversion
   2 #wxversion.select("2.6")
   3 wxversion.select("2.8")
   4 
   5 import wx
   6 
   7 from webkit_gtk import WKHtmlWindow as HtmlWindow
   8 
   9 class TestWKPanel(wx.Panel):
  10     def __init__(self, parent):
  11         wx.Panel.__init__(self, parent)
  12 
  13         self.html = HtmlWindow(self)
  14         self.html.SetEditable(True)
  15 
  16         self.box = wx.BoxSizer(wx.VERTICAL)
  17         self.box.Add(self.html, 1, wx.EXPAND)
  18 
  19         subbox = wx.BoxSizer(wx.HORIZONTAL)
  20 
  21         btn = wx.Button(self, -1, "Load File")
  22         self.Bind(wx.EVT_BUTTON, self.OnLoadFile, btn)
  23         subbox.Add(btn, 1, wx.EXPAND | wx.ALL, 2)
  24 
  25         btn = wx.Button(self, -1, "Load URL")
  26         self.Bind(wx.EVT_BUTTON, self.OnLoadURL, btn)
  27         subbox.Add(btn, 1, wx.EXPAND | wx.ALL, 2)
  28 
  29         btn = wx.Button(self, -1, "Back")
  30         self.Bind(wx.EVT_BUTTON, self.OnBack, btn)
  31         subbox.Add(btn, 1, wx.EXPAND | wx.ALL, 2)
  32 
  33         btn = wx.Button(self, -1, "Forward")
  34         self.Bind(wx.EVT_BUTTON, self.OnForward, btn)
  35         subbox.Add(btn, 1, wx.EXPAND | wx.ALL, 2)
  36 
  37         self.box.Add(subbox, 0, wx.EXPAND)
  38         self.SetSizer(self.box)
  39         self.Layout()
  40 
  41     def OnLoadFile(self, event):
  42         dlg = wx.FileDialog(self, wildcard = '*.htm*', style=wx.OPEN)
  43 
  44         if dlg.ShowModal() == wx.ID_OK:
  45             path = dlg.GetPath()
  46             self.html.LoadUrl("file://%s" % path)
  47 
  48         dlg.Destroy()
  49 
  50     def OnLoadURL(self, event):
  51         dlg = wx.TextEntryDialog(self, "Enter a URL", defaultValue="http://")
  52 
  53         if dlg.ShowModal() == wx.ID_OK:
  54             url = dlg.GetValue()
  55             if not url.startswith('http://'):
  56                 url = "http://%s" % url
  57             self.html.LoadUrl(url)
  58 
  59         dlg.Destroy()
  60 
  61     def OnBack(self, event):
  62         self.html.HistoryBack()
  63 
  64     def OnForward(self, event):
  65         self.html.HistoryForward()
  66 
  67 
  68 class Frame(wx.Frame):
  69     def __init__(self):
  70         wx.Frame.__init__(self, None)
  71         self.Show()
  72 
  73         TestWKPanel(self)
  74 
  75         # This is need it for wxPython2.8,
  76         # for 2.6 doesnt hurt
  77         self.SendSizeEvent()
  78 
  79 
  80 if __name__ == '__main__':
  81     app = wx.App()
  82     f = Frame()
  83     app.MainLoop()

wxGTKWebKit (last edited 2010-08-05 22:17:36 by 93)

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.