- a developer's blog

Simplest Web Browser in Python & Tkinter – 32 lines of code

Want to develop a web browser in python – let me get you started with a small working prototype.
This GUI interface done in Tkinter and uses the urllib module to get the url data.

The web browser currently looks like this:

As you can see, the browser can read the contents of a site from the server. It currently does not parse HTML – hence you see raw html code for the fetched web page.

The design is also far from satisfactory – but the idea is to let a beginner have fun getting started with the project – not to write a full fledged production browser.

The Code So far

1) Fetching Data from the URL (

import urllib
class urlhandler:
	address = ''
	data = ''
	def __init__(self, url):
		self.address = url
	def openURL(self): =  urllib.urlopen(self.address)
	def getURLdata(self):
	def closeURL(self):

2) The GUI for the Browser (

from Tkinter import *
import urlhandler
def go():
	text.delete(1.0, END)
	conn = urlhandler.urlhandler(entry.get())
	text.insert(INSERT, conn.getURLdata())
browserwindow = Tk()
browserwindow.title('knowpapa browser')
label = Label(browserwindow, text= 'url:')
entry = Entry(browserwindow)
button = Button(browserwindow, text='Go', command = go)
text = Text(browserwindow)
text.pack(side= TOP)

Note that Tkinter does not have a built-in `webview` widget unlike other GUI toolkits like pyGTk or pyQt. This means that you will have to extend the `Text` widget to write your own `webview` widget. This would involve writing your own html, css and javascript parsers which can be hell lot of task.

This makes it inherently difficult to write a browser in Tkinter. However even with other toolkits, their `webview` widgets do not process Javascript. This means that soner or later you will have to get your hands dirty with writing your own parsers. So why not begin right away – getting a finer control over your browser right from the start.

Suggestions for Adding New features

You can possibly think of adding the following capabilities to the browser:
1) Fetch the URL content on a new thread of execution as the current code blocks the GUI loop while the browser fethces the content. This should be easy. Use the threading module from Python standard library.

2) HTML parsing – You can check out the standard python library for html parsing.

3) Add a Javascript Engine – You can llok at the source code of engines like V8 – Rhino – SpiderMonkey – Tamarin

4) Add a Layout engine (look inside WebKit,Gecko, Presto)

5) Ability to Handle Forms

6) Ability to Handle Cookies – urllib2 again !

7) Strengthening Security

8) Manage authentication information for a series of requests- urllib2 again !

9) Managing proxies – urllib2 !

10) Improve the GUI to make it look like from this century (Tkinter ttk library).

Look at the menu for any proper browser(Firefox, IE, Opera or Google Chrome) for tons of ideas for new features that can be added.

Download Source Code

And here’s the source code.
To run the browser – run the script.

If you have any more ideas for development or suggestions to make – leave it as a comment.