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

IF YOU HAVE NOT YET, PLEASE READ PART 1 BEFORE CONTINUING!

In Part 1, I showed you how to create a very basic web browser in Python with WebKit and GTK. It is a great example of Python’s power, but there was no refresh button. Part 2 focuses on adding this feature so you can refresh unresponsive pages.

First, load up the Python script you created in Part 1 and find the lines:

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

And put the following above self.address_bar = gtk.Entry()

self.refresh = gtk.ToolButton(gtk.STOCK_REFRESH)

Then find the

self.address_bar.connect(‘activate’, self.load_page)
self.gobutton.connect(‘clicked’, self.load_page)

…and put the following above self.address_bar.connect(‘activate’, self.load_page):

self.refresh.connect(‘clicked’, self.refresh_page)

Eh, what is going on here! The variable self.refresh_page doesn’t even exist. Well, it doesn’t yet. We will later add that variable so you can run this program without errors.

Now, find the lines:

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

And add the following above self.navigation.pack_start(self.address_bar):

self.navigation.pack_start(self.refresh, False)

Now find:  def change_url(self, widget, frame):
uri = frame.get_uri()
self.address_bar.set_text(uri) and press enter beneath it. If you are using a Python IDE, then press enter twice then backspace once.

Now add the following lines:

def refresh_page(self, widget):
self.webview.reload()

Now we have finished Part 2 of your basic browser. I am thinking of what to show you in Part 3 currently, but I am sure it will be awesome!

The finished code:

finished2

#!/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.refresh = gtk.ToolButton(gtk.STOCK_REFRESH)
self.address_bar = gtk.Entry()
self.gobutton = gtk.Button(“GO!”)

self.view = gtk.ScrolledWindow()
self.webview = webkit.WebView()
self.webview.open(‘http://google.com/’)
self.webview.connect(‘title-changed’, self.change_title)
self.webview.connect(‘load-committed’, self.change_url)
self.view.add(self.webview)

self.refresh.connect(‘clicked’, self.refresh_page)
self.address_bar.connect(‘activate’, self.load_page)
self.gobutton.connect(‘clicked’, self.load_page)
self.navigation.pack_start(self.refresh, False)
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)

def refresh_page(self, widget):
self.webview.reload()
browser = Basic()

 

Here is what our browser looks like now 🙂

basicbrowser2

Hope you like it!

Cheers!

Epic Chas Gamer 😀

 

 

 

 

 

 

Advertisement

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

  1. this is awesome, d00d. the other day, i was on my raspberry pi using my cell phone internet tether and wondered; wouldn’t it be nice to take off images so that sites load faster? At the moment, I use Google’s ‘Cached’ site option to load text-only versions of sites I visit… just throwing that out there. 😉 It’s wonderful that you know how to make a browser from scratch, I don’t know how to code at all 😅🔫

    later! 😁

    Liked by 1 person

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 )

Connecting to %s

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

%d bloggers like this: