The 0.5 release happened on 2/15, not on 2/14. :-)
[python/dscho.git] / Demo / tkinter / www / www10.py
blobeef5220281cb3a8f07985e5a013f35a19f1e8ed0
1 #! /usr/bin/env python
3 # www10.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 # - editable url entry and reload button
11 import sys
12 import urllib
13 from Tkinter import *
15 def main():
16 if len(sys.argv) != 2 or sys.argv[1][:1] == '-':
17 print "Usage:", sys.argv[0], "url"
18 sys.exit(2)
19 url = sys.argv[1]
20 viewer = Viewer()
21 viewer.load(url)
22 viewer.go()
24 class Viewer:
26 def __init__(self):
27 # Create root window
28 self.root = Tk()
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)
44 # Create the button
45 self.reload = Button(self.topframe,
46 {'text': 'Reload',
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')
64 self.url = None
66 def load(self, url):
67 # Load a new URL into the window
68 fp = urllib.urlopen(url)
70 self.url = url
72 self.root.title(url)
74 self.entry.delete('0', 'end')
75 self.entry.insert('end', url)
77 self.text.delete('1.0', 'end')
79 while 1:
80 line = fp.readline()
81 if not line: break
82 if line[-2:] == '\r\n': line = line[:-2] + '\n'
83 self.text.insert('end', line)
84 self.root.update_idletasks()
86 fp.close()
88 def go(self):
89 # Start Tk main loop
90 self.root.mainloop()
92 def reload(self, *args):
93 # Callback for Reload button
94 if self.url:
95 self.load(self.url)
97 def loadit(self, *args):
98 # Callback for <Return> event in entry
99 self.load(self.entry.get())
101 main()