Search code examples
hyperlinkpygtk

Hyperlink in CellRendererText Markup


I want to display a hyperlink in a pygtk table:

cr=gtk.CellRendererText()
column=gtk.TreeViewColumn(name)
column.add_attribute(cr, "markup", 0)

my_liststore=['<a href="http://google.com/">google</a>', ...]

Hyperlink "a" seems not supported by the markup. I get this warning:

GtkWarning: Failed to set text from markup due to error parsing markup: Unknown tag 'a' on line 1

How can I display a hyperlink in a pygtk table? And of course it should open the browser if you click on it ...

Update

Several month after asking this question: Here is my personal advice: don't use gtk. It is a dead horse. I don't know if Qt is better. The way to go is web technology.


Solution

  • Here are the lines which I use now. The cell gets rendered with blue color and underlined. The double click event calls a callback with uses the webbrowser module.

    table = gtk.TreeView(list_store)
    
    cr = gtk.CellRendererText()
    
    # allow pango markup
    column.add_attribute(cr, "markup", i) 
    
    # connect double click handler:
    self.timeline.connect('row-activated', self.on_treeview_click)
    
    # content in the data rows:
    u'<span foreground="blue" underline="single">%s</span>' % (
                        glib.markup_escape_text(name))
    

    Callback:

        def on_treeview_click(self, treeview, path, view_column):
            model=treeview.get_model()
            action_id=model[path][0]
            url='....' # build your url
            import webbrowser
            webbrowser.open(url)