Martin v. Löwis | e354d78 | 2008-12-29 16:03:04 +0000 | [diff] [blame] | 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.) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 6 | |
Martin v. Löwis | e354d78 | 2008-12-29 16:03:04 +0000 | [diff] [blame] | 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 tkinter.constants import RIGHT, LEFT, Y, BOTH |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 18 | |
Guido van Rossum | 460b6bb | 1994-07-06 21:54:39 +0000 | [diff] [blame] | 19 | class ScrolledText(Text): |
Martin v. Löwis | e354d78 | 2008-12-29 16:03:04 +0000 | [diff] [blame] | 20 | def __init__(self, master=None, **kw): |
| 21 | self.frame = Frame(master) |
| 22 | self.vbar = Scrollbar(self.frame) |
Fred Drake | d038ca8 | 2000-10-23 18:31:14 +0000 | [diff] [blame] | 23 | self.vbar.pack(side=RIGHT, fill=Y) |
Martin v. Löwis | e354d78 | 2008-12-29 16:03:04 +0000 | [diff] [blame] | 24 | |
| 25 | kw.update({'yscrollcommand': self.vbar.set}) |
| 26 | Text.__init__(self, self.frame, **kw) |
| 27 | self.pack(side=LEFT, fill=BOTH, expand=True) |
Fred Drake | d038ca8 | 2000-10-23 18:31:14 +0000 | [diff] [blame] | 28 | self.vbar['command'] = self.yview |
Guido van Rossum | 460b6bb | 1994-07-06 21:54:39 +0000 | [diff] [blame] | 29 | |
Guilherme Polo | bcd03df | 2009-08-18 15:35:57 +0000 | [diff] [blame] | 30 | # Copy geometry methods of self.frame without overriding Text |
| 31 | # methods -- hack! |
| 32 | text_meths = vars(Text).keys() |
Martin v. Löwis | e354d78 | 2008-12-29 16:03:04 +0000 | [diff] [blame] | 33 | methods = vars(Pack).keys() + vars(Grid).keys() + vars(Place).keys() |
Guilherme Polo | bcd03df | 2009-08-18 15:35:57 +0000 | [diff] [blame] | 34 | methods = set(methods).difference(text_meths) |
Guido van Rossum | 61d3637 | 2001-12-10 16:42:43 +0000 | [diff] [blame] | 35 | |
| 36 | for m in methods: |
Fred Drake | d038ca8 | 2000-10-23 18:31:14 +0000 | [diff] [blame] | 37 | if m[0] != '_' and m != 'config' and m != 'configure': |
| 38 | setattr(self, m, getattr(self.frame, m)) |
Martin v. Löwis | e354d78 | 2008-12-29 16:03:04 +0000 | [diff] [blame] | 39 | |
| 40 | def __str__(self): |
| 41 | return str(self.frame) |
| 42 | |
| 43 | |
| 44 | def example(): |
| 45 | import __main__ |
| 46 | from tkinter.constants 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 | |
| 54 | if __name__ == "__main__": |
| 55 | example() |