python-examples: b231b58946ea3784c4d1c86b0766e6d9e01ee145
1: # Web browser in 20 lines of Python (excluding comments)
2: # Made by Chris Warburton whilst consulting the example browser of
3: # PyWebKitGTK by Jan Alonzo <jmalonzo@unpluggable.com>,
4: # One Laptop Per Child and Red Hat, Inc.
5: # This is Public Domain. Chop it, mix it, fiddle around, it's meant for
6: # learning.
7:
8: import gtk, webkit
9:
10: # Define a web browser as a type of window
11: class WebBrowser(gtk.Window):
12:
13: def __init__(self):
14: # Set up the regular window stuff
15: super(WebBrowser, self).__init__()
16:
17: # When the window closes we want the program to stop
18: self.connect('destroy', gtk.main_quit)
19:
20: # We need 2 things stacked vertically, so make a box which
21: # stacks things vertically, then add it to the window
22: self.vbox = gtk.VBox()
23: self.add(self.vbox)
24:
25: # Make a text entry box to input a new address and add it to
26: # the vertically stacking box so that it only expands across
27: self.addressbar = gtk.Entry()
28: self.vbox.pack_start(self.addressbar, expand=False, fill=False)
29: # When an address is entered run "location_entered"
30: self.addressbar.connect('activate', self.location_entered)
31:
32: # The page may be too big for the window, so make a box which
33: # can scroll and add it to the vertically stacking box
34: self.scroller = gtk.ScrolledWindow()
35: self.vbox.add(self.scroller)
36:
37: # Make the actual page viewer and put it in the scrolling box
38: self.webpage = webkit.WebView()
39: self.scroller.add(self.webpage)
40:
41: # Finally make everything visible
42: self.show_all()
43:
44: def location_entered(self, addressbar):
45: # This is run when an address is entered and tells the webpage
46: # viewer to load the new address
47: self.webpage.open(addressbar.props.text)
48:
49: # Make one of the web browsers we just defined
50: webbrowser = WebBrowser()
51:
52: # Run the program
53: gtk.main()
Generated by git2html.