10.3 The Tooltips Object

These are the little text strings that pop up when you leave your pointer over a button or other widget for a few seconds. They are easy to use, so I will just give a short example on how to use them.

Widgets that do not receive events (widgets that do not have thier own window) will not work with tooltips.

The first call you will use creates a new tooltip. You will need to do this once for a set of tooltips as the Gtk::Tooltips object this method returns can be used to create multiple tooltips.

Gtk::Tooltips.new

Once you have created the new tooltip, and the widget you wish to use it on, simply use this call to set it:

Gtk::Tooltips.set_tip( widget, tip_text, tip_private )

The first argument is the widget you wish to use this tooltip pop up for, and the text you wish to say. The last argument is a text string that can be used as an identifier to implement context sensitive help. For now, you can set it to nil.

Here is a short example

require 'gtk2'

Gtk.init
window = Gtk::Window.new( Gtk::Window::TOPLEVEL )
window.title=( "Tooltips" )
window.signal_connect( "destroy" ) { Gtk.main_quit }
window.border_width= ( 10 )
hbox = Gtk::HBox.new( false, 0 )

# Create the tooltips object
tooltips = Gtk::Tooltips.new

# Create the first button
button = Gtk::Button.new( "Button 1" )

# Connect the first tip to button 1
tooltips.set_tip( button, "This is button 1", nil )
hbox.pack_start( button, false, false, 0 )

# Do it again
button = Gtk::Button.new( "Button 2" )
tooltips.set_tip( button, "And this, is button 2", nil )
hbox.pack_start( button, false, false, 0 )

window.add( hbox )
window.show_all
Gtk.main

Prev Next