[pygtk] Hyperlinking text

John Finlay finlay at moeraki.com
Tue Mar 1 08:35:49 WST 2005


Saurabh Wagh wrote:

>Hey there...
>
>I posted this question once, but the suggestions that i got didnt
>quite work out, so i guess , i wasnt able to make the problem
>understood in the first place. So i reframe the question ...
>
>Basically, i want to make some text clickable ( Please note that it
>isnt some predefined static text.. so.. i cant make buttons out of
>it.. )
>
>The text in my case, is a list of names of objects ( which i display
>on the interface after getting them as a result of a query to the
>server ) ..
>
>Now, i want each name to be clickable ( so that on clicking that, i
>can send a query for info of that object to server n display content
>which i get in response )
>
>Currently, the list of names that i display is using a text buffer
>which displays the names of objects (text) .. will i be able to make
>text displayed in such a text area clickable ?? if yes .. how ??
>
>  
>
Create a tag that is used to tag each of the clickable names. Then 
connect a handler to the "event" signal of the tag. See the following 
for more info:
http://www.pygtk.org/pygtk2reference/class-gtktexttag.html#signal-gtktexttag--event
on the event handler signature.

For example:

def hyperlink_handler(tag, textview, event, iter):
if event.type == gtk.gdk.BUTTON_RELEASE:
#figure out which hyperlink name was clicked and query
...

buffer = textview.get_buffer()
tag = buffer.create_tag("hyperlink", foreground='blue')
tag.connect('event', hyperlink_handler)
iter = b.get_iter_at_mark(b.get_insert())
b.insert_with_tags(iter, name, tag)

The testtext.py example in the PyGTK tutorial has an example of handling 
tag events:
http://www.pygtk.org/pygtk2tutorial/sec-TextViewExample.html

John



More information about the pygtk mailing list