Skip to content

ScrolledText

Bases: Frame

A text widget with optional vertical and horizontal scrollbars. Setting autohide=True will cause the scrollbars to hide when the mouse is not over the widget. The vertical scrollbar is on by default, but can be turned off. The horizontal scrollbar can be enabled by setting vbar=True.

This widget is identical in configuration to the Text widget other than the scrolling frame. https://tcl.tk/man/tcl8.6/TkCmd/text.htm

scrolled text

Examples:

```python
import ttkbootstrap as ttk
from ttkbootstrap.constants import *
from ttkbootstrap.scrolled import ScrolledText

app = ttk.Window()

# scrolled text with autohide vertical scrollbar
st = ScrolledText(app, padding=5, height=10, autohide=True)
st.pack(fill=BOTH, expand=YES)

# add text
st.insert(END, 'Insert your text here.')

app.mainloop()
```
Source code in src/ttkbootstrap/widgets/scrolled.py
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
class ScrolledText(ttk.Frame):
    """A text widget with optional vertical and horizontal scrollbars.
    Setting `autohide=True` will cause the scrollbars to hide when the
    mouse is not over the widget. The vertical scrollbar is on by
    default, but can be turned off. The horizontal scrollbar can be
    enabled by setting `vbar=True`.

    This widget is identical in configuration to the `Text` widget other
    than the scrolling frame. https://tcl.tk/man/tcl8.6/TkCmd/text.htm

    ![scrolled text](../../../assets/scrolled/scrolledtext.gif)

    Examples:

        ```python
        import ttkbootstrap as ttk
        from ttkbootstrap.constants import *
        from ttkbootstrap.scrolled import ScrolledText

        app = ttk.Window()

        # scrolled text with autohide vertical scrollbar
        st = ScrolledText(app, padding=5, height=10, autohide=True)
        st.pack(fill=BOTH, expand=YES)

        # add text
        st.insert(END, 'Insert your text here.')

        app.mainloop()
        ```
    """

    def __init__(
            self,
            master: Optional[tkinter.Misc] = None,
            padding: int = 2,
            bootstyle: str = DEFAULT,
            autohide: bool = False,
            vbar: bool = True,
            hbar: bool = False,
            **kwargs: Any,
    ) -> None:
        """
        Parameters:

            master (Widget):
                The parent widget.

            padding (int):
                The amount of empty space to create on the outside of
                the widget.

            bootstyle (str):
                A style keyword used to set the color and style of the
                vertical scrollbar. Available options include -> primary,
                secondary, success, info, warning, danger, dark, light.

            vbar (bool):
                A vertical scrollbar is shown when **True** (default).

            hbar (bool):
                A horizontal scrollbar is shown when **True**. Turning
                on this scrollbar will also set `wrap="none"`. This
                scrollbar is _off_ by default.

            autohide (bool):
                When **True**, the scrollbars will hide when the mouse
                is not within the frame bbox.

            **kwargs (dict[str, Any]):
                Other keyword arguments passed to the `Text` widget.
        """
        super().__init__(master, padding=padding)

        # setup text widget
        self._text: ttk.Text = ttk.Text(self, padx=50, **kwargs)
        self._hbar: Optional[ttk.Scrollbar] = None
        self._vbar: Optional[ttk.Scrollbar] = None

        # delegate text methods to frame
        for method in vars(ttk.Text).keys():
            if any(["pack" in method, "grid" in method, "place" in method]):
                pass
            else:
                setattr(self, method, getattr(self._text, method))

        # setup scrollbars
        if vbar:
            self._vbar = ttk.Scrollbar(
                master=self,
                bootstyle=bootstyle,
                command=self._text.yview,
                orient=VERTICAL,
            )
            self._vbar.place(relx=1.0, relheight=1.0, anchor=NE)
            self._text.configure(yscrollcommand=self._vbar.set)

        if hbar:
            self._hbar = ttk.Scrollbar(
                master=self,
                bootstyle=bootstyle,
                command=self._text.xview,
                orient=HORIZONTAL,
            )
            self._hbar.place(rely=1.0, relwidth=1.0, anchor=SW)
            self._text.configure(xscrollcommand=self._hbar.set, wrap="none")

        self._text.pack(side=LEFT, fill=BOTH, expand=YES)

        # position scrollbars
        if self._hbar:
            self.update_idletasks()
            self._text_width = self.winfo_reqwidth()
            self._scroll_width = self.winfo_reqwidth()

        self.bind("<Configure>", self._on_configure)

        if autohide:
            self.autohide_scrollbar()
            self.hide_scrollbars()

    def _on_configure(self, *args: Any) -> None:
        """Callback for when the configure method is used."""
        if self._hbar:
            self.update_idletasks()
            text_width = self.winfo_width()
            vbar_width = self._vbar.winfo_width()
            relx = (text_width - vbar_width) / text_width
            self._hbar.place(rely=1.0, relwidth=relx)

    def __getattr__(self, name: str) -> Any:
        """Delegate unknown attribute access to the internal Text widget.

        This supports static type checkers by indicating that any missing
        attribute on ScrolledText should be treated as Any and forwarded to
        the underlying `ttk.Text` instance.
        """
        return getattr(self._text, name)

    @property
    def text(self) -> ttk.Text:
        """Return the internal text widget.

        Returns:

            ttk.Text: The underlying text widget instance.
        """
        return self._text

    @property
    def hbar(self) -> Optional[ttk.Scrollbar]:
        """Return the internal horizontal scrollbar.

        Returns:

            Optional[ttk.Scrollbar]: The horizontal scrollbar, if created.
        """
        return self._hbar

    @property
    def vbar(self) -> Optional[ttk.Scrollbar]:
        """Return the internal vertical scrollbar.

        Returns:

            Optional[ttk.Scrollbar]: The vertical scrollbar, if created.
        """
        return self._vbar

    def hide_scrollbars(self, *args: Any) -> None:
        """Hide the scrollbars."""
        try:
            self._vbar.lower(self._text)
        except:
            pass
        try:
            self._hbar.lower(self._text)
        except:
            pass

    def show_scrollbars(self, *args: Any) -> None:
        """Show the scrollbars."""
        try:
            self._vbar.lift(self._text)
        except:
            pass
        try:
            self._hbar.lift(self._text)
        except:
            pass

    def autohide_scrollbar(self, *args: Any) -> None:
        """Show the scrollbars when the mouse enters the widget frame,
        and hide when it leaves the frame."""
        self.bind("<Enter>", self.show_scrollbars)
        self.bind("<Leave>", self.hide_scrollbars)

hbar property

Return the internal horizontal scrollbar.

Returns:

Optional[ttk.Scrollbar]: The horizontal scrollbar, if created.

text property

Return the internal text widget.

Returns:

ttk.Text: The underlying text widget instance.

vbar property

Return the internal vertical scrollbar.

Returns:

Optional[ttk.Scrollbar]: The vertical scrollbar, if created.

__getattr__(name)

Delegate unknown attribute access to the internal Text widget.

This supports static type checkers by indicating that any missing attribute on ScrolledText should be treated as Any and forwarded to the underlying ttk.Text instance.

Source code in src/ttkbootstrap/widgets/scrolled.py
179
180
181
182
183
184
185
186
def __getattr__(self, name: str) -> Any:
    """Delegate unknown attribute access to the internal Text widget.

    This supports static type checkers by indicating that any missing
    attribute on ScrolledText should be treated as Any and forwarded to
    the underlying `ttk.Text` instance.
    """
    return getattr(self._text, name)

__init__(master=None, padding=2, bootstyle=DEFAULT, autohide=False, vbar=True, hbar=False, **kwargs)

Parameters:

master (Widget):
    The parent widget.

padding (int):
    The amount of empty space to create on the outside of
    the widget.

bootstyle (str):
    A style keyword used to set the color and style of the
    vertical scrollbar. Available options include -> primary,
    secondary, success, info, warning, danger, dark, light.

vbar (bool):
    A vertical scrollbar is shown when **True** (default).

hbar (bool):
    A horizontal scrollbar is shown when **True**. Turning
    on this scrollbar will also set `wrap="none"`. This
    scrollbar is _off_ by default.

autohide (bool):
    When **True**, the scrollbars will hide when the mouse
    is not within the frame bbox.

**kwargs (dict[str, Any]):
    Other keyword arguments passed to the `Text` widget.
Source code in src/ttkbootstrap/widgets/scrolled.py
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
def __init__(
        self,
        master: Optional[tkinter.Misc] = None,
        padding: int = 2,
        bootstyle: str = DEFAULT,
        autohide: bool = False,
        vbar: bool = True,
        hbar: bool = False,
        **kwargs: Any,
) -> None:
    """
    Parameters:

        master (Widget):
            The parent widget.

        padding (int):
            The amount of empty space to create on the outside of
            the widget.

        bootstyle (str):
            A style keyword used to set the color and style of the
            vertical scrollbar. Available options include -> primary,
            secondary, success, info, warning, danger, dark, light.

        vbar (bool):
            A vertical scrollbar is shown when **True** (default).

        hbar (bool):
            A horizontal scrollbar is shown when **True**. Turning
            on this scrollbar will also set `wrap="none"`. This
            scrollbar is _off_ by default.

        autohide (bool):
            When **True**, the scrollbars will hide when the mouse
            is not within the frame bbox.

        **kwargs (dict[str, Any]):
            Other keyword arguments passed to the `Text` widget.
    """
    super().__init__(master, padding=padding)

    # setup text widget
    self._text: ttk.Text = ttk.Text(self, padx=50, **kwargs)
    self._hbar: Optional[ttk.Scrollbar] = None
    self._vbar: Optional[ttk.Scrollbar] = None

    # delegate text methods to frame
    for method in vars(ttk.Text).keys():
        if any(["pack" in method, "grid" in method, "place" in method]):
            pass
        else:
            setattr(self, method, getattr(self._text, method))

    # setup scrollbars
    if vbar:
        self._vbar = ttk.Scrollbar(
            master=self,
            bootstyle=bootstyle,
            command=self._text.yview,
            orient=VERTICAL,
        )
        self._vbar.place(relx=1.0, relheight=1.0, anchor=NE)
        self._text.configure(yscrollcommand=self._vbar.set)

    if hbar:
        self._hbar = ttk.Scrollbar(
            master=self,
            bootstyle=bootstyle,
            command=self._text.xview,
            orient=HORIZONTAL,
        )
        self._hbar.place(rely=1.0, relwidth=1.0, anchor=SW)
        self._text.configure(xscrollcommand=self._hbar.set, wrap="none")

    self._text.pack(side=LEFT, fill=BOTH, expand=YES)

    # position scrollbars
    if self._hbar:
        self.update_idletasks()
        self._text_width = self.winfo_reqwidth()
        self._scroll_width = self.winfo_reqwidth()

    self.bind("<Configure>", self._on_configure)

    if autohide:
        self.autohide_scrollbar()
        self.hide_scrollbars()

autohide_scrollbar(*args)

Show the scrollbars when the mouse enters the widget frame, and hide when it leaves the frame.

Source code in src/ttkbootstrap/widgets/scrolled.py
240
241
242
243
244
def autohide_scrollbar(self, *args: Any) -> None:
    """Show the scrollbars when the mouse enters the widget frame,
    and hide when it leaves the frame."""
    self.bind("<Enter>", self.show_scrollbars)
    self.bind("<Leave>", self.hide_scrollbars)

hide_scrollbars(*args)

Hide the scrollbars.

Source code in src/ttkbootstrap/widgets/scrolled.py
218
219
220
221
222
223
224
225
226
227
def hide_scrollbars(self, *args: Any) -> None:
    """Hide the scrollbars."""
    try:
        self._vbar.lower(self._text)
    except:
        pass
    try:
        self._hbar.lower(self._text)
    except:
        pass

show_scrollbars(*args)

Show the scrollbars.

Source code in src/ttkbootstrap/widgets/scrolled.py
229
230
231
232
233
234
235
236
237
238
def show_scrollbars(self, *args: Any) -> None:
    """Show the scrollbars."""
    try:
        self._vbar.lift(self._text)
    except:
        pass
    try:
        self._hbar.lift(self._text)
    except:
        pass