特殊:Badtitle/NS100:PythonRecipes/WebBrowser:修订间差异
小 新页面: {{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 ... |
小无编辑摘要 |
||
第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的版本
文章出处: |
{{#if: | {{{2}}} | https://help.ubuntu.com/community/PythonRecipes/WebBrowser }} |
点击翻译: |
English {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/WebBrowser | UbuntuHelp:PythonRecipes/WebBrowser | {{#if: | :}}PythonRecipes/WebBrowser}}/af | • {{#if: UbuntuHelp:PythonRecipes/WebBrowser|Afrikaans| [[::PythonRecipes/WebBrowser/af|Afrikaans]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/WebBrowser | UbuntuHelp:PythonRecipes/WebBrowser | {{#if: | :}}PythonRecipes/WebBrowser}}/ar | • {{#if: UbuntuHelp:PythonRecipes/WebBrowser|العربية| [[::PythonRecipes/WebBrowser/ar|العربية]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/WebBrowser | UbuntuHelp:PythonRecipes/WebBrowser | {{#if: | :}}PythonRecipes/WebBrowser}}/az | • {{#if: UbuntuHelp:PythonRecipes/WebBrowser|azərbaycanca| [[::PythonRecipes/WebBrowser/az|azərbaycanca]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/WebBrowser | UbuntuHelp:PythonRecipes/WebBrowser | {{#if: | :}}PythonRecipes/WebBrowser}}/bcc | • {{#if: UbuntuHelp:PythonRecipes/WebBrowser|جهلسری بلوچی| [[::PythonRecipes/WebBrowser/bcc|جهلسری بلوچی]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/WebBrowser | UbuntuHelp:PythonRecipes/WebBrowser | {{#if: | :}}PythonRecipes/WebBrowser}}/bg | • {{#if: UbuntuHelp:PythonRecipes/WebBrowser|български| [[::PythonRecipes/WebBrowser/bg|български]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/WebBrowser | UbuntuHelp:PythonRecipes/WebBrowser | {{#if: | :}}PythonRecipes/WebBrowser}}/br | • {{#if: UbuntuHelp:PythonRecipes/WebBrowser|brezhoneg| [[::PythonRecipes/WebBrowser/br|brezhoneg]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/WebBrowser | UbuntuHelp:PythonRecipes/WebBrowser | {{#if: | :}}PythonRecipes/WebBrowser}}/ca | • {{#if: UbuntuHelp:PythonRecipes/WebBrowser|català| [[::PythonRecipes/WebBrowser/ca|català]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/WebBrowser | UbuntuHelp:PythonRecipes/WebBrowser | {{#if: | :}}PythonRecipes/WebBrowser}}/cs | • {{#if: UbuntuHelp:PythonRecipes/WebBrowser|čeština| [[::PythonRecipes/WebBrowser/cs|čeština]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/WebBrowser | UbuntuHelp:PythonRecipes/WebBrowser | {{#if: | :}}PythonRecipes/WebBrowser}}/de | • {{#if: UbuntuHelp:PythonRecipes/WebBrowser|Deutsch| [[::PythonRecipes/WebBrowser/de|Deutsch]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/WebBrowser | UbuntuHelp:PythonRecipes/WebBrowser | {{#if: | :}}PythonRecipes/WebBrowser}}/el | • {{#if: UbuntuHelp:PythonRecipes/WebBrowser|Ελληνικά| [[::PythonRecipes/WebBrowser/el|Ελληνικά]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/WebBrowser | UbuntuHelp:PythonRecipes/WebBrowser | {{#if: | :}}PythonRecipes/WebBrowser}}/es | • {{#if: UbuntuHelp:PythonRecipes/WebBrowser|español| [[::PythonRecipes/WebBrowser/es|español]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/WebBrowser | UbuntuHelp:PythonRecipes/WebBrowser | {{#if: | :}}PythonRecipes/WebBrowser}}/fa | • {{#if: UbuntuHelp:PythonRecipes/WebBrowser|فارسی| [[::PythonRecipes/WebBrowser/fa|فارسی]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/WebBrowser | UbuntuHelp:PythonRecipes/WebBrowser | {{#if: | :}}PythonRecipes/WebBrowser}}/fi | • {{#if: UbuntuHelp:PythonRecipes/WebBrowser|suomi| [[::PythonRecipes/WebBrowser/fi|suomi]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/WebBrowser | UbuntuHelp:PythonRecipes/WebBrowser | {{#if: | :}}PythonRecipes/WebBrowser}}/fr | • {{#if: UbuntuHelp:PythonRecipes/WebBrowser|français| [[::PythonRecipes/WebBrowser/fr|français]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/WebBrowser | UbuntuHelp:PythonRecipes/WebBrowser | {{#if: | :}}PythonRecipes/WebBrowser}}/gu | • {{#if: UbuntuHelp:PythonRecipes/WebBrowser|ગુજરાતી| [[::PythonRecipes/WebBrowser/gu|ગુજરાતી]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/WebBrowser | UbuntuHelp:PythonRecipes/WebBrowser | {{#if: | :}}PythonRecipes/WebBrowser}}/he | • {{#if: UbuntuHelp:PythonRecipes/WebBrowser|עברית| [[::PythonRecipes/WebBrowser/he|עברית]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/WebBrowser | UbuntuHelp:PythonRecipes/WebBrowser | {{#if: | :}}PythonRecipes/WebBrowser}}/hu | • {{#if: UbuntuHelp:PythonRecipes/WebBrowser|magyar| [[::PythonRecipes/WebBrowser/hu|magyar]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/WebBrowser | UbuntuHelp:PythonRecipes/WebBrowser | {{#if: | :}}PythonRecipes/WebBrowser}}/id | • {{#if: UbuntuHelp:PythonRecipes/WebBrowser|Bahasa Indonesia| [[::PythonRecipes/WebBrowser/id|Bahasa Indonesia]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/WebBrowser | UbuntuHelp:PythonRecipes/WebBrowser | {{#if: | :}}PythonRecipes/WebBrowser}}/it | • {{#if: UbuntuHelp:PythonRecipes/WebBrowser|italiano| [[::PythonRecipes/WebBrowser/it|italiano]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/WebBrowser | UbuntuHelp:PythonRecipes/WebBrowser | {{#if: | :}}PythonRecipes/WebBrowser}}/ja | • {{#if: UbuntuHelp:PythonRecipes/WebBrowser|日本語| [[::PythonRecipes/WebBrowser/ja|日本語]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/WebBrowser | UbuntuHelp:PythonRecipes/WebBrowser | {{#if: | :}}PythonRecipes/WebBrowser}}/ko | • {{#if: UbuntuHelp:PythonRecipes/WebBrowser|한국어| [[::PythonRecipes/WebBrowser/ko|한국어]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/WebBrowser | UbuntuHelp:PythonRecipes/WebBrowser | {{#if: | :}}PythonRecipes/WebBrowser}}/ksh | • {{#if: UbuntuHelp:PythonRecipes/WebBrowser|Ripoarisch| [[::PythonRecipes/WebBrowser/ksh|Ripoarisch]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/WebBrowser | UbuntuHelp:PythonRecipes/WebBrowser | {{#if: | :}}PythonRecipes/WebBrowser}}/mr | • {{#if: UbuntuHelp:PythonRecipes/WebBrowser|मराठी| [[::PythonRecipes/WebBrowser/mr|मराठी]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/WebBrowser | UbuntuHelp:PythonRecipes/WebBrowser | {{#if: | :}}PythonRecipes/WebBrowser}}/ms | • {{#if: UbuntuHelp:PythonRecipes/WebBrowser|Bahasa Melayu| [[::PythonRecipes/WebBrowser/ms|Bahasa Melayu]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/WebBrowser | UbuntuHelp:PythonRecipes/WebBrowser | {{#if: | :}}PythonRecipes/WebBrowser}}/nl | • {{#if: UbuntuHelp:PythonRecipes/WebBrowser|Nederlands| [[::PythonRecipes/WebBrowser/nl|Nederlands]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/WebBrowser | UbuntuHelp:PythonRecipes/WebBrowser | {{#if: | :}}PythonRecipes/WebBrowser}}/no | • {{#if: UbuntuHelp:PythonRecipes/WebBrowser|norsk| [[::PythonRecipes/WebBrowser/no|norsk]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/WebBrowser | UbuntuHelp:PythonRecipes/WebBrowser | {{#if: | :}}PythonRecipes/WebBrowser}}/oc | • {{#if: UbuntuHelp:PythonRecipes/WebBrowser|occitan| [[::PythonRecipes/WebBrowser/oc|occitan]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/WebBrowser | UbuntuHelp:PythonRecipes/WebBrowser | {{#if: | :}}PythonRecipes/WebBrowser}}/pl | • {{#if: UbuntuHelp:PythonRecipes/WebBrowser|polski| [[::PythonRecipes/WebBrowser/pl|polski]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/WebBrowser | UbuntuHelp:PythonRecipes/WebBrowser | {{#if: | :}}PythonRecipes/WebBrowser}}/pt | • {{#if: UbuntuHelp:PythonRecipes/WebBrowser|português| [[::PythonRecipes/WebBrowser/pt|português]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/WebBrowser | UbuntuHelp:PythonRecipes/WebBrowser | {{#if: | :}}PythonRecipes/WebBrowser}}/ro | • {{#if: UbuntuHelp:PythonRecipes/WebBrowser|română| [[::PythonRecipes/WebBrowser/ro|română]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/WebBrowser | UbuntuHelp:PythonRecipes/WebBrowser | {{#if: | :}}PythonRecipes/WebBrowser}}/ru | • {{#if: UbuntuHelp:PythonRecipes/WebBrowser|русский| [[::PythonRecipes/WebBrowser/ru|русский]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/WebBrowser | UbuntuHelp:PythonRecipes/WebBrowser | {{#if: | :}}PythonRecipes/WebBrowser}}/si | • {{#if: UbuntuHelp:PythonRecipes/WebBrowser|සිංහල| [[::PythonRecipes/WebBrowser/si|සිංහල]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/WebBrowser | UbuntuHelp:PythonRecipes/WebBrowser | {{#if: | :}}PythonRecipes/WebBrowser}}/sq | • {{#if: UbuntuHelp:PythonRecipes/WebBrowser|shqip| [[::PythonRecipes/WebBrowser/sq|shqip]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/WebBrowser | UbuntuHelp:PythonRecipes/WebBrowser | {{#if: | :}}PythonRecipes/WebBrowser}}/sr | • {{#if: UbuntuHelp:PythonRecipes/WebBrowser|српски / srpski| [[::PythonRecipes/WebBrowser/sr|српски / srpski]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/WebBrowser | UbuntuHelp:PythonRecipes/WebBrowser | {{#if: | :}}PythonRecipes/WebBrowser}}/sv | • {{#if: UbuntuHelp:PythonRecipes/WebBrowser|svenska| [[::PythonRecipes/WebBrowser/sv|svenska]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/WebBrowser | UbuntuHelp:PythonRecipes/WebBrowser | {{#if: | :}}PythonRecipes/WebBrowser}}/th | • {{#if: UbuntuHelp:PythonRecipes/WebBrowser|ไทย| [[::PythonRecipes/WebBrowser/th|ไทย]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/WebBrowser | UbuntuHelp:PythonRecipes/WebBrowser | {{#if: | :}}PythonRecipes/WebBrowser}}/tr | • {{#if: UbuntuHelp:PythonRecipes/WebBrowser|Türkçe| [[::PythonRecipes/WebBrowser/tr|Türkçe]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/WebBrowser | UbuntuHelp:PythonRecipes/WebBrowser | {{#if: | :}}PythonRecipes/WebBrowser}}/vi | • {{#if: UbuntuHelp:PythonRecipes/WebBrowser|Tiếng Việt| [[::PythonRecipes/WebBrowser/vi|Tiếng Việt]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/WebBrowser | UbuntuHelp:PythonRecipes/WebBrowser | {{#if: | :}}PythonRecipes/WebBrowser}}/yue | • {{#if: UbuntuHelp:PythonRecipes/WebBrowser|粵語| [[::PythonRecipes/WebBrowser/yue|粵語]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/WebBrowser | UbuntuHelp:PythonRecipes/WebBrowser | {{#if: | :}}PythonRecipes/WebBrowser}}/zh | • {{#if: UbuntuHelp:PythonRecipes/WebBrowser|中文| [[::PythonRecipes/WebBrowser/zh|中文]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/WebBrowser | UbuntuHelp:PythonRecipes/WebBrowser | {{#if: | :}}PythonRecipes/WebBrowser}}/zh-hans | • {{#if: UbuntuHelp:PythonRecipes/WebBrowser|中文(简体)| [[::PythonRecipes/WebBrowser/zh-hans|中文(简体)]]}}|}} {{#ifexist: {{#if: UbuntuHelp:PythonRecipes/WebBrowser | UbuntuHelp:PythonRecipes/WebBrowser | {{#if: | :}}PythonRecipes/WebBrowser}}/zh-hant | • {{#if: UbuntuHelp:PythonRecipes/WebBrowser|中文(繁體)| [[::PythonRecipes/WebBrowser/zh-hant|中文(繁體)]]}}|}} |
{{#ifeq:UbuntuHelp:PythonRecipes/WebBrowser|:PythonRecipes/WebBrowser|请不要直接编辑翻译本页,本页将定期与来源同步。}} |
{{#ifexist: :PythonRecipes/WebBrowser/zh | | {{#ifexist: PythonRecipes/WebBrowser/zh | | {{#ifeq: {{#titleparts:PythonRecipes/WebBrowser|1|-1|}} | zh | | }} }} }} {{#ifeq: {{#titleparts:PythonRecipes/WebBrowser|1|-1|}} | zh | | }}
- 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):
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.