Guido van Rossum | f06ee5f | 1996-11-27 19:52:01 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
Guido van Rossum | dfa70a9 | 1995-01-10 17:05:37 +0000 | [diff] [blame] | 2 | |
| 3 | # www9.py -- display the contents of a URL in a Text widget |
| 4 | # - set window title |
| 5 | # - make window resizable |
| 6 | # - update display while reading |
| 7 | # - vertical scroll bar |
| 8 | # - rewritten as class |
| 9 | |
| 10 | import sys |
| 11 | import urllib |
| 12 | from Tkinter import * |
| 13 | |
| 14 | def main(): |
| 15 | if len(sys.argv) != 2 or sys.argv[1][:1] == '-': |
| 16 | print "Usage:", sys.argv[0], "url" |
| 17 | sys.exit(2) |
| 18 | url = sys.argv[1] |
| 19 | viewer = Viewer() |
| 20 | viewer.load(url) |
| 21 | viewer.go() |
| 22 | |
| 23 | class Viewer: |
| 24 | |
| 25 | def __init__(self): |
| 26 | # Create root window |
| 27 | self.root = Tk() |
| 28 | self.root.minsize(1, 1) |
| 29 | |
| 30 | # The Scrollbar *must* be created first |
| 31 | self.vbar = Scrollbar(self.root) |
| 32 | self.vbar.pack({'fill': 'y', 'side': 'right'}) |
| 33 | self.text = Text(self.root) |
| 34 | self.text.pack({'expand': 1, 'fill': 'both', 'side': 'left'}) |
| 35 | |
| 36 | # Link Text widget and Scrollbar |
| 37 | self.text['yscrollcommand'] = (self.vbar, 'set') |
| 38 | self.vbar['command'] = (self.text, 'yview') |
| 39 | |
| 40 | def load(self, url): |
| 41 | # Load a new URL into the window |
| 42 | fp = urllib.urlopen(url) |
| 43 | |
| 44 | self.root.title(url) |
| 45 | |
| 46 | self.text.delete('0.0', 'end') |
| 47 | |
| 48 | while 1: |
| 49 | line = fp.readline() |
| 50 | if not line: break |
| 51 | self.text.insert('end', line) |
| 52 | self.root.update_idletasks() |
| 53 | |
| 54 | fp.close() |
| 55 | |
| 56 | def go(self): |
| 57 | # Start Tk main loop |
| 58 | self.root.mainloop() |
| 59 | |
| 60 | main() |