个人工具

UbuntuHelp:PythonRecipes/WebBrowser

来自Ubuntu中文

跳转至: 导航, 搜索

<<Include(Tag/StyleCleanup)>>

  1. title Simple GTK Web Browser

Parent: UbuntuHelp:ProgrammingPython Discuss this page You can add a built-in web browser to your GTK program using the GTKMoz'Embed widget. GTKMoz'Embed is just like a cut-down version of Mozilla Firefox, and so pages displayed in a GTKMoz'Embed 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): WebBrowser?action=AttachFile&do=get&target=simple-web-browser.png

Example Code

Example Python code for creating a simple web browser:

#!python
#!/usr/bin/env python

import gtk
import gtkmozembed

def CloseWindow(caller_widget):
    """Close the window and exit the app"""
    gtk.main_quit() # Close the app fully

# Set-up the main window which we're going to embed the widget into
win = gtk.Window() # Create a new GTK window called 'win'

win.set_title("Simple Web Browser") # Set the title of the window
win.set_icon_from_file('/usr/share/icons/Tango/22x22/apps/web-browser.png') # Set the window icon to a web browser icon
win.set_position(gtk.WIN_POS_CENTER) # Position the window in the centre of the screen

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

# Create the browser widget
gtkmozembed.set_profile_path("/tmp", "simple_browser_user") # Set a temporary Mozilla profile (works around some bug)
mozbrowser = gtkmozembed.MozEmbed() # Create the browser widget

# Set-up the browser widget before we display it
win.add(mozbrowser) # Add the 'mozbrowser' widget to the main window 'win'
mozbrowser.load_url("http://www.ubuntu.com") # Load a web page
mozbrowser.set_size_request(600,400) # Attempt to set the size of the browser widget to 600x400 pixels
mozbrowser.show() # Try to show the browser widget before we show the window, so that the window appears at the correct size (600x400)

win.show() # Show the window

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 GTKMoz'Embed. 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 [email protected]
  • 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 GTKMoz'Embed and not your app.

Further Reading


Categories: