- 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.

Here’s the complete code that works in Python 3.x:

from tkinter import *
import urllib.request
def go():
	text.delete(1.0, END)
	with urllib.request.urlopen(entry.get()) as response:
		received_html =
	text.insert(1.0, received_html)

browser_window = Tk()
browser_window.title('knowpapa browser')
label = Label(browser_window, text= 'Enter URL:')
entry = Entry(browser_window)
entry.insert(END, "")
button = Button(browser_window, text='Go', command = go)
text = Text(browser_window)
text.pack(side= TOP)

Note that this code will NOT WORK on Python 2.x as the urllib module has different behavior in both the versions.

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 sooner or later you will have to get your hands dirty with writing your own parsers.

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 fetches 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 look 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).

Concluding remarks

Implementing a full-fledged web browser in Tkinter can be a daunting task and is not meant for beginners.If you still insist on using Tkinter, I would suggest you look at the source code of the Grail Browser. This web browser was originally written by Guido Van Rossum (inventor of Python) and it uses Tkinter library for the GUI.
I should warn that this though is not for the faint-hearted.

A simpler approach could be to use another GUI module(instead of Tkinter) such as wxPython or PyQT.Both these packages have a built-in widget for viewing HTML content.

wxPython, for instance, has a webview widget, while pyQT has qwebview. So you will not need to write and integrate HTML parser and writing a bare but functional browser should be easy.

If you believe in doing some-thing more state of the art, you can look at newer GUI library called Kiwi which provides a cross-platform development option that will work even on phones and tablets and is in my opinion, more stylish than other GUI modules but at the expense of adding some complexity.