You can add a built-in web browser to your GTK program using the GTKMozEmbed widget. GTKMozEmbed is just like a cut-down version of Mozilla Firefox, and so pages displayed in a GTKMozEmbed widget should look identical to those displayed in Firefox.

Embedding a web browser is useful for such things as displaying documents, as well as for allowing users to browse the web. For example, Yelp (the GNOME help browser) uses an embedded browser to display help pages.

This example shows you how to create a very basic web browser. You may also be interested in a similar tutorial, Creating a GNOME Web Browser with Python.

Screenshot

Screenshot of the example code running under Ubuntu 7.10 (Gutsy Gibbon):

simple-web-browser.png

Example Code

Example Python code for creating a simple web browser:

   1 #!/usr/bin/env python
   2 
   3 import gtk
   4 import gtkmozembed
   5 
   6 def CloseWindow(caller_widget):
   7     """Close the window and exit the app"""
   8     gtk.main_quit() # Close the app fully
   9 
  10 # Set-up the main window which we're going to embed the widget into
  11 win = gtk.Window() # Create a new GTK window called 'win'
  12 
  13 win.set_title("Simple Web Browser") # Set the title of the window
  14 win.set_icon_from_file('/usr/share/icons/Tango/22x22/apps/web-browser.png') # Set the window icon to a web browser icon
  15 win.set_position(gtk.WIN_POS_CENTER) # Position the window in the centre of the screen
  16 
  17 win.connect("destroy", CloseWindow) # Connect the 'destroy' event to the 'CloseWindow' function, so that the app will quit properly when we press the close button
  18 
  19 # Create the browser widget
  20 gtkmozembed.set_profile_path("/tmp", "simple_browser_user") # Set a temporary Mozilla profile (works around some bug)
  21 mozbrowser = gtkmozembed.MozEmbed() # Create the browser widget
  22 
  23 # Set-up the browser widget before we display it
  24 win.add(mozbrowser) # Add the 'mozbrowser' widget to the main window 'win'
  25 mozbrowser.load_url("http://www.ubuntu.com") # Load a web page
  26 mozbrowser.set_size_request(600,400) # Attempt to set the size of the browser widget to 600x400 pixels
  27 mozbrowser.show() # Try to show the browser widget before we show the window, so that the window appears at the correct size (600x400)
  28 
  29 win.show() # Show the window
  30 
  31 gtk.main() # Enter the 'GTK mainloop', so that the GTK app starts to run

Bugs

  • There is a bug which can cause your application to segfault/crash if it uses a GTKMozEmbed. The error message reads Segmentation fault (core dumped). A workaround is to use the following command to execute your Python application:

    export LD_LIBRARY_PATH=/usr/lib/firefox && export MOZILLA_FIVE_HOME=/usr/lib/firefox &&  python /path/to/your/python/application.py $@
  • If you get warnings related to gecko, for example ** (gecko:7021): WARNING **: No listener with the specified listener id 27, these are normally generated by the GTKMozEmbed and not your app.

Further Reading


Categories: CategoryPythonRecipes Parent: ProgrammingPython Discuss this page

PythonRecipes/WebBrowser (last edited 2011-04-09 03:57:25 by k.dejong)