跳转至

ScrolledFrame

ttkbootstrap.scrolled.ScrolledFrame (Frame)

A widget container with a vertical scrollbar.

The ScrolledFrame fills the width of its container. The height is either set explicitly or is determined by the content frame's contents.

This widget behaves mostly like a normal frame other than the exceptions stated already. Another exception is when packing it into a Notebook or Panedwindow. In this case, you'll need to add the container instead of the content frame. For example, mynotebook.add(myscrolledframe.container).

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

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=200, width=300, scrollheight=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 container frame in screen units.

200
width int

The width of the container frame in screen units.

300
scrollheight int

The height of the content frame in screen units. If None, the height is determined by the frame contents.

None
**kwargs Dict[str, Any]

Other keyword arguments passed to the content frame.

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

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

        scrollheight (int):
            The height of the content frame in screen units. If None,
            the height is determined by the frame contents.

        **kwargs (Dict[str, Any]):
            Other keyword arguments passed to the content frame.
    """
    # content frame container
    self.container = ttk.Frame(
        master=master,
        relief=FLAT,
        borderwidth=0,
        width=width,
        height=height,
    )
    self.container.bind("<Configure>", lambda _: self.yview())
    self.container.propagate(0)

    # content frame
    super().__init__(
        master=self.container,
        padding=padding,
        bootstyle=bootstyle.replace('round', ''),
        width=width,
        height=height,
        **kwargs,
    )
    self.place(rely=0.0, relwidth=1.0, height=scrollheight)

    # vertical scrollbar
    self.vscroll = ttk.Scrollbar(
        master=self.container,
        command=self.yview,
        orient=VERTICAL,
        bootstyle=bootstyle,
    )
    self.vscroll.pack(side=RIGHT, fill=Y)

    self.winsys = self.tk.call("tk", "windowingsystem")

    # setup autohide scrollbar
    self.autohide = autohide
    if self.autohide:
        self.hide_scrollbars()

    # widget event binding
    self.container.bind("<Enter>", self._on_enter, "+")
    self.container.bind("<Leave>", self._on_leave, "+")
    self.container.bind("<Map>", self._on_map, "+")
    self.bind("<<MapChild>>", self._on_map_child, "+")

    # delegate content geometry methods to container 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]):
            # prefix content frame methods with 'content_'
            setattr(self, f"content_{method}", getattr(self, method))
            # overwrite content frame methods from container frame
            setattr(self, method, getattr(self.container, method))

autohide_scrollbar(self)

Toggle the autohide funtionality. 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):
    """Toggle the autohide funtionality. Show the scrollbars when
    the mouse enters the widget frame, and hide when it leaves the
    frame."""
    self.autohide = not self.autohide

disable_scrolling(self)

Disable mousewheel scrolling on the frame and all of its children.

Source code in ttkbootstrap/scrolled.py
def disable_scrolling(self):
    """Disable mousewheel scrolling on the frame and all of its
    children."""
    self._del_scroll_binding(self)

enable_scrolling(self)

Enable mousewheel scrolling on the frame and all of its children.

Source code in ttkbootstrap/scrolled.py
def enable_scrolling(self):
    """Enable mousewheel scrolling on the frame and all of its
    children."""
    self._add_scroll_binding(self)

hide_scrollbars(self)

Hide the scrollbars.

Source code in ttkbootstrap/scrolled.py
def hide_scrollbars(self):
    """Hide the scrollbars."""
    self.vscroll.pack_forget()

show_scrollbars(self)

Show the scrollbars.

Source code in ttkbootstrap/scrolled.py
def show_scrollbars(self):
    """Show the scrollbars."""
    self.vscroll.pack(side=RIGHT, fill=Y)

yview(self, *args)

Update the vertical position of the content frame within the container.

Parameters:

Name Type Description Default
*args List[Any, ...]

Optional arguments passed to yview in order to move the content frame within the container frame.

()
Source code in ttkbootstrap/scrolled.py
def yview(self, *args):
    """Update the vertical position of the content frame within the
    container.

    Parameters:

        *args (List[Any, ...]):
            Optional arguments passed to yview in order to move the
            content frame within the container frame.
    """
    if not args:
        first, _ = self.vscroll.get()
        self.yview_moveto(fraction=first)
    elif args[0] == "moveto":
        self.yview_moveto(fraction=float(args[1]))
    elif args[0] == "scroll":
        self.yview_scroll(number=int(args[1]), what=args[2])
    else:
        return

yview_moveto(self, fraction)

Update the vertical position of the content frame within the container.

Parameters:

Name Type Description Default
fraction float

The relative position of the content frame within the container.

required
Source code in ttkbootstrap/scrolled.py
def yview_moveto(self, fraction: float):
    """Update the vertical position of the content frame within the
    container.

    Parameters:

        fraction (float):
            The relative position of the content frame within the
            container.
    """
    base, thumb = self._measures()
    if fraction < 0:
        first = 0.0
    elif (fraction + thumb) > 1:
        first = 1 - thumb
    else:
        first = fraction
    self.vscroll.set(first, first + thumb)
    self.content_place(rely=-first * base)

yview_scroll(self, number, what)

Update the vertical position of the content frame within the container.

Parameters:

Name Type Description Default
number int

The amount by which the content frame will be moved within the container frame by 'what' units.

required
what str

The type of units by which the number is to be interpeted. This parameter is currently not used and is assumed to be 'units'.

required
Source code in ttkbootstrap/scrolled.py
def yview_scroll(self, number: int, what: str):
    """Update the vertical position of the content frame within the
    container.

    Parameters:

        number (int):
            The amount by which the content frame will be moved
            within the container frame by 'what' units.

        what (str):
            The type of units by which the number is to be interpeted.
            This parameter is currently not used and is assumed to be
            'units'.
    """
    first, _ = self.vscroll.get()
    fraction = (number / 100) + first
    self.yview_moveto(fraction)