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 | # www8.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 | |
| 9 | import sys |
| 10 | import urllib |
| 11 | from Tkinter import * |
| 12 | |
| 13 | def main(): |
| 14 | if len(sys.argv) != 2 or sys.argv[1][:1] == '-': |
| 15 | print "Usage:", sys.argv[0], "url" |
| 16 | sys.exit(2) |
| 17 | url = sys.argv[1] |
| 18 | fp = urllib.urlopen(url) |
| 19 | |
| 20 | # Create root window |
| 21 | root = Tk() |
| 22 | root.title(url) |
| 23 | root.minsize(1, 1) |
| 24 | |
| 25 | # The Scrollbar *must* be created first -- this is magic for me :-( |
| 26 | vbar = Scrollbar(root) |
| 27 | vbar.pack({'fill': 'y', 'side': 'right'}) |
| 28 | text = Text(root, {'yscrollcommand': (vbar, 'set')}) |
| 29 | text.pack({'expand': 1, 'fill': 'both', 'side': 'left'}) |
| 30 | |
| 31 | # Link Text widget and Scrollbar -- this is magic for you :-) |
| 32 | ##text['yscrollcommand'] = (vbar, 'set') |
| 33 | vbar['command'] = (text, 'yview') |
| 34 | |
| 35 | while 1: |
| 36 | line = fp.readline() |
| 37 | if not line: break |
| 38 | text.insert('end', line) |
| 39 | root.update_idletasks() |
| 40 | |
| 41 | root.mainloop() |
| 42 | |
| 43 | main() |