1 #! /usr/local/bin/python
3 # www11.py -- display the contents of a URL in a Text widget
5 # - make window resizable
6 # - update display while reading
7 # - vertical scroll bar
9 # - editable url entry and reload button
18 if len(sys
.argv
) != 2 or sys
.argv
[1][:1] == '-':
19 print "Usage:", sys
.argv
[0], "url"
31 self
.root
.minsize(1, 1)
33 # Create topframe for the entry and button
34 self
.topframe
= Frame(self
.root
)
35 self
.topframe
.pack({'fill': 'x'})
37 # Create a label in front of the entry
38 self
.urllabel
= Label(self
.topframe
, {'text': 'URL:'})
39 self
.urllabel
.pack({'side': 'left'})
41 # Create the entry containing the URL
42 self
.entry
= Entry(self
.topframe
,
43 {'relief': 'sunken', 'border': 2})
44 self
.entry
.pack({'side': 'left', 'fill': 'x', 'expand': 1})
45 self
.entry
.bind('<Return>', self
.loadit
)
48 self
.reload = Button(self
.topframe
,
50 'command': self
.reload})
51 self
.reload.pack({'side': 'right'})
53 # Create botframe for the text and scrollbar
54 self
.botframe
= Frame(self
.root
)
55 self
.botframe
.pack({'fill': 'both', 'expand': 1})
57 # The Scrollbar *must* be created first
58 self
.vbar
= Scrollbar(self
.botframe
)
59 self
.vbar
.pack({'fill': 'y', 'side': 'right'})
60 self
.text
= Text(self
.botframe
)
61 self
.text
.pack({'expand': 1, 'fill': 'both', 'side': 'left'})
63 # Link Text widget and Scrollbar
64 self
.text
['yscrollcommand'] = (self
.vbar
, 'set')
65 self
.vbar
['command'] = (self
.text
, 'yview')
70 # Load a new URL into the window
71 fp
, url
= self
.urlopen(url
)
79 self
.entry
.delete('0', 'end')
80 self
.entry
.insert('end', url
)
82 self
.text
.delete('0.0', 'end')
87 if line
[-2:] == '\r\n': line
= line
[:-2] + '\n'
88 self
.text
.insert('end', line
)
89 self
.root
.update_idletasks()
93 def urlopen(self
, url
):
95 # return (fp, url) if successful
96 # display dialog and return (None, url) for errors
98 fp
= urllib
.urlopen(url
)
101 if type(msg
) == types
.TupleType
and len(msg
) == 4:
104 if m
.has_key('location'):
106 return self
.urlopen(url
)
107 elif m
.has_key('uri'):
109 return self
.urlopen(url
)
110 self
.errordialog(IOError, msg
)
114 def errordialog(self
, exc
, msg
):
115 # Display an error dialog -- return when the user clicks OK
116 Dialog
.Dialog(self
.root
, {
128 def reload(self
, *args
):
129 # Callback for Reload button
133 def loadit(self
, *args
):
134 # Callback for <Return> event in entry
135 self
.load(self
.entry
.get())