Skip to content

Public API

grid_manager(parent, **kwargs)

A context manager to create a grid of widgets. It is intended to simplify a placement of widgets with .grid().

Basicly, it is a wrapper around Grid class, but additionaly, it sets the parent widget of a grid (within the with statement scope), so you don't need to specify it explicitly for every widget.

Usage example:

with grid_manager(root, sticky=tk.EW) as grid:
    with grid.new_row() as row:
        row.add(tk.Label(text="0", width=20))
        row.add(tk.Label(text="1", width=20))
        row.add(tk.Label(text="2", width=20))
        row.add(tk.Label(text="3", width=20))
        row.add(tk.Label(text="4", width=20))
Source code in tkinter_layout_helpers/grid_helper.py
@contextlib.contextmanager
def grid_manager(parent: TParent, **kwargs) -> Generator[Grid, None, None]:
    """
    A context manager to create a grid of widgets. It is intended to simplify a placement of widgets with `.grid()`.

    Basicly, it is a wrapper around `Grid` class, but additionaly, it sets the parent widget of a grid
    (within the `with` statement scope), so you don't need to specify it explicitly for every widget.

    Usage example:

    ```python
    with grid_manager(root, sticky=tk.EW) as grid:
        with grid.new_row() as row:
            row.add(tk.Label(text="0", width=20))
            row.add(tk.Label(text="1", width=20))
            row.add(tk.Label(text="2", width=20))
            row.add(tk.Label(text="3", width=20))
            row.add(tk.Label(text="4", width=20))
    ```
    """
    with set_parent(parent):
        grid = Grid(parent, **kwargs)
        yield grid
        grid.build()

pack_expanded(widget, **kwargs)

Pack a widget in a parent widget expanded.

Parameters:

Name Type Description Default
widget Widget

widget to pack

required
kwargs

all additional parameters to configure the widget's position in the cell

{}
Source code in tkinter_layout_helpers/pack_helper.py
def pack_expanded(widget: tk.Widget, **kwargs) -> None:
    """
    Pack a widget in a parent widget expanded.

    Args:
        widget: widget to pack
        kwargs: all additional parameters to configure the widget's position in the cell

    """
    kwargs.update(dict(fill=tk.BOTH, expand=True))
    widget.pack(**kwargs)

pack_manager(parent, **kwargs)

A context manager to help to place widgets in window or a frame using .pack() method.

Basicly, it is a wrapper around Packer class, but additionaly, it sets the parent widget of a grid (within the with statement scope), so you don't need to specify it explicitly for every widget.

Usage example:

with pack_manager(root, fill=tk.BOTH, relief=tk.RAISED) as packer:
    packer.pack_left(tk.Label(text="Left bar"))
    packer.pack_top(tk.Label(text="Top bar"))
    packer.pack_bottom(tk.Label(text="Bottom bar"))
    packer.pack_right(tk.Label(text="Right bar"))
    packer.pack_expanded(tk.Text())
Source code in tkinter_layout_helpers/pack_helper.py
@contextlib.contextmanager
def pack_manager(parent: TParent, **kwargs) -> Generator[Packer, None, None]:
    """
    A context manager to help to place widgets in window or a frame using `.pack()` method.

    Basicly, it is a wrapper around `Packer` class, but additionaly, it sets the parent widget of a grid
    (within the `with` statement scope), so you don't need to specify it explicitly for every widget.

    Usage example:

    ```python
    with pack_manager(root, fill=tk.BOTH, relief=tk.RAISED) as packer:
        packer.pack_left(tk.Label(text="Left bar"))
        packer.pack_top(tk.Label(text="Top bar"))
        packer.pack_bottom(tk.Label(text="Bottom bar"))
        packer.pack_right(tk.Label(text="Right bar"))
        packer.pack_expanded(tk.Text())
    ```
    """
    with set_parent(parent):
        packer = Packer(parent, **kwargs)
        yield packer

set_parent(parent)

Set the parent widget for all widgets created within the with statement scope, so you will not have to pass the parent for every created widget.

Parameters:

Name Type Description Default
parent TParent

parent widget

required
Source code in tkinter_layout_helpers/parent_manager.py
@contextlib.contextmanager
def set_parent(parent: TParent) -> Generator[TParent, None, None]:
    """
    Set the parent widget for all widgets created within the `with` statement scope,
    so you will not have to pass the parent for every created widget.

    Args:
        parent: parent widget

    """
    old_root = _default_root_wrapper.default_root
    _default_root_wrapper.default_root = parent
    try:
        yield parent
    finally:
        _default_root_wrapper.default_root = old_root