How to Embed Web Browser in Python GTK application using pymozembed

Embedding web browser or a screen for parsing HTML is easy in PyGTK. You only need to import the pymozembed, and add a few lines of code in your pygtk library, and you are set to go.

Here’s a sample PyGTK application that embeds a web-browser as well as a “Back” button for demonstration purpose :

PyGTK + browser Screenshot
PyMozEmbed PyGTK+ Mypapit Demo

[python]
#!/usr/bin/python
#
# demo by mypapit (info@mypapit.net) – Sept 2009
# http://blog.mypapit.net/
#
import pygtk
pygtk.require(‘2.0’)
import gtk
import gtkmozembed

class PyMoz:

def delete_event(self,widget,data=None):
print(“Exit”)
return False

def destroy(self,widget,data=None):
gtk.main_quit()

def button_clicked(self,widget,data):
data.go_back()

def __init__(self):
#init mozembed
self.moz = gtkmozembed.MozEmbed()
#create a Vertical Box Container to whole the browser
#and the “Back Button”
box = gtk.VBox(False,0)

#create a basic GTK+ window
win = gtk.Window()
win.add(box)

#create and connect “Back” button, to add functionality
self.button = gtk.Button(“Back”)
self.button.connect(“clicked”,self.button_clicked,self.moz)

#include both back button and the browser in the vertical box
#and the GTK+ window
box.pack_start(self.button,False,False,0)
box.pack_start(self.moz,True,True,0)

#load the URL
self.moz.load_url(‘http://blog.mypapit.net/’)

#set the window title
title=self.moz.get_title()
win.set_title(“browser demo”)

#show all the stuffs on screen
win.show_all()

#connect the delete_event and destroy event to make sure
#the app quits when the window is closed
win.connect(“delete_event”,self.delete_event)
win.connect(“destroy”,self.destroy)

if __name__ == “__main__”:
PyMoz()
gtk.main()

[/python]

Download demo source code : pymoz.py

Working hard publishing papers and learning PyGTK

For the last few weeks I’ve been working hard to finish up my research project, most of it concerning about on mobile phone application usability and context-aware applications. This leaves me with less  time to devote for this blog and I started to realise that I might have a ‘burn-out’ issue at the same time.

I’m still using Ubuntu in my daily work if you are curious (Ubuntu Netbook Remix 9.04 for netbook) and Ubuntu Jaunty Jackalope for my Intel Core 2 Duo Desktop PC. Besides writing those research papers, I’ve use what left of my time to learn PyGTK to enable me to code RAD app for GNU/Linux desktop environment.

I found out the references around the internet regarding PyGTK is very helpful to aid me in understanding the GTK+ bindings in python, which is not much different from its C counterpart.

Now that I’ve some time to spare until Hari Raya, I’ll try to use that time wisely to fill up this blog with a couple more of fresh new posts. So, keep reading!