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, Grid and
|
---|
11 | Place methods are redirected to the Frame widget however.
|
---|
12 | """
|
---|
13 |
|
---|
14 | __all__ = ['ScrolledText']
|
---|
15 |
|
---|
16 | from Tkinter import Frame, Text, Scrollbar, Pack, Grid, Place
|
---|
17 | from Tkconstants import RIGHT, LEFT, Y, BOTH
|
---|
18 |
|
---|
19 | class ScrolledText(Text):
|
---|
20 | def __init__(self, master=None, **kw):
|
---|
21 | self.frame = Frame(master)
|
---|
22 | self.vbar = Scrollbar(self.frame)
|
---|
23 | self.vbar.pack(side=RIGHT, fill=Y)
|
---|
24 |
|
---|
25 | kw.update({'yscrollcommand': self.vbar.set})
|
---|
26 | Text.__init__(self, self.frame, **kw)
|
---|
27 | self.pack(side=LEFT, fill=BOTH, expand=True)
|
---|
28 | self.vbar['command'] = self.yview
|
---|
29 |
|
---|
30 | # Copy geometry methods of self.frame -- hack!
|
---|
31 | methods = vars(Pack).keys() + vars(Grid).keys() + vars(Place).keys()
|
---|
32 |
|
---|
33 | for m in methods:
|
---|
34 | if m[0] != '_' and m != 'config' and m != 'configure':
|
---|
35 | setattr(self, m, getattr(self.frame, m))
|
---|
36 |
|
---|
37 | def __str__(self):
|
---|
38 | return str(self.frame)
|
---|
39 |
|
---|
40 |
|
---|
41 | def example():
|
---|
42 | import __main__
|
---|
43 | from Tkconstants import END
|
---|
44 |
|
---|
45 | stext = ScrolledText(bg='white', height=10)
|
---|
46 | stext.insert(END, __main__.__doc__)
|
---|
47 | stext.pack(fill=BOTH, side=LEFT, expand=True)
|
---|
48 | stext.focus_set()
|
---|
49 | stext.mainloop()
|
---|
50 |
|
---|
51 | if __name__ == "__main__":
|
---|
52 | example()
|
---|