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 πŸ˜€

Advertisement

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

    1. This web browser isn’t SeaSurf, remember. This is just an example of Python.

      And the result of this Basic Web Browser is just an address bar and a go button… Doesn’t even have forward and back! 😎

      Like

  1. Hello, I tried testing the code myself but there’s one consistent error that keeps coming up:
    def __init__(self):
    ^
    SyntaxError: expected an indented block

    Is there any way you can help out? I’d appreciate it a ton.

    Thanks,
    Kegan

    Like

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 )

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.