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