Exemplo n.º 1
0
def add_tooltip(
    targ_widget: tk.Misc,
    text: str='',
    image: img.Handle=None,
    delay: int=500,
    show_when_disabled: bool=False,
) -> None:
    """Add a tooltip to the specified widget.

    delay is the amount of milliseconds of hovering needed to show the
    tooltip.
    text is the initial text for the tooltip.
    If set, image is also shown on the tooltip.
    If show_when_disabled is false, no context menu will be shown if the
    target widget is disabled.
    """
    targ_widget._bee2_tooltip_text = text
    targ_widget._bee2_tooltip_img = image

    event_id = None  # The id of the enter event, so we can cancel it.

    # Only check for disabled widgets if the widget actually has a state,
    # and the user hasn't disabled the functionality
    check_disabled = hasattr(targ_widget, 'instate') and not show_when_disabled

    def after_complete(x, y):
        """Remove the id and show the tooltip after the delay."""
        nonlocal event_id
        event_id = None  # Invalidate event id
        # noinspection PyUnresolvedReferences, PyProtectedMember
        if targ_widget._bee2_tooltip_text or targ_widget._bee2_tooltip_img is not None:
            _show(targ_widget, x, y)

    def enter_handler(event):
        """Schedule showing the tooltip."""
        nonlocal event_id
        # noinspection PyUnresolvedReferences, PyProtectedMember
        if targ_widget._bee2_tooltip_text or targ_widget._bee2_tooltip_img is not None:
            # We know it has this method from above!
            # noinspection PyUnresolvedReferences
            if check_disabled and not targ_widget.instate(('!disabled',)):
                return
            event_id = TK_ROOT.after(
                delay,
                after_complete,
                event.x_root, event.y_root,
            )

    def exit_handler(e):
        """When the user leaves, cancel the event."""
        # We only want to cancel if the event hasn't expired already
        nonlocal event_id
        window.withdraw()
        if event_id is not None:
            TK_ROOT.after_cancel(
                event_id
            )

    targ_widget.bind('<Enter>', enter_handler)
    targ_widget.bind('<Leave>', exit_handler)
Exemplo n.º 2
0
def add_tooltip(
    targ_widget: tk.Misc,
    text: str='',
    image: tk.Image=None,
    delay: int=500,
    show_when_disabled: bool=False,
) -> None:
    """Add a tooltip to the specified widget.

    delay is the amount of milliseconds of hovering needed to show the
    tooltip.
    text is the initial text for the tooltip.
    If set, image is also shown on the tooltip.
    If show_when_disabled is false, no context menu will be shown if the
    target widget is disabled.
    """
    targ_widget._bee2_tooltip_text = text
    targ_widget._bee2_tooltip_img = image

    event_id = None  # The id of the enter event, so we can cancel it.

    # Only check for disabled widgets if the widget actually has a state,
    # and the user hasn't disabled the functionality
    check_disabled = hasattr(targ_widget, 'instate') and not show_when_disabled

    def after_complete(x, y):
        """Remove the id and show the tooltip after the delay."""
        nonlocal event_id
        event_id = None  # Invalidate event id
        # noinspection PyUnresolvedReferences, PyProtectedMember
        if targ_widget._bee2_tooltip_text or targ_widget._bee2_tooltip_img is not None:
            _show(targ_widget, x, y)

    def enter_handler(event):
        """Schedule showing the tooltip."""
        nonlocal event_id
        # noinspection PyUnresolvedReferences, PyProtectedMember
        if targ_widget._bee2_tooltip_text or targ_widget._bee2_tooltip_img is not None:
            # We know it has this method from above!
            # noinspection PyUnresolvedReferences
            if check_disabled and not targ_widget.instate(('!disabled',)):
                return
            event_id = TK_ROOT.after(
                delay,
                after_complete,
                event.x_root, event.y_root,
            )

    def exit_handler(e):
        """When the user leaves, cancel the event."""
        # We only want to cancel if the event hasn't expired already
        nonlocal event_id
        window.withdraw()
        if event_id is not None:
            TK_ROOT.after_cancel(
                event_id
            )

    targ_widget.bind('<Enter>', enter_handler)
    targ_widget.bind('<Leave>', exit_handler)
Exemplo n.º 3
0
def set_tooltip(widget: tk.Misc, text: str='', image: img.Handle=None):
    """Change the tooltip for a widget."""
    widget._bee2_tooltip_text = text
    widget._bee2_tooltip_img = image
Exemplo n.º 4
0
def set_tooltip(widget: tk.Misc, text: str='', image: tk.Image=None):
    """Change the tooltip for a widget."""
    widget._bee2_tooltip_text = text
    widget._bee2_tooltip_img = image