home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyos2bin.zip / Demo / tkinter / www / www10.py < prev    next >
Text File  |  1996-11-27  |  2KB  |  102 lines

  1. #! /usr/bin/env python
  2.  
  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
  10.  
  11. import sys
  12. import urllib
  13. from Tkinter import *
  14.  
  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()
  23.  
  24. class Viewer:
  25.  
  26.     def __init__(self):
  27.         # Create root window
  28.         self.root = Tk()
  29.         self.root.minsize(1, 1)
  30.  
  31.         # Create topframe for the entry and button
  32.         self.topframe = Frame(self.root)
  33.         self.topframe.pack({'fill': 'x', 'side': 'top'})
  34.  
  35.         # Create a label in front of the entry
  36.         self.urllabel = Label(self.topframe, {'text': 'URL:'})
  37.         self.urllabel.pack({'side': 'left'})
  38.  
  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)
  43.  
  44.         # Create the button
  45.         self.reload = Button(self.topframe,
  46.                      {'text': 'Reload',
  47.                       'command': self.reload})
  48.         self.reload.pack({'side': 'right'})
  49.  
  50.         # Create botframe for the text and scrollbar
  51.         self.botframe = Frame(self.root)
  52.         self.botframe.pack({'fill': 'both', 'expand': 1})
  53.  
  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'})
  59.  
  60.         # Link Text widget and Scrollbar
  61.         self.text['yscrollcommand'] = (self.vbar, 'set')
  62.         self.vbar['command'] = (self.text, 'yview')
  63.  
  64.         self.url = None
  65.  
  66.     def load(self, url):
  67.         # Load a new URL into the window
  68.         fp = urllib.urlopen(url)
  69.  
  70.         self.url = url
  71.  
  72.         self.root.title(url)
  73.  
  74.         self.entry.delete('0', 'end')
  75.         self.entry.insert('end', url)
  76.  
  77.         self.text.delete('1.0', 'end')
  78.  
  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()
  85.  
  86.         fp.close()
  87.  
  88.     def go(self):
  89.         # Start Tk main loop
  90.         self.root.mainloop()
  91.  
  92.     def reload(self, *args):
  93.         # Callback for Reload button
  94.         if self.url:
  95.             self.load(self.url)
  96.  
  97.     def loadit(self, *args):
  98.         # Callback for <Return> event in entry
  99.         self.load(self.entry.get())
  100.  
  101. main()
  102.