Skip to content

toast module

ToastNotification

A semi-transparent popup window for temporary alerts or messages. You may choose to display the toast for a specified period of time, otherwise you must click the toast to close it.

toast notification

Examples:

import ttkbootstrap as ttk
from ttkbootstrap.toast import ToastNotification

app = ttk.Window()

toast = ToastNotification(
    title="ttkbootstrap toast message",
    message="This is a toast message",
    duration=3000,
)
toast.show_toast()

app.mainloop()

__init__(self, title, message, duration=None, bootstyle='light', alert=False, icon=None, iconfont=None, position=None, **kwargs) special

Parameters:

Name Type Description Default
title str

The toast title.

required
message str

The toast message.

required
duration int

The number of milliseconds to show the toast. If None (default), then you must click the toast to close it.

None
bootstyle str

Style keywords used to updated the label style. One of the accepted color keywords.

'light'
alert bool

Indicates whether to ring the display bell when the toast is shown.

False
icon str

A unicode character to display on the top-left hand corner of the toast. The default symbol is OS specific. Pass an empty string to remove the symbol.

None
iconfont Union[str, Font]

The font used to render the icon. By default, this is OS specific. You may need to change the font to enable better character or emoji support for the icon you want to use. Windows (Segoe UI Symbol), Linux (FreeSerif), MacOS (Apple Symbol)

None
position Tuple[int, int, str]

A tuple that controls the position of the toast. Default is OS specific. The tuple cooresponds to (horizontal, vertical, anchor), where the horizontal and vertical elements represent the position of the toplevel releative to the anchor, which is "ne" or top-left by default. Acceptable anchors include: n, e, s, w, nw, ne, sw, se. For example: (100, 100, 'ne').

None
**kwargs Dict

Other keyword arguments passed to the Toplevel window.

{}
Source code in ttkbootstrap/toast.py
def __init__(
    self,
    title,
    message,
    duration=None,
    bootstyle=LIGHT,
    alert=False,
    icon=None,
    iconfont=None,
    position=None,
    **kwargs,
):
    """
    Parameters:

        title (str):
            The toast title.

        message (str):
            The toast message.

        duration (int):
            The number of milliseconds to show the toast. If None
            (default), then you must click the toast to close it.

        bootstyle (str):
            Style keywords used to updated the label style. One of
            the accepted color keywords.

        alert (bool):
            Indicates whether to ring the display bell when the
            toast is shown.

        icon (str):
            A unicode character to display on the top-left hand
            corner of the toast. The default symbol is OS specific.
            Pass an empty string to remove the symbol.

        iconfont (Union[str, Font]):
            The font used to render the icon. By default, this is
            OS specific. You may need to change the font to enable
            better character or emoji support for the icon you
            want to use. Windows (Segoe UI Symbol),
            Linux (FreeSerif), MacOS (Apple Symbol)

        position (Tuple[int, int, str]):
            A tuple that controls the position of the toast. Default
            is OS specific. The tuple cooresponds to
            (horizontal, vertical, anchor), where the horizontal and
            vertical elements represent the position of the toplevel
            releative to the anchor, which is "ne" or top-left by
            default. Acceptable anchors include: n, e, s, w, nw, ne,
            sw, se. For example: (100, 100, 'ne').

        **kwargs (Dict):
            Other keyword arguments passed to the `Toplevel` window.
    """
    self.message = message
    self.title = title
    self.duration = duration
    self.bootstyle = bootstyle
    self.icon = icon
    self.iconfont = iconfont
    self.iconfont = None
    self.titlefont = None
    self.toplevel = None
    self.kwargs = kwargs
    self.alert = alert
    self.position = position

    if "overrideredirect" not in self.kwargs:
        self.kwargs["overrideredirect"] = True
    if "alpha" not in self.kwargs:
        self.kwargs["alpha"] = 0.95

    if position is not None:
        if len(position) != 3:
            self.position = None

hide_toast(self, *_)

Destroy and close the toast window.

Source code in ttkbootstrap/toast.py
def hide_toast(self, *_):
    """Destroy and close the toast window."""
    try:
        alpha = float(self.toplevel.attributes("-alpha"))
        if alpha <= 0.1:
            self.toplevel.destroy()
        else:
            self.toplevel.attributes("-alpha", alpha - 0.1)
            self.toplevel.after(25, self.hide_toast)
    except:
        if self.toplevel:
            self.toplevel.destroy()

show_toast(self, *_)

Create and show the toast window.

Source code in ttkbootstrap/toast.py
def show_toast(self, *_):
    """Create and show the toast window."""

    # build toast
    self.toplevel = ttk.Toplevel(**self.kwargs)
    self._setup(self.toplevel)

    self.container = ttk.Frame(self.toplevel, bootstyle=self.bootstyle)
    self.container.pack(fill=BOTH, expand=YES)
    ttk.Label(
        self.container,
        text=self.icon,
        font=self.iconfont,
        bootstyle=f"{self.bootstyle}-inverse",
        anchor=NW,
    ).grid(row=0, column=0, rowspan=2, sticky=NSEW, padx=(5, 0))
    ttk.Label(
        self.container,
        text=self.title,
        font=self.titlefont,
        bootstyle=f"{self.bootstyle}-inverse",
        anchor=NW,
    ).grid(row=0, column=1, sticky=NSEW, padx=10, pady=(5, 0))
    ttk.Label(
        self.container,
        text=self.message,
        wraplength=utility.scale_size(self.toplevel, 300),
        bootstyle=f"{self.bootstyle}-inverse",
        anchor=NW,
    ).grid(row=1, column=1, sticky=NSEW, padx=10, pady=(0, 5))

    self.toplevel.bind("<ButtonPress>", self.hide_toast)

    # alert toast
    if self.alert:
        self.toplevel.bell()

    # specified duration to close
    if self.duration:
        self.toplevel.after(self.duration, self.hide_toast)