dumbmatter.com

Online home of Jeremy Scheff

Tooltips when hovering over a PyGTK TreeView column header

If you search for information about showing tooltips in a PyGTK TreeView, most of what you find is about tooltips for hovering over rows. Here, I'll explain how to show a tooltip when you hover over a column header in a PyGTK TreeView.

GTK+ has this nice tooltips class which generally works by doing something like this:

tooltips = gtk.Tooltips()
tooltips.set_tip(widget, 'TOOLTIP TEXT')

Then, when your mouse hovers over the widget, TOOLTIP TEXT will appear in a tooltip. But for TreeView columns, there is a slight complication: what is the widget? Well, you have to do something like this:

tooltips = gtk.Tooltips()
column = gtk.TreeViewColumn()
column_header = gtk.Label('Column Header')
column_header.show()
column.set_widget(column_header)
tooltips.set_tip(column_header, 'TOOLTIP TEXT')

This code is a bit ugly, as normally you would just do column = gtk.TreeViewColumn('Column Header') without making a separate gtk.Label. But, if you do it that way, then (as far as I can tell) there is no way to get the label widget, and thus no way to set a tooltip.

Here is an example that you can run. This is based on the basictreeview.py example from the PyGTK Tutorial.

#!/usr/bin/env python

# example basictreeview.py

import pygtk
pygtk.require('2.0')
import gtk

class BasicTreeViewExample:

    # close the window and quit
    def delete_event(self, widget, event, data=None):
        gtk.main_quit()
        return False

    def __init__(self):
        # Create a new window
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)

        self.window.set_title("Basic TreeView Example")

        self.window.set_size_request(200, 200)

        self.window.connect("delete_event", self.delete_event)

        # create a TreeStore with one string column to use as the model
        self.treestore = gtk.TreeStore(str)

        # we'll add some data now - 4 rows with 3 child rows each
        for parent in range(4):
            piter = self.treestore.append(None, ['parent %i' % parent])
            for child in range(3):
                self.treestore.append(piter, ['child %i of parent %i' %
                                              (child, parent)])

        # create the TreeView using treestore
        self.treeview = gtk.TreeView(self.treestore)

        # create the TreeViewColumn to display the data
        tooltips = gtk.Tooltips()
        self.tvcolumn = gtk.TreeViewColumn()
        column_header = gtk.Label('Column 0')
        column_header.show()
        self.tvcolumn.set_widget(column_header)
        tooltips.set_tip(column_header, 'TOOLTIP TEXT')

        # add tvcolumn to treeview
        self.treeview.append_column(self.tvcolumn)

        # create a CellRendererText to render the data
        self.cell = gtk.CellRendererText()

        # add the cell to the tvcolumn and allow it to expand
        self.tvcolumn.pack_start(self.cell, True)

        # set the cell "text" attribute to column 0 - retrieve text
        # from that column in treestore
        self.tvcolumn.add_attribute(self.cell, 'text', 0)

        # make it searchable
        self.treeview.set_search_column(0)

        # Allow sorting on the column
        self.tvcolumn.set_sort_column_id(0)

        # Allow drag and drop reordering of rows
        self.treeview.set_reorderable(True)

        self.window.add(self.treeview)

        self.window.show_all()

def main():
    gtk.main()

if __name__ == "__main__":
    tvexample = BasicTreeViewExample()
    main()

If you're setting the tooltip in a different place in your code than right where you set the header text (i.e., if the label widget is not accessible), you can use the get_widget method on the column to get the label widget.

2 archived comments »

  1. How about a tutorial showing how to use gtk3 with css, which is the main feature of gtk3.

    Comment by craig — January 7, 2012 @ 6:49 pm

  2. In the meanwhile you can use widget.set_tooltip_text(aString) for setting a tooltip text for all widgets, as it inherits from class gtk.Widget:

    column = gtk.TreeViewColumn()
    column_header = gtk.Label(‘Column Header')
    column_header.set_tooltip_text(‘TOOLTIP TEXT')
    column_header.show()
    column.set_widget(column_header)

    Comment by Chris X. — April 2, 2012 @ 4:52 pm