webkit with wxPython (wxGTK only)

39 views
Skip to first unread message

Ricardo Pedroso

unread,
Jun 10, 2010, 8:31:30 PM6/10/10
to wxpytho...@googlegroups.com
Today I wake up thinking if pygtk as a webkit control, why can I use it in
wxPython? And Today I had the time and want a little fun

So I start trying and found a way to use webkit inside wxPython.
I haven't done many tests and only run it in my system (Ubuntu 10.04)

I'm not sure if it's robust enough to be used in prodution code,
but at least it's funny. You can even edit on it, doing .SetEditable(True)

There's some problems with pages with flash only - content draw problem.
The same happend with pygtk - so not a wxPython problem.

To run the demo attached you need:
pywebkitgtk
pygtk

I tested with:
pywebkitgtk 1.1.7
pygtk 2.17
wxPython 2.6 and 2.8

All from Ubuntu 10.04 repository

Regards,
Ricardo

webkit_gtk.py

Ricardo Pedroso

unread,
Jun 10, 2010, 9:18:05 PM6/10/10
to wxpytho...@googlegroups.com
On Fri, Jun 11, 2010 at 1:31 AM, Ricardo Pedroso <rmdpe...@gmail.com> wrote:
>
> There's some problems with pages with flash only - content draw problem.
> The same happend with pygtk - so not a wxPython problem.
>

I found that flash was freezing the app.
I attached the all demo again, correcting this issue, I hope

Ricardo

webkit_gtk.py

Robin Dunn

unread,
Jun 11, 2010, 3:15:02 PM6/11/10
to wxpytho...@googlegroups.com

This is a good example. It would be nice if it was added to the wiki
along with some explainations so others can easily find it there if they
need to do something similar.

--
Robin Dunn
Software Craftsman
http://wxPython.org

Ricardo Pedroso

unread,
Aug 2, 2010, 6:35:41 PM8/2/10
to wxpytho...@googlegroups.com

Finally I added to the wiki (http://wiki.wxpython.org/wxGTKWebKit)
It's a simpler "version" of what I've posted here, just to be cleaner and clear.

Is the explanation good enough?
What could be done to improve it?
Is the page title wxGTKWebKit OK?
Should have a link to it somewhere?

Ricardo

Mike Driscoll

unread,
Aug 3, 2010, 9:47:31 AM8/3/10
to wxPython-users


On Aug 2, 5:35 pm, Ricardo Pedroso <rmdpedr...@gmail.com> wrote:
> On Fri, Jun 11, 2010 at 8:15 PM, Robin Dunn <ro...@alldunn.com> wrote:
> > On 6/10/10 6:18 PM, Ricardo Pedroso wrote:
>
> >> On Fri, Jun 11, 2010 at 1:31 AM, Ricardo Pedroso<rmdpedr...@gmail.com>
> >>  wrote:
>
> >>> There's some problems with pages with flash only - content draw problem.
> >>> The same happend with pygtk - so not a wxPython problem.
>
> >> I found that flash was freezing the app.
> >> I attached the all demo again, correcting this issue, I hope
>
> > This is a good example.  It would be nice if it was added to the wiki along
> > with some explainations so others can easily find it there if they need to
> > do something similar.
>
> Finally I added to the wiki (http://wiki.wxpython.org/wxGTKWebKit)
> It's a simpler "version" of what I've posted here, just to be cleaner and clear.
>
> Is the explanation good enough?
> What could be done to improve it?
> Is the page title wxGTKWebKit OK?


I think it's fine.


> Should have a link to it somewhere?
>
> Ricardo


I linked to it from the Recipes page: http://wiki.wxpython.org/RecipesControls

Why is a GTK Pizza required? That seems kind of strange to me.

Hopefully Kevin, Robin and the rest will have good feedback as well.

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org

Ricardo Pedroso

unread,
Aug 3, 2010, 10:08:16 AM8/3/10
to wxpytho...@googlegroups.com
On Tue, Aug 3, 2010 at 2:47 PM, Mike Driscoll <kyos...@gmail.com> wrote:
>
>
> On Aug 2, 5:35 pm, Ricardo Pedroso <rmdpedr...@gmail.com> wrote:
>>
>> Finally I added to the wiki (http://wiki.wxpython.org/wxGTKWebKit)
>> It's a simpler "version" of what I've posted here, just to be cleaner and clear.
>>
>> Is the explanation good enough?
>> What could be done to improve it?
>> Is the page title wxGTKWebKit OK?
>
>
> I think it's fine.
>
>
>> Should have a link to it somewhere?
>>
>> Ricardo
>
>
> I linked to it from the Recipes page: http://wiki.wxpython.org/RecipesControls
>

Thanks

> Why is a GTK Pizza required? That seems kind of strange to me.
>

The best I can say, It's a wxWidget internal control to "bridge" the
wxWidgets and GTK
funcionality.

You can read a tecnical description in src/gtk/window.cpp, for eg:
http://svn.wxwidgets.org/viewvc/wx/wxWidgets/branches/WX_2_8_BRANCH/src/gtk/window.cpp?revision=61811&view=markup

around line 90

Ricardo

Robin Dunn

unread,
Aug 3, 2010, 1:48:23 PM8/3/10
to wxpytho...@googlegroups.com
On 8/3/10 6:47 AM, Mike Driscoll wrote:
>

> I linked to it from the Recipes page: http://wiki.wxpython.org/RecipesControls
>
> Why is a GTK Pizza required? That seems kind of strange to me.

The Pizza widget is where a lot of the wx personality for non-control
widgets (like panels) is implemented. I'm not sure why that name was
chosen, maybe just to help ensure it didn't conflict with something else.

pyGrant

unread,
Aug 5, 2010, 5:57:55 AM8/5/10
to wxPython-users
Thanks heaps for making this work Ricardo. Getting Webkit to work via
gtk is an ingenious workaround which could be a life-saver for a
project of mine. I was able to get your demo working on a near-clean
Lucid install which had wxPython installed. I didn't need to do
anything special.

One suggestion for the wiki. It wasn't immediately clear that the
first block of code should be saved as a module called webkit_gtk.py
which should be put in the same folder as a module with the other code
in it. Once I figured that out it was plain sailing. Once again,
thanks for sharing your solution.

Ricardo Pedroso

unread,
Aug 5, 2010, 6:30:26 PM8/5/10
to wxpytho...@googlegroups.com
On Thu, Aug 5, 2010 at 10:57 AM, pyGrant <gr...@p-s.co.nz> wrote:
> Thanks heaps for making this work Ricardo.  Getting Webkit to work via
> gtk is an ingenious workaround which could be a life-saver for a
> project of mine.  I was able to get your demo working on a near-clean
> Lucid install which had wxPython installed. I didn't need to do
> anything special.

Glad that may be usefull for you.
Today's distros (gnome based, at least) are python and pygtk depend.
I was hoping that was the case that a distro near-clean install was
enough but I was not sure since my system is far from be a clean
install at the moment, so I didn't mentioned.

> One suggestion for the wiki.  It wasn't immediately clear that the
> first block of code should be saved as a module called webkit_gtk.py
> which should be put in the same folder as a module with the other code
> in it.  Once I figured that out it was plain sailing.  Once again,
> thanks for sharing your solution.

You are right. Done.

Ricardo

pyGrant

unread,
Aug 8, 2010, 9:53:43 PM8/8/10
to wxPython-users
A couple of notes which might help others:

1) It seems you can only get the handle for the html panel once the
dialog/frame has been shown. Otherwise, you get a handle id of 0
which doesn't work. This can be a problem. In the case of a dialog,
you might want to define the dialog, including the gtk html widget,
before showing the dialog.

e.g. dlg = MyDialog()
dlg.ShowModal()

But, if you do that, your html widget won't work. Solution - use the
SHOW event to finish configuring the html widget:

In the dialog __init__
self.Bind(wx.EVT_SHOW, self.on_show)

then:

def on_show(self, event):
try:
self.html.pizza_magic() # must happen after Show
finally: # any initial content

pyGrant

unread,
Aug 8, 2010, 10:08:44 PM8/8/10
to wxPython-users
Ooops - I accidentally hit enter before I finished ;-)

self.html refers to the special html panel widget. The pizza_magic()
method contains most of the code that previously was in the html panel
widget's __init__ method. I shifted it across so that I could run
that code _after_ the widget was Shown. If it was run at the same
time as the __init__ it would not get a handle and would not succeed.

The try-finally can be ignored and only relates to my specific cross-
platform code. It should have been edited out of the example.

2) If you want to load html from a string you can use something like
this:

self.ctrl.load_string(strHTML, "text/html", "utf-8", base_path)

3) Ubuntu has python-webkit already installed but Kubuntu doesn't (I
checked Lucid). A good test is

>>> import webkit
>>>
It needs to be installed for this approach to work.


All the best, Grant

Robin Dunn

unread,
Aug 9, 2010, 1:33:34 PM8/9/10
to wxpytho...@googlegroups.com
On 8/8/10 6:53 PM, pyGrant wrote:
> A couple of notes which might help others:
>
> 1) It seems you can only get the handle for the html panel once the
> dialog/frame has been shown. Otherwise, you get a handle id of 0
> which doesn't work. This can be a problem. In the case of a dialog,
> you might want to define the dialog, including the gtk html widget,
> before showing the dialog.
>
> e.g. dlg = MyDialog()
> dlg.ShowModal()
>
> But, if you do that, your html widget won't work. Solution - use the
> SHOW event to finish configuring the html widget:

You should also be able to use the EVT_WINDOW_CREATE event to do it. On
wxGTK that event is sent when the window is "realized" (X-Windows
terminology which means that it has been mapped to the display server.)
You can see an example of using EVT_WINDOW_CREATE in the ShapedWindow
sample in the demo.

pyGrant

unread,
Aug 11, 2010, 6:25:24 PM8/11/10
to wxPython-users
Hi Robin,

Thanks for that. It seems like I may need both according to platform.
Using EVT_SHOW on the frame/dialog generally works cross-platform and
the html widget displays its content. But on Mac OS X it doesn't. It
only works if I use EVT_CREATE_WINDOW on the html widget. The latter
binding also works in Ubuntu (which works with both EVT_SHOW and
EVT_CREATE_WINDOW) but not in Windows XP. At the moment I am thinking
of doing one binding (EVT_CREATE_WINDOW on the html widget) if the
platform is Mac and the other (EVT_SHOW on the frame/dialog) if it
isn't.


All the best, Grant
Reply all
Reply to author
Forward
0 new messages