[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:
http://porthole.cvs.sourceforge.net/porthole/trunk/packagebook/summary.py?revision=1.3&view=markup


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'}),
             'useunset':({'foreground':'red'}),
             '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:
            load_web_page(tag.get_property("name"))

    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.set_property("underline",pango.UNDERLINE_NONE)
            self.get_window(gtk.TEXT_WINDOW_TEXT).set_cursor(None)
            self.underlined_url = None
        for tag in tags:
            if tag in self.url_tags:
                tag.set_property("underline",pango.UNDERLINE_SINGLE)
                self.get_window(gtk.TEXT_WINDOW_TEXT).set_cursor(gtk.gdk.Cursor
                                                                 (gtk.gdk.HAND2))
                self.underlined_url = tag
        if self.reset_cursor: # defaults to gtk.gdk.XTERM - reset it to None
            self.get_window(gtk.TEXT_WINDOW_TEXT).set_cursor(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)
            self.url_tags.append(tag)
            append(text, tag.get_property("name"))

-- 
Brian <dol-sen at telus.net>



More information about the pygtk mailing list