[pygtk] Can't get tool tip to display

Dieter Verfaillie dieterv at optionexplicit.be
Fri Dec 3 17:25:53 WST 2010


Quoting "Gerald Britton" <gerald.britton at gmail.com>:
> Hi -- I have a treeview where I want tooltips to display upon
> mouse-hover.  I need help to see what I'm missing in this section
> since the tips are not popping up:  ("columns" is a simple list of
> column names)
>
>         self.view = gtk.TreeView()
>         self.model = gtk.ListStore(*[str] * (len(columns) + 1))
>         for index, name in enumerate(columns):
>             renderer = gtk.CellRendererText()
>             if index > 0:
>                 renderer.set_property('editable', True)
>                 renderer.connect('edited', self.__cell_edited,
>                                                 (self.model, index + 1))
>             column = gtk.TreeViewColumn(name, renderer, text=index + 1)
>             foo = gtk.Tooltip()
>             foo.text = name
>             self.view.append_column(column)
>             self.view.set_tooltip_cell(foo, None, column, None)
>

Here's a working example:

8<8<8<8<8<8<8<8<8<8<8<8<8<8<8<8<8<8<8<8<8<8<8<8<8<8<8<8<8<8<
import gtk

class TestWindow(gtk.Window):
     def __init__(self):
         gtk.Window.__init__(self)

         column_types = [str, str, str]
         columns = ['index', 'something', 'whatever']

         self.model = gtk.ListStore(*column_types)
         self.view = gtk.TreeView(self.model)
         self.view.set_headers_visible(True)
         self.view.set_has_tooltip(True)
         self.view.connect('query-tooltip', self.on_query_tooltip)

         for index, name in enumerate(columns):
             renderer = gtk.CellRendererText()

             if index > 0:
                 renderer.set_property('editable', True)

             column = gtk.TreeViewColumn(name, renderer, text=index)
             self.view.append_column(column)

         self.model.append(['0', 'test', 'dfjqkm'])
         self.model.append(['1', 'test', 'arearzer'])

         self.add(self.view)
         self.show_all()

     def on_query_tooltip(self, widget, x, y, keyboard_tip, tooltip):
         if not widget.get_tooltip_context(x, y, keyboard_tip):
             return False
         else:
             model, path, iter = widget.get_tooltip_context(x, y, keyboard_tip)

             tooltip.set_markup('<b>Value: %s</b>' % model.get_value(iter, 2))
             widget.set_tooltip_cell(tooltip, path, None, None)
             return True


def quit(widget, event):
     gtk.main_quit()


if __name__ == '__main__':
     window = TestWindow()
     window.connect('delete-event', quit)

     gtk.main()
8<8<8<8<8<8<8<8<8<8<8<8<8<8<8<8<8<8<8<8<8<8<8<8<8<8<8<8<8<8<

----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.


More information about the pygtk mailing list