[pygtk] Treeview Tooltips

Frederic Back fredericback at gmail.com
Fri May 5 16:36:45 WST 2006


On Thu, 2006-05-04 at 09:54 -0400, Stephen Langer wrote:
> Here's an e-mail exchange I had about adding tooltips to menus.   
> Since menus use treeviews underneath, I imagine the same hack should  
> work.  (The original e-mails don't seem to be in the archive...)
> 
> Please note that the code here is due to Phil Dumont, not me.   The  
> only substantial change I made to get it to work in my context was to  
> put a try/except around the line "cv_parent = cell_view.get_parent 
> ()".  If you're using arrow keys to navigate a menu, sometimes  
> cell_view is a TreeViewColumn and doesn't have a get_parent method.
> 
>   --  Steve

Hi Steve,

Thank you for the code. The solution did not work well for me (The
exploit seems to work only on menues), so I tried another approach:
I basically just track mouse movement, get the row and column the cursor
is pointing at, and change the tooltip accordingly. I added my code
below this email's body.

Thank you,
Fred

############# The sample code: #############
import gtk, gobject


def treeViewTooltip( widget, e, tooltips, cell, emptyText="no
information" ):
    """ 
    If emptyText is None, the cursor has to enter widget from a side
    that contains an item, otherwise no tooltip will be displayed. """

    try:
        (path,col,x,y) = widget.get_path_at_pos( int(e.x), int(e.y) ) 
        it = widget.get_model().get_iter(path)
        value = widget.get_model().get_value(it,cell)
        tooltips.set_tip(widget, value)
        tooltips.enable()
    except:
        tooltips.set_tip(widget, emptyText)

# create a tree model
mod = gtk.ListStore(str, str)
mod.append( ("fruit A", "Banana") )
mod.append( ("fruit B", "Apple") )
mod.append( ("fruit C", "Cherry") )
mod.append( ("fruit D", "Melon") )

# create an absolutely normal treeview
treeview = gtk.TreeView(mod)
col = gtk.TreeViewColumn()
treeview.append_column(col)
cr=gtk.CellRendererText()
col.pack_start(cr)
col.add_attribute(cr,"text",0)

# assign the tooltip
tips = gtk.Tooltips()
tips.set_tip(treeview, "")
treeview.connect("motion-notify-event",treeViewTooltip, tips, 1) # <---
treeview.set_events( gtk.gdk.POINTER_MOTION_MASK )

w=gtk.Window()
w.connect_after('delete-event', lambda *args: gtk.main_quit())
w.add(treeview)
w.set_size_request(200,-1)
w.show_all()
gtk.main()



More information about the pygtk mailing list