3 # www12.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
11 # - menu bar; added 'master' option to constructor
19 if len(sys
.argv
) != 2 or sys
.argv
[1][:1] == '-':
20 print "Usage:", sys
.argv
[0], "url"
31 def __init__(self
, master
= None):
34 self
.root
= self
.master
= Tk()
37 self
.root
= Toplevel(self
.master
)
38 self
.root
.minsize(1, 1)
41 self
.mbar
= Frame(self
.root
,
44 self
.mbar
.pack({'fill': 'x'})
47 self
.filebutton
= Menubutton(self
.mbar
, {'text': 'File'})
48 self
.filebutton
.pack({'side': 'left'})
50 self
.filemenu
= Menu(self
.filebutton
)
51 self
.filebutton
['menu'] = self
.filemenu
54 self
.editbutton
= Menubutton(self
.mbar
, {'text': 'Edit'})
55 self
.editbutton
.pack({'side': 'left'})
57 self
.editmenu
= Menu(self
.editbutton
)
58 self
.editbutton
['menu'] = self
.editmenu
60 # Magic so you can swipe from one button to the next
61 self
.mbar
.tk_menuBar(self
.filebutton
, self
.editbutton
)
64 self
.filemenu
.add('command', {'label': 'New',
65 'command': self
.new_command
})
66 self
.filemenu
.add('command', {'label': 'Open...',
67 'command': self
.open_command
})
68 self
.filemenu
.add('command', {'label': 'Clone',
69 'command': self
.clone_command
})
70 self
.filemenu
.add('separator')
71 self
.filemenu
.add('command', {'label': 'Close',
72 'command': self
.close_command
})
73 self
.filemenu
.add('command', {'label': 'Quit',
74 'command': self
.quit_command
})
79 # Create topframe for the entry and button
80 self
.topframe
= Frame(self
.root
)
81 self
.topframe
.pack({'fill': 'x'})
83 # Create a label in front of the entry
84 self
.urllabel
= Label(self
.topframe
, {'text': 'URL:'})
85 self
.urllabel
.pack({'side': 'left'})
87 # Create the entry containing the URL
88 self
.entry
= Entry(self
.topframe
,
89 {'relief': 'sunken', 'border': 2})
90 self
.entry
.pack({'side': 'left', 'fill': 'x', 'expand': 1})
91 self
.entry
.bind('<Return>', self
.loadit
)
94 self
.reload = Button(self
.topframe
,
96 'command': self
.reload})
97 self
.reload.pack({'side': 'right'})
99 # Create botframe for the text and scrollbar
100 self
.botframe
= Frame(self
.root
)
101 self
.botframe
.pack({'fill': 'both', 'expand': 1})
103 # The Scrollbar *must* be created first
104 self
.vbar
= Scrollbar(self
.botframe
)
105 self
.vbar
.pack({'fill': 'y', 'side': 'right'})
106 self
.text
= Text(self
.botframe
)
107 self
.text
.pack({'expand': 1, 'fill': 'both', 'side': 'left'})
109 # Link Text widget and Scrollbar
110 self
.text
['yscrollcommand'] = (self
.vbar
, 'set')
111 self
.vbar
['command'] = (self
.text
, 'yview')
116 # Load a new URL into the window
117 fp
, url
= self
.urlopen(url
)
125 self
.entry
.delete('0', 'end')
126 self
.entry
.insert('end', url
)
128 self
.text
.delete('0.0', 'end')
133 if line
[-2:] == '\r\n': line
= line
[:-2] + '\n'
134 self
.text
.insert('end', line
)
135 self
.root
.update_idletasks()
139 def urlopen(self
, url
):
141 # return (fp, url) if successful
142 # display dialog and return (None, url) for errors
144 fp
= urllib
.urlopen(url
)
147 if type(msg
) == types
.TupleType
and len(msg
) == 4:
150 if m
.has_key('location'):
152 return self
.urlopen(url
)
153 elif m
.has_key('uri'):
155 return self
.urlopen(url
)
156 self
.errordialog(IOError, msg
)
160 def errordialog(self
, exc
, msg
):
161 # Display an error dialog -- return when the user clicks OK
162 Dialog
.Dialog(self
.root
, {
174 def reload(self
, *args
):
175 # Callback for Reload button
179 def loadit(self
, *args
):
180 # Callback for <Return> event in entry
181 self
.load(self
.entry
.get())
183 def new_command(self
):
187 def clone_command(self
):
189 v
= Viewer(self
.master
)
192 def open_command(self
):
194 print "File/Open...: Not implemented"
196 def close_command(self
):
200 def quit_command(self
):
205 # Destroy this window
207 if self
.master
is not self
.root
and not self
.master
.children
: