Skip to content

tooltip module

ToolTip

A semi-transparent tooltip popup window that shows text when the mouse is hovering over the widget and closes when the mouse is no longer hovering over the widget. Clicking a mouse button will also close the tooltip.

Examples:

import ttkbootstrap as ttk
from ttkbootstrap.constants import *
from ttkbootstrap.tooltip import ToolTip

app = ttk.Window()
b1 = ttk.Button(app, text="default tooltip")
b1.pack()
b2 = ttk.Button(app, text="styled tooltip")
b2.pack()

# default tooltip
ToolTip(b1, text="This is the default style")

# styled tooltip
ToolTip(b2, text="This is dangerous", bootstyle=(DANGER, INVERSE))

app.mainloop()

__init__(self, widget, text='widget info', bootstyle=None, wraplength=None, delay=250, **kwargs) special

Parameters:

Name Type Description Default
widget Widget

The tooltip window will position over this widget when hovering.

required
text str

The text to display in the tooltip window.

'widget info'
bootstyle str

The style to apply to the tooltip label. You can use any of the standard ttkbootstrap label styles.

None
wraplength int

The width of the tooltip window in screenunits before the text is wrapped to the next line. By default, this will be a scaled factor of 300.

None
**kwargs Dict

Other keyword arguments passed to the Toplevel window.

{}
Source code in ttkbootstrap/tooltip.py
def __init__(
    self,
    widget,
    text="widget info",
    bootstyle=None,
    wraplength=None,
    delay=250,    # milliseconds
    **kwargs,
):
    """
    Parameters:

        widget (Widget):
            The tooltip window will position over this widget when
            hovering.

        text (str):
            The text to display in the tooltip window.

        bootstyle (str):
            The style to apply to the tooltip label. You can use
            any of the standard ttkbootstrap label styles.

        wraplength (int):
            The width of the tooltip window in screenunits before the
            text is wrapped to the next line. By default, this will be
            a scaled factor of 300.

        **kwargs (Dict):
            Other keyword arguments passed to the `Toplevel` window.
    """
    self.widget = widget
    self.text = text
    self.bootstyle = bootstyle
    self.wraplength = wraplength or utility.scale_size(self.widget, 300)
    self.toplevel = None
    self.delay = delay
    self.id = None

    # set keyword arguments
    kwargs["overrideredirect"] = True
    kwargs["master"] = self.widget
    if "alpha" not in kwargs:
        kwargs["alpha"] = 0.95
    self.toplevel_kwargs = kwargs

    # create default tooltip style
    ttk.Style().configure(
        style="tooltip.TLabel",
        background="#fffddd",
        foreground="#333",
        bordercolor="#888",
        borderwidth=1,
        darkcolor="#fffddd",
        lightcolor="#fffddd",
        relief=RAISED,
    )

    # event binding
    self.widget.bind("<Enter>", self.enter)
    self.widget.bind("<Leave>", self.leave)
    self.widget.bind("<Motion>", self.move_tip)
    self.widget.bind("<ButtonPress>", self.leave)

hide_tip(self, *_)

Destroy the tooltip window.

Source code in ttkbootstrap/tooltip.py
def hide_tip(self, *_):
    """Destroy the tooltip window."""
    if self.toplevel:
        self.toplevel.destroy()
        self.toplevel = None

move_tip(self, *_)

Move the tooltip window to the current mouse position within the widget.

Source code in ttkbootstrap/tooltip.py
def move_tip(self, *_):
    """Move the tooltip window to the current mouse position within the
    widget.
    """
    if self.toplevel:
        x = self.widget.winfo_pointerx() + 25
        y = self.widget.winfo_pointery() + 10
        self.toplevel.geometry(f"+{x}+{y}")

show_tip(self, *_)

Create a show the tooltip window

Source code in ttkbootstrap/tooltip.py
def show_tip(self, *_):
    """Create a show the tooltip window"""
    if self.toplevel:
        return
    x = self.widget.winfo_pointerx() + 25
    y = self.widget.winfo_pointery() + 10

    self.toplevel = ttk.Toplevel(position=(x, y), **self.toplevel_kwargs)
    lbl = ttk.Label(
        master=self.toplevel,
        text=self.text,
        justify=LEFT,
        wraplength=self.wraplength,
        padding=10,
    )
    lbl.pack(fill=BOTH, expand=YES)
    if self.bootstyle:
        lbl.configure(bootstyle=self.bootstyle)
    else:
        lbl.configure(style="tooltip.TLabel")