3 # www10.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
16 if len(sys
.argv
) != 2 or sys
.argv
[1][:1] == '-':
17 print "Usage:", sys
.argv
[0], "url"
29 self
.root
.minsize(1, 1)
31 # Create topframe for the entry and button
32 self
.topframe
= Frame(self
.root
)
33 self
.topframe
.pack({'fill': 'x', 'side': 'top'})
35 # Create a label in front of the entry
36 self
.urllabel
= Label(self
.topframe
, {'text': 'URL:'})
37 self
.urllabel
.pack({'side': 'left'})
39 # Create the entry containing the URL
40 self
.entry
= Entry(self
.topframe
, {'relief': 'sunken'})
41 self
.entry
.pack({'side': 'left', 'fill': 'x', 'expand': 1})
42 self
.entry
.bind('<Return>', self
.loadit
)
45 self
.reload = Button(self
.topframe
,
47 'command': self
.reload})
48 self
.reload.pack({'side': 'right'})
50 # Create botframe for the text and scrollbar
51 self
.botframe
= Frame(self
.root
)
52 self
.botframe
.pack({'fill': 'both', 'expand': 1})
54 # The Scrollbar *must* be created first
55 self
.vbar
= Scrollbar(self
.botframe
)
56 self
.vbar
.pack({'fill': 'y', 'side': 'right'})
57 self
.text
= Text(self
.botframe
)
58 self
.text
.pack({'expand': 1, 'fill': 'both', 'side': 'left'})
60 # Link Text widget and Scrollbar
61 self
.text
['yscrollcommand'] = (self
.vbar
, 'set')
62 self
.vbar
['command'] = (self
.text
, 'yview')
67 # Load a new URL into the window
68 fp
= urllib
.urlopen(url
)
74 self
.entry
.delete('0', 'end')
75 self
.entry
.insert('end', url
)
77 self
.text
.delete('1.0', 'end')
82 if line
[-2:] == '\r\n': line
= line
[:-2] + '\n'
83 self
.text
.insert('end', line
)
84 self
.root
.update_idletasks()
92 def reload(self
, *args
):
93 # Callback for Reload button
97 def loadit(self
, *args
):
98 # Callback for <Return> event in entry
99 self
.load(self
.entry
.get())