[pygtk] Hyperlinks

Brian dol-sen at telus.net
Sat Dec 16 13:41:36 WST 2006

On Sat, 2006-16-12 at 02:34 +0000, Peter Morgan wrote:
> Been searching high and low and cant find a way to make an "url" in pygtk.
> My application has a setting to a web page.. all I want is something 
> like a label that can be clicked on which launches a browser.
> Can anyone point me in the right direction pls
> pete

This is part of some info added to a gtkTextView.

View the entire module here:

some code chunks from our app:

        table = create(
            {'name': ({'weight': pango.WEIGHT_BOLD,
                       'scale': pango.SCALE_X_LARGE,
                       'pixels-above-lines': 5}),
             'description': ({"style": pango.STYLE_ITALIC}),
             'url': ({'foreground': 'blue'}),
             'property': ({'weight': pango.WEIGHT_BOLD}),
             'value': ({}),
             'useset': ({'foreground':'darkgreen'}),
             'masked': ({"style": pango.STYLE_ITALIC}),
        return table

    def on_url_event(self, tag, widget, event, iter):
        """ Catch when the user clicks the URL """
        if event.type == gtk.gdk.BUTTON_RELEASE:

    def on_mouse_motion(self, widget, event, data = None):
        # we need to call get_pointer, or we won't get any more events
        pointer = self.window.get_pointer()
        x, y, spam = self.window.get_pointer()
        x, y = self.window_to_buffer_coords(gtk.TEXT_WINDOW_TEXT, x, y)
        tags = self.get_iter_at_location(x, y).get_tags()
        if self.underlined_url:
            self.underlined_url = None
        for tag in tags:
            if tag in self.url_tags:
                self.underlined_url = tag
        if self.reset_cursor: # defaults to gtk.gdk.XTERM - reset it to None
            self.reset_cursor = False
        return False

        def append_url(text, url, colour):
            """ Append URL to textbuffer and connect an event """
            tag = self.buffer.create_tag(url)
            tag.set_property("foreground", colour)
            tag.connect("event", self.on_url_event)
            append(text, tag.get_property("name"))

Brian <dol-sen at telus.net>

More information about the pygtk mailing list