ScrolledText.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. #
  7. # Configuration options are passed to the Text widget.
  8. # A Frame widget is inserted between the master and the text, to hold
  9. # the Scrollbar widget.
  10. # Most methods calls are inherited from the Text widget; Pack methods
  11. # are redirected to the Frame widget however.
  12. from Tkinter import *
  13. from Tkinter import _cnfmerge
  14. class ScrolledText(Text):
  15. def __init__(self, master=None, cnf=None, **kw):
  16. if cnf is None:
  17. cnf = {}
  18. if kw:
  19. cnf = _cnfmerge((cnf, kw))
  20. fcnf = {}
  21. for k in cnf.keys():
  22. if type(k) == ClassType or k == 'name':
  23. fcnf[k] = cnf[k]
  24. del cnf[k]
  25. self.frame = Frame(master, **fcnf)
  26. self.vbar = Scrollbar(self.frame, name='vbar')
  27. self.vbar.pack(side=RIGHT, fill=Y)
  28. cnf['name'] = 'text'
  29. Text.__init__(self, self.frame, **cnf)
  30. self.pack(side=LEFT, fill=BOTH, expand=1)
  31. self['yscrollcommand'] = self.vbar.set
  32. self.vbar['command'] = self.yview
  33. # Copy geometry methods of self.frame -- hack!
  34. methods = Pack.__dict__.keys()
  35. methods = methods + Grid.__dict__.keys()
  36. methods = methods + Place.__dict__.keys()
  37. for m in methods:
  38. if m[0] != '_' and m != 'config' and m != 'configure':
  39. setattr(self, m, getattr(self.frame, m))