Tooltip

A Tooltip is a piece of text which is displayed when hovering over a widget to describe what the function of the widget is.

There are two types of tooltip; basic and advanced. Basic provides only textual information and in most cases will be used. Advanced allows displaying of styled or formatted text and icons.

Constructor

To apply a basic Tooltip to any widget, use the method:

widget.set_tooltip_text(text)

Alternatively, an advanced Tooltip can be created with:

tooltip = Gtk.Tooltip()

Methods

Note

The following methods only apply to the advanced Tooltip.

To set the text on the Tooltip use:

tooltip.set_text(text)

In some cases it may be useful to format the text with markup by calling:

tooltip.set_markup(markup)

The markup parameter should be set to a string, for example “<b>Text in a Tooltip</b>” would be displayed in bold.

Alternatively icons can also be used with:

tooltip.set_icon(pixbuf)

Specifying a pixbuf allows any image in the Pixbuf format to be rendered in the Tooltip.

If widgets are required to be packed into the Tooltip, then use:

tooltip.set_custom(custom_widget)

This allows packing of child widgets alongside the default Image and Label which are created at construction time.

Signals

The commonly used signals of a Tooltip are:

"query-tooltip" (widget, x, y, keyboard_mode, tooltip)

The "query-tooltip" parameter links to a function from which the Tooltip is called. There are several parameters passed to the function. The widget value identifies the widget upon which the Tooltip was called. An x and y are passed to identify the location of the cursor. The keyboard_mode value returns True or False depending on whether the Tooltip was called by the keyboard. Finally, the tooltip value indicates the Tooltip to be passed.

Examples

Below is an example of a basic Tooltip:

#!/usr/bin/env python3

from gi.repository import Gtk

window = Gtk.Window()
window.set_border_width(5)
window.connect("destroy", Gtk.main_quit)

label = Gtk.Label(label="Hover over this Label")
label.set_tooltip_text("This is an example of the basic Tooltip")
window.add(label)

window.show_all()

Gtk.main()

Download: Tooltip Basic

Alternatively, the advanced Tooltip is displayed below:

#!/usr/bin/env python3

from gi.repository import Gtk

def tooltip_query(widget, x, y, keyboard_mode, tooltip):
    tooltip.set_text("This is an example of the advanced Tooltip")

    return True

window = Gtk.Window()
window.set_border_width(5)
window.connect("destroy", Gtk.main_quit)

tooltip = Gtk.Tooltip()

label = Gtk.Label(label="Hover over this Label")
label.set_has_tooltip(True)
label.connect("query-tooltip", tooltip_query)
window.add(label)

window.show_all()

Gtk.main()

Download: Tooltip Advanced