/Lib/lib-tk/ScrolledText.py

http://unladen-swallow.googlecode.com/ · Python · 52 lines · 28 code · 11 blank · 13 comment · 5 complexity · 50dc3ae7624b2e741858230ce83f880b MD5 · raw file

  1. """A ScrolledText widget feels like a text widget but also has a
  2. vertical scroll bar on its right. (Later, options may be added to
  3. add a horizontal bar as well, to make the bars disappear
  4. automatically when not needed, to move them to the other side of the
  5. window, etc.)
  6. Configuration options are passed to the Text widget.
  7. A Frame widget is inserted between the master and the text, to hold
  8. the Scrollbar widget.
  9. Most methods calls are inherited from the Text widget; Pack, Grid and
  10. Place methods are redirected to the Frame widget however.
  11. """
  12. __all__ = ['ScrolledText']
  13. from Tkinter import Frame, Text, Scrollbar, Pack, Grid, Place
  14. from Tkconstants import RIGHT, LEFT, Y, BOTH
  15. class ScrolledText(Text):
  16. def __init__(self, master=None, **kw):
  17. self.frame = Frame(master)
  18. self.vbar = Scrollbar(self.frame)
  19. self.vbar.pack(side=RIGHT, fill=Y)
  20. kw.update({'yscrollcommand': self.vbar.set})
  21. Text.__init__(self, self.frame, **kw)
  22. self.pack(side=LEFT, fill=BOTH, expand=True)
  23. self.vbar['command'] = self.yview
  24. # Copy geometry methods of self.frame -- hack!
  25. methods = vars(Pack).keys() + vars(Grid).keys() + vars(Place).keys()
  26. for m in methods:
  27. if m[0] != '_' and m != 'config' and m != 'configure':
  28. setattr(self, m, getattr(self.frame, m))
  29. def __str__(self):
  30. return str(self.frame)
  31. def example():
  32. import __main__
  33. from Tkconstants import END
  34. stext = ScrolledText(bg='white', height=10)
  35. stext.insert(END, __main__.__doc__)
  36. stext.pack(fill=BOTH, side=LEFT, expand=True)
  37. stext.focus_set()
  38. stext.mainloop()
  39. if __name__ == "__main__":
  40. example()