Native FileChooser Dialogs
Embedding a web browser inside PyGTK is rather easy, although there can be problems. If when starting the browser you get a "Segmentation fault (core dumped)" read the "Run MozEmbed" page.
If you are using Microsoft Windows and would like to embed a browser in PyGTK you will want to take a look at ie_in_gtk.py. It embeds Internet Explorer and is not covered in this tutorial. I found it on the internet, not sure where anymore. Seems to work ok.
The Example Browser found here is based off the MozEmbed reference found at http://pygtk.org/pygtkmozembed/class-gtkmozembed.html.
To use MozEmbed you have to import gtkmozembed:
import gtkmozembed
The browser is initiated by calling gtkmozembed.MozEmbed() like so:
browser = gtkmozembed.MozEmbed()
With that you now have a browser with the name of browser. It can now be added to pygtk container (such as a window, or HBox, VBox, etc...) and load html data or a web page. To render data you would use:
browser.render_data(data, len, base_uri, mime_type)
In our case we use something like the following:
browser.render_data("html code", len("html code"), 'file:///', 'text/html')
Web pages can be loaded using load_url(url).
browser.load_url("http://majorsilence.com")
Everything else in the code below is just PyGTK code to display the window and is in every PyGTK program.
To run this code read the "Run MozEmbed" page.
#!/usr/bin/python
import gtk
import gtkmozembed
class ExampleBrowser(object):
def __init__(self):
data = """<html><head><title>Hello</title></head>
<body>
PyGTK using MozEmbed to embed a web browser.
</body>
</html>"""
win = gtk.Window()
win.set_size_request(800, 600)
win.connect("delete_event", lambda w,e: gtk.main_quit())
vbox = gtk.VBox(False, 0)
# Initiate MozEmbed
self.browser = gtkmozembed.MozEmbed()
vbox.add(self.browser)
win.add(vbox)
win.show_all()
# Display the html data
self.browser.render_data(data, long(len(data)), 'file:///', 'text/html')
if __name__ == '__main__':
ExampleBrowser()
gtk.main()
To run a program with MozEmbed you may have to set some library paths. If you get a "Segmentation fault (core dumped)" error on the command line, not having the proper libray paths set is probably at fault.
I do not know about other linux distros, but on Ubuntu the library path that will be set depends on the version you are using.
Using Ubuntu Feisty, Edgy, or Dapper do the following:
export LD_LIBRARY_PATH=/usr/lib/firefox
Using Ubuntu Gutsy do the following:
export LD_LIBRARY_PATH=/usr/lib/firefox
export MOZILLA_FIVE_HOME=/usr/lib/firefox
As an example, on Ubuntu Gutsy you may want to create a shell script that
sets the library path and launches your application.
export LD_LIBRARY_PATH=/usr/lib/firefox
export MOZILLA_FIVE_HOME=/usr/lib/firefox
python MyWebBrowser.py
Other functions that you will want to be aware of are:
can_go_forward() - Returns True if is able to go back.
go_back() - Go back to the previous viewed page
can_go_forward() - Returns True if able to go forward.
go_forward() - Go forward in navigation history.
reload(gtkmozembed.FLAG_RELOADNORMAL) - Reload the currently viewed page.
stop_load() - Stop loading the current page.
load_url("http://www.majorsilence.com") - Loads the specified web address.
So basically the way this code works is that there are some buttons (Back, Forward, Refresh, Stop, Home, and Go) and an address bar. The buttons are setup to call their respective MozEmbed functions.
Examine the code below. It should be self explanatory.
#!/usr/bin/python
import gtk
import gtkmozembed
class ExampleBrowser(object):
def __init__(self):
data = """<html><head><title>Hello</title></head>
<body>
PyGTK using MozEmbed to embed a web browser.
</body>
</html>"""
win = gtk.Window()
win.set_size_request(800, 600)
win.connect("delete_event", lambda w,e: gtk.main_quit())
# gtk.VBox(homogeneous=False, spacing=0)
vbox = gtk.VBox(False, 0)
control_box = gtk.HBox(False, 0)
#pack_start(button, expand, fill, padding)
back = gtk.Button("Back")
forward = gtk.Button("Forward")
refresh = gtk.Button("Refresh")
stop = gtk.Button("Stop")
home = gtk.Button("Home")
self.address = gtk.Entry(max=0) # no limit on address length
go = gtk.Button("Go")
control_box.pack_start(back, True, True, 2)
control_box.pack_start(forward, True, True, 2)
control_box.pack_start(refresh, True, True, 2)
control_box.pack_start(stop, True, True, 2)
control_box.pack_start(home, True, True, 2)
control_box.pack_start(self.address, True, True, 2)
control_box.pack_start(go, True, True, 2)
back.connect("clicked", self.on_back_clicked, None)
forward.connect("clicked", self.on_forward_clicked, None)
refresh.connect("clicked", self.on_refresh_clicked, None)
stop.connect("clicked", self.on_stop_clicked, None)
home.connect("clicked", self.on_home_clicked, data)
self.address.connect("key_press_event", self.on_address_keypress)
go.connect("clicked", self.on_go_clicked, None)
vbox.pack_start(control_box, False, True, 2)
self.browser = gtkmozembed.MozEmbed()
#gtkmozembed.set_profile_path("/tmp", "foobar")
vbox.add(self.browser)
win.add(vbox)
win.show_all()
## self.browser.load_url('http://www.pygtk.org')
self.browser.render_data(data, long(len(data)), 'file:///', 'text/html')
# Load file from file system
#self.browser.load_url('file:///path/to/file/name.html')
def on_back_clicked(self, widget=None, data=None):
print "Back button clicked."
if self.browser.can_go_back():
self.browser.go_back()
def on_forward_clicked(self, widget=None, data=None):
print "Forward button clicked."
if self.browser.can_go_forward():
self.browser.go_forward()
def on_refresh_clicked(self, widget=None, data=None):
print "Refresh button clicked."
self.browser.reload(gtkmozembed.FLAG_RELOADNORMAL)
def on_stop_clicked(self, widget=None, data=None):
print "Stop Button Clicked."
self.browser.stop_load()
def on_home_clicked(self, widget=None, data=None):
print "Home Button clicked."
print "Back button only works on actual pages and not render_data"
self.browser.render_data(data, long(len(data)), 'file:///', 'text/html')
def on_address_keypress(self, widget, event):
print "Key event in address bar"
if gtk.gdk.keyval_name(event.keyval) == "Return":
print "Key press: Return"
self.on_go_clicked(None)
def on_go_clicked(self, widget=None, data=None):
print "Go Button Clicked."
self.browser.load_url(self.address.get_text())
if __name__ == '__main__':
ExampleBrowser()
gtk.main()