特殊:Badtitle/NS100:PythonRecipes/WebBrowser:修订间差异

来自Ubuntu中文
跳到导航跳到搜索
Oneleaf留言 | 贡献
新页面: {{From|https://help.ubuntu.com/community/PythonRecipes/WebBrowser}} {{Languages|UbuntuHelp:PythonRecipes/WebBrowser}} #title Simple GTK Web Browser You can add a built-in web browser to ...
 
Wikibot留言 | 贡献
无编辑摘要
第2行: 第2行:
{{Languages|UbuntuHelp:PythonRecipes/WebBrowser}}
{{Languages|UbuntuHelp:PythonRecipes/WebBrowser}}
#title Simple GTK Web Browser
#title Simple GTK Web Browser
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.
This example shows you how to create a very basic web browser. You may also be interested in a similar tutorial, [http://patrick.wagstrom.net/tutorials/pygtkmozembed/pygtkmozembed.html Creating a GNOME Web Browser with Python].
This example shows you how to create a very basic web browser. You may also be interested in a similar tutorial, [http://patrick.wagstrom.net/tutorials/pygtkmozembed/pygtkmozembed.html Creating a GNOME Web Browser with Python].
=== Screenshot ===
=== Screenshot ===
Screenshot of the example code running under Ubuntu 7.10 (Gutsy Gibbon):
Screenshot of the example code running under Ubuntu 7.10 (Gutsy Gibbon):
https://help.ubuntu.com/community/PythonRecipes/WebBrowser?action=AttachFile&do=get&target=simple-web-browser.png
https://help.ubuntu.com/community/PythonRecipes/WebBrowser?action=AttachFile&do=get&target=simple-web-browser.png
=== Example Code ===
=== Example Code ===
Example Python code for creating a simple web browser:
Example Python code for creating a simple web browser:
<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'
第47行: 第31行:
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.
=== Further Reading ===
=== Further Reading ===
* [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]


[[category:UbuntuHelp]]
[[category:UbuntuHelp]]

2007年11月30日 (五) 21:11的版本

{{#ifexist: :PythonRecipes/WebBrowser/zh | | {{#ifexist: PythonRecipes/WebBrowser/zh | | {{#ifeq: {{#titleparts:PythonRecipes/WebBrowser|1|-1|}} | zh | | }} }} }} {{#ifeq: {{#titleparts:PythonRecipes/WebBrowser|1|-1|}} | zh | | }}

  1. title Simple GTK Web Browser

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