Skip to content

ScrolledFrame

ttkbootstrap.scrolled.ScrolledFrame (Frame)

A widget container with a vertical scrollbar.

The scrollbar has an autohide feature that can be turned on by setting autohide=True.

There is unfortunately not a clean way to implement this megawidget in tkinter. A common implementation is to reference an internal frame as the master for objects to be packed, placed, etc... I've chosen to expose the internal container foremost, so that you can use this ScrolledFrame just as you would a normal frame. This is more natural. However, there are cases when you need to have the actual parent container, and for that reason, you can access this parent container object via ScrolledFrame.container. Specifically, you will need this object when adding a ScrolledFrame to a Notebook or Panedwindow. For example, mynotebook.add(myscrolledframe.container).

Examples:

import ttkbootstrap as ttk
from ttkbootstrap.constants import *
from ttkbootstrap.scrolled import ScrolledFrame

app = ttk.Window()

sf = ScrolledFrame(app, autohide=True)
sf.pack(fill=BOTH, expand=YES, padx=10, pady=10)

# add a large number of checkbuttons into the scrolled frame
for x in range(20):
    ttk.Checkbutton(sf, text=f"Checkbutton {x}").pack(anchor=W)

app.mainloop()        

__init__(self, master=None, padding=2, bootstyle='default', autohide=False, height=None, width=None, **kwargs) special

Parameters:

Name Type Description Default
master Widget

The parent widget.

None
padding int

The amount of empty space to create on the outside of the widget.

2
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.

'default'
autohide bool

When True, the scrollbars will hide when the mouse is not within the frame bbox.

False
height int

The height of the frame in screen units.

None
width int

The widget of the frame in screen units.

None
**kwargs Dict[str, Any]

Other keyword arguments.

{}
Source code in ttkbootstrap/scrolled.py
def __init__(
    self,
    master=None,
    padding=2,
    bootstyle=DEFAULT,
    autohide=False,
    height=None,
    width=None,
    **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.

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

        height (int):
            The height of the frame in screen units.

        width (int):
            The widget of the frame in screen units.

        **kwargs (Dict[str, Any]):
            Other keyword arguments.
    """
    self.container = ttk.Frame(
        master=master, 
        relief=FLAT, 
        borderwidth=0
    )
    self._canvas = ttk.Canvas(
        self.container,
        relief=FLAT,
        borderwidth=0,
        highlightthickness=0,
        height=height,
        width=width,
    )
    self._canvas.pack(fill=BOTH, expand=YES)
    self._vbar = ttk.Scrollbar(
        master=self.container,
        bootstyle=bootstyle,
        command=self._canvas.yview,
        orient=VERTICAL,
    )
    self._vbar.place(relx=1.0, relheight=1.0, anchor=NE)
    self._canvas.configure(yscrollcommand=self._vbar.set)

    super().__init__(
        master=self._canvas, 
        padding=padding, 
        bootstyle=bootstyle, 
        **kwargs
    )
    self._winsys = self.tk.call('tk', 'windowingsystem')
    self._wid = self._canvas.create_window((0, 0), anchor=NW, window=self)

    # delegate text methods to frame
    _methods = vars(Pack).keys() | vars(Grid).keys() | vars(Place).keys()
    for method in _methods:
        if any(["pack" in method, "grid" in method, "place" in method]):
            setattr(self, method, getattr(self.container, method))

    self.bind("<Configure>", self._resize_canvas)
    self._canvas.bind("<Enter>", self._enable_scrolling, "+")
    self._canvas.bind("<Leave>", self._disable_scrolling, "+")

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

autohide_scrollbar(self, *_)

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

Source code in ttkbootstrap/scrolled.py
def autohide_scrollbar(self, *_):
    """Show the scrollbars when the mouse enters the widget frame,
    and hide when it leaves the frame."""
    self.container.bind("<Enter>", self.show_scrollbars)
    self.container.bind("<Leave>", self.hide_scrollbars)

hide_scrollbars(self, *_)

Hide the scrollbars.

Source code in ttkbootstrap/scrolled.py
def hide_scrollbars(self, *_):
    """Hide the scrollbars."""
    try:
        self._vbar.lower(self._canvas)
    except:
        pass
    try:
        self._hbar.lower(self._canvas)
    except:
        pass

show_scrollbars(self, *_)

Show the scrollbars.

Source code in ttkbootstrap/scrolled.py
def show_scrollbars(self, *_):
    """Show the scrollbars."""
    try:
        self._vbar.lift(self._canvas)
    except:
        pass
    try:
        self._hbar.lift(self._canvas)
    except:
        pass