个人工具

“UbuntuHelp:PythonRecipes/WebBrowser”的版本间的差异

来自Ubuntu中文

跳转至: 导航, 搜索
 
(未显示同一用户的5个中间版本)
第1行: 第1行:
 
{{From|https://help.ubuntu.com/community/PythonRecipes/WebBrowser}}
 
{{From|https://help.ubuntu.com/community/PythonRecipes/WebBrowser}}
 
{{Languages|UbuntuHelp:PythonRecipes/WebBrowser}}
 
{{Languages|UbuntuHelp:PythonRecipes/WebBrowser}}
 +
<<Include(Tag/StyleCleanup)>>
 
#title Simple GTK Web Browser
 
#title Simple GTK Web Browser
 +
Parent: [[UbuntuHelp:ProgrammingPython]] [[UbuntuHelp:PythonRecipes/WebBrowser/PageDiscussion|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.
 
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.
 
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.
第12行: 第14行:
 
<pre><nowiki>#!python
 
<pre><nowiki>#!python
 
#!/usr/bin/env python
 
#!/usr/bin/env python
 +
 
import gtk
 
import gtk
 
import gtkmozembed
 
import gtkmozembed
 +
 
def CloseWindow(caller_widget):
 
def CloseWindow(caller_widget):
"""Close the window and exit the app"""
+
    """Close the window and exit the app"""
gtk.main_quit() # Close the app fully
+
    gtk.main_quit() # Close the app fully
 +
 
 
# Set-up the main window which we're going to embed the widget into
 
# 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 = gtk.Window() # Create a new GTK window called 'win'
 +
 
win.set_title("Simple Web Browser") # Set the title of the window
 
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_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.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
 
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
 
# Create the browser widget
 
gtkmozembed.set_profile_path("/tmp", "simple_browser_user") # Set a temporary Mozilla profile (works around some bug)
 
gtkmozembed.set_profile_path("/tmp", "simple_browser_user") # Set a temporary Mozilla profile (works around some bug)
 
mozbrowser = gtkmozembed.MozEmbed() # Create the browser widget
 
mozbrowser = gtkmozembed.MozEmbed() # Create the browser widget
 +
 
# Set-up the browser widget before we display it
 
# Set-up the browser widget before we display it
 
win.add(mozbrowser) # Add the 'mozbrowser' widget to the main window 'win'
 
win.add(mozbrowser) # Add the 'mozbrowser' widget to the main window 'win'
第31行: 第40行:
 
mozbrowser.set_size_request(600,400) # Attempt to set the size of the browser widget to 600x400 pixels
 
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)
 
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
 
win.show() # Show the window
 +
 
gtk.main() # Enter the 'GTK mainloop', so that the GTK app starts to run
 
gtk.main() # Enter the 'GTK mainloop', so that the GTK app starts to run
 +
 
</nowiki></pre>
 
</nowiki></pre>
 
=== Bugs ===
 
=== Bugs ===
 
* There is a [https://bugs.launchpad.net/ubuntu/+source/firefox/+bug/26436 bug] which can cause your application to segfault/crash if it uses a GTKMoz''''''Embed. The error message reads <code><nowiki>Segmentation fault (core dumped)</nowiki></code>. A workaround is to use the following command to execute your Python application:
 
* There is a [https://bugs.launchpad.net/ubuntu/+source/firefox/+bug/26436 bug] which can cause your application to segfault/crash if it uses a GTKMoz''''''Embed. The error message reads <code><nowiki>Segmentation fault (core dumped)</nowiki></code>. A workaround is to use the following command to execute your Python application:
<pre><nowiki>export LD_LIBRARY_PATH=/usr/lib/firefox && export MOZILLA_FIVE_HOME=/usr/lib/firefox &&  python /path/to/your/python/application.py $@
+
<pre><nowiki>
 +
export LD_LIBRARY_PATH=/usr/lib/firefox && export MOZILLA_FIVE_HOME=/usr/lib/firefox &&  python /path/to/your/python/application.py $@
 
</nowiki></pre>
 
</nowiki></pre>
 
* If you get warnings related to <code><nowiki>gecko</nowiki></code>, for example <code><nowiki>** (gecko:7021): WARNING **: No listener with the specified listener id 27</nowiki></code>, these are normally generated by the GTKMoz''''''Embed and not your app.
 
* If you get warnings related to <code><nowiki>gecko</nowiki></code>, for example <code><nowiki>** (gecko:7021): WARNING **: No listener with the specified listener id 27</nowiki></code>, these are normally generated by the GTKMoz''''''Embed and not your app.
第42行: 第55行:
 
* [http://www.pygtk.org/pygtkmozembed/index.html Python GtkMozembed Reference Manual]
 
* [http://www.pygtk.org/pygtkmozembed/index.html Python GtkMozembed Reference Manual]
 
* [http://patrick.wagstrom.net/tutorials/pygtkmozembed/pygtkmozembed.html Creating a GNOME Web Browser with Python]
 
* [http://patrick.wagstrom.net/tutorials/pygtkmozembed/pygtkmozembed.html Creating a GNOME Web Browser with Python]
 +
----
 +
Categories: [[category:CategoryGtk]] [[category:CategoryProgrammingPython]]
  
 
[[category:UbuntuHelp]]
 
[[category:UbuntuHelp]]

2009年5月14日 (四) 17:22的最新版本

<<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 $@
  • 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: