Python, GTK and WebKit – creating a web browser – Part 1

THIS WEB BROWSER IS NOT SEASURF. IT IS A VERY BASIC BUT RATHER FUNCTIONAL WEB BROWSER. 

Here is how to use Python, GTK and WebKit to create a very simple web browser.

First, you will need Python and the GTK and WebKit modules installed.

Here is the code for the basic web browser:

#!/usr/bin/env python
import gtk, webkit, os, pickle

class Basic():

def __init__(self):
self.window = gtk.Window()
self.window.connect('destroy', lambda w: gtk.main_quit())
self.window.set_default_size(640, 480)

self.navigation = gtk.HBox()

self.address_bar = gtk.Entry()
self.gobutton = gtk.Button("GO!")

self.view = gtk.ScrolledWindow()
self.webview = webkit.WebView()
self.webview.open('https://coolchasgamer.wordpress.com')
self.webview.connect('title-changed', self.change_title)
self.webview.connect('load-committed', self.change_url)
self.view.add(self.webview)

self.address_bar.connect('activate', self.load_page)
self.gobutton.connect('clicked', self.load_page)

self.navigation.pack_start(self.address_bar)
self.navigation.pack_start(self.gobutton, False)

self.container = gtk.VBox()
self.container.pack_start(self.navigation, False)
self.container.pack_start(self.view)

self.window.add(self.container)
self.window.show_all()
gtk.main()

def load_page(self, widget):
add = self.address_bar.get_text()
if add.startswith('http://') or add.startswith('https://') or add.startswith('file:///'):
self.webview.open(add)
else:
add = 'http://' + add
self.address_bar.set_text(add)
self.webview.open(add)

def change_title(self, widget, frame, title):
self.window.set_title(title + " - Basic Web Browser")

def change_url(self, widget, frame):
uri = frame.get_uri()
self.address_bar.set_text(uri)

browser = Basic()

The result:

basicbrowser

Enjoy!

Epic Chas Gamer 😀

8 thoughts on “Python, GTK and WebKit – creating a web browser – Part 1

Go on, you know you want to! Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s