blob: a1ef79ca74c74f8c1211ab52ed8a45666d7ad654 [file] [log] [blame]
Martin v. Löwise2eb2b42008-12-29 15:51:01 +00001"""A ScrolledText widget feels like a text widget but also has a
2vertical scroll bar on its right. (Later, options may be added to
3add a horizontal bar as well, to make the bars disappear
4automatically when not needed, to move them to the other side of the
5window, etc.)
Georg Brandl33cece02008-05-20 06:58:21 +00006
Martin v. Löwise2eb2b42008-12-29 15:51:01 +00007Configuration options are passed to the Text widget.
8A Frame widget is inserted between the master and the text, to hold
9the Scrollbar widget.
10Most methods calls are inherited from the Text widget; Pack, Grid and
11Place methods are redirected to the Frame widget however.
12"""
13
14__all__ = ['ScrolledText']
15
16from Tkinter import Frame, Text, Scrollbar, Pack, Grid, Place
17from Tkconstants import RIGHT, LEFT, Y, BOTH
Georg Brandl33cece02008-05-20 06:58:21 +000018
19class ScrolledText(Text):
Martin v. Löwise2eb2b42008-12-29 15:51:01 +000020 def __init__(self, master=None, **kw):
21 self.frame = Frame(master)
22 self.vbar = Scrollbar(self.frame)
Georg Brandl33cece02008-05-20 06:58:21 +000023 self.vbar.pack(side=RIGHT, fill=Y)
Martin v. Löwise2eb2b42008-12-29 15:51:01 +000024
25 kw.update({'yscrollcommand': self.vbar.set})
26 Text.__init__(self, self.frame, **kw)
27 self.pack(side=LEFT, fill=BOTH, expand=True)
Georg Brandl33cece02008-05-20 06:58:21 +000028 self.vbar['command'] = self.yview
29
Guilherme Polod3e6e4b2009-08-18 13:23:08 +000030 # Copy geometry methods of self.frame without overriding Text
31 # methods -- hack!
32 text_meths = vars(Text).keys()
Martin v. Löwise2eb2b42008-12-29 15:51:01 +000033 methods = vars(Pack).keys() + vars(Grid).keys() + vars(Place).keys()
Guilherme Polod3e6e4b2009-08-18 13:23:08 +000034 methods = set(methods).difference(text_meths)
Georg Brandl33cece02008-05-20 06:58:21 +000035
36 for m in methods:
37 if m[0] != '_' and m != 'config' and m != 'configure':
38 setattr(self, m, getattr(self.frame, m))
Martin v. Löwise2eb2b42008-12-29 15:51:01 +000039
40 def __str__(self):
41 return str(self.frame)
42
43
44def example():
45 import __main__
46 from Tkconstants import END
47
48 stext = ScrolledText(bg='white', height=10)
49 stext.insert(END, __main__.__doc__)
50 stext.pack(fill=BOTH, side=LEFT, expand=True)
51 stext.focus_set()
52 stext.mainloop()
53
54if __name__ == "__main__":
55 example()