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

Hey guys! Welcome to Part 3 of my Python, GTK and WebKit – creating a web browser tutorial. Part 3 will show you how to add an “About” button to the browser. The about dialog will look like:

aboutbasic

Add the following few lines to the beginning of the code:

import Tkinter
from ScrolledText import *
import tkFileDialog
import tkMessageBox

Then we add the About button and dialog and all that. Here is the finished code for Part 3:

#!/usr/bin/env python
import gtk, webkit, os, pickle
import Tkinter
from ScrolledText import *
import tkFileDialog
import tkMessageBox

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.about = gtk.ToolButton(gtk.STOCK_DIALOG_INFO)

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.about.connect(‘clicked’, self.about_browser)
self.navigation.pack_start(self.refresh, False)
self.navigation.pack_start(self.address_bar)
self.navigation.pack_start(self.gobutton, False)
self.navigation.pack_start(self.about, 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 about_browser(self, widget):
root = Tkinter.Tk(className=””)
SeaSurfAbout = ScrolledText(root, width=50, height=50)
label = tkMessageBox.showinfo(“”, “Basic Web Browser \n Version 1.0″)
root.destroy()

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()

I wonder what is in store for part 4?

Epic Chas Gamer 😀

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