Skip to content

Pack Helper

Packer

Bases: Generic[TParent]

Builder class to pack widgets in a window or a frame.

Source code in tkinter_layout_helpers/pack_helper.py
class Packer(Generic[TParent]):
    """Builder class to pack widgets in a window or a frame."""

    parent: TParent
    __kwargs: Mapping[str, Any]

    def __init__(self, parent: TParent, **kwargs) -> None:
        """
        Initialize a packer object.

        Args:
            parent: parent widget
            kwargs: common parameters to configure the widgets placement with `.pack()` method.

        """
        self.parent = parent
        self.__kwargs = kwargs

    def pack_all(self, *args: tk.Widget, **kwargs) -> None:
        """
        Pack all widgets in a window or a frame.

        Args:
            args: widgets to pack
            kwargs: all additional parameters to configure the widgets placement with `.pack()` method.

        """
        kwargs.update(self.__kwargs)
        for item in args:
            item.pack(**kwargs)

    def pack(self, widget: tk.Widget, **kwargs) -> Self:
        """
        Pack a widget in a window or a frame.

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

        """
        kwargs.update(self.__kwargs)
        widget.pack(**kwargs)
        return self

    def pack_left(self, widget: tk.Widget, **kwargs) -> Self:
        """
        Pack a widget in a window or a frame to the left.

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

        """
        kwargs.update(self.__kwargs)
        widget.pack(side=tk.LEFT, **kwargs)
        return self

    def pack_right(self, widget: tk.Widget, **kwargs) -> Self:
        """
        Pack a widget in a window or a frame to the right.

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

        """
        kwargs.update(self.__kwargs)
        widget.pack(side=tk.RIGHT, **kwargs)
        return self

    def pack_top(self, widget: tk.Widget, **kwargs) -> Self:
        """
        Pack a widget in a window or a frame to the top.

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

        """
        kwargs.update(self.__kwargs)
        widget.pack(side=tk.TOP, **kwargs)
        return self

    def pack_bottom(self, widget: tk.Widget, **kwargs) -> Self:
        """
        Pack a widget in a window or a frame to the bottom.

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

        """
        kwargs.update(self.__kwargs)
        widget.pack(side=tk.BOTTOM, **kwargs)
        return self

    def pack_expanded(self, widget: tk.Widget, **kwargs) -> Self:
        """
        Pack a widget in a window or a frame expanded.

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

        """
        pack_expanded(widget, **self.__kwargs, **kwargs)
        return self

__init__(parent, **kwargs)

Initialize a packer object.

Parameters:

Name Type Description Default
parent TParent

parent widget

required
kwargs

common parameters to configure the widgets placement with .pack() method.

{}
Source code in tkinter_layout_helpers/pack_helper.py
def __init__(self, parent: TParent, **kwargs) -> None:
    """
    Initialize a packer object.

    Args:
        parent: parent widget
        kwargs: common parameters to configure the widgets placement with `.pack()` method.

    """
    self.parent = parent
    self.__kwargs = kwargs

pack(widget, **kwargs)

Pack a widget in a window or a frame.

Parameters:

Name Type Description Default
widget Widget

widget to pack

required
kwargs

all additional parameters to configure the widget's position.

{}
Source code in tkinter_layout_helpers/pack_helper.py
def pack(self, widget: tk.Widget, **kwargs) -> Self:
    """
    Pack a widget in a window or a frame.

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

    """
    kwargs.update(self.__kwargs)
    widget.pack(**kwargs)
    return self

pack_all(*args, **kwargs)

Pack all widgets in a window or a frame.

Parameters:

Name Type Description Default
args Widget

widgets to pack

()
kwargs

all additional parameters to configure the widgets placement with .pack() method.

{}
Source code in tkinter_layout_helpers/pack_helper.py
def pack_all(self, *args: tk.Widget, **kwargs) -> None:
    """
    Pack all widgets in a window or a frame.

    Args:
        args: widgets to pack
        kwargs: all additional parameters to configure the widgets placement with `.pack()` method.

    """
    kwargs.update(self.__kwargs)
    for item in args:
        item.pack(**kwargs)

pack_bottom(widget, **kwargs)

Pack a widget in a window or a frame to the bottom.

Parameters:

Name Type Description Default
widget Widget

widget to pack

required
kwargs

all additional parameters to configure the widget's position.

{}
Source code in tkinter_layout_helpers/pack_helper.py
def pack_bottom(self, widget: tk.Widget, **kwargs) -> Self:
    """
    Pack a widget in a window or a frame to the bottom.

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

    """
    kwargs.update(self.__kwargs)
    widget.pack(side=tk.BOTTOM, **kwargs)
    return self

pack_expanded(widget, **kwargs)

Pack a widget in a window or a frame expanded.

Parameters:

Name Type Description Default
widget Widget

widget to pack

required
kwargs

all additional parameters to configure the widget's position.

{}
Source code in tkinter_layout_helpers/pack_helper.py
def pack_expanded(self, widget: tk.Widget, **kwargs) -> Self:
    """
    Pack a widget in a window or a frame expanded.

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

    """
    pack_expanded(widget, **self.__kwargs, **kwargs)
    return self

pack_left(widget, **kwargs)

Pack a widget in a window or a frame to the left.

Parameters:

Name Type Description Default
widget Widget

widget to pack

required
kwargs

all additional parameters to configure the widget's position.

{}
Source code in tkinter_layout_helpers/pack_helper.py
def pack_left(self, widget: tk.Widget, **kwargs) -> Self:
    """
    Pack a widget in a window or a frame to the left.

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

    """
    kwargs.update(self.__kwargs)
    widget.pack(side=tk.LEFT, **kwargs)
    return self

pack_right(widget, **kwargs)

Pack a widget in a window or a frame to the right.

Parameters:

Name Type Description Default
widget Widget

widget to pack

required
kwargs

all additional parameters to configure the widget's position.

{}
Source code in tkinter_layout_helpers/pack_helper.py
def pack_right(self, widget: tk.Widget, **kwargs) -> Self:
    """
    Pack a widget in a window or a frame to the right.

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

    """
    kwargs.update(self.__kwargs)
    widget.pack(side=tk.RIGHT, **kwargs)
    return self

pack_top(widget, **kwargs)

Pack a widget in a window or a frame to the top.

Parameters:

Name Type Description Default
widget Widget

widget to pack

required
kwargs

all additional parameters to configure the widget's position.

{}
Source code in tkinter_layout_helpers/pack_helper.py
def pack_top(self, widget: tk.Widget, **kwargs) -> Self:
    """
    Pack a widget in a window or a frame to the top.

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

    """
    kwargs.update(self.__kwargs)
    widget.pack(side=tk.TOP, **kwargs)
    return self

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