Marking hyperlinks in a Text widget

Cliff Crawford cjc26 at nospam.cornell.edu
Wed May 31 23:12:35 EDT 2000


* André Dahlqvist <andre at beta.telenordia.se> menulis:
| > def click(event):
| >     os.system("netscape -remote 'openURL(%s)'" % \
| >                textw.tag_names('@%d,%d' % (event.x, event.y))[1])
| 
| Is openURL a function that you have written? If so, can I check it out?

That's actually a command-line argument of Unix versions of Netscape.  I
don't know if it would work on Windows.  If you're using Unix, try
typing the following at a shell prompt (with Netscape already running):

% netscape -remote 'openURL(www.python.org)'

It should make Netscape go to the Python page.


| If I understand the code above correctly textw.tag_names() in this
| case returns the name of the tag associated with the text positioned 
| where the mouse is. But how do you actually get the URL?

It actually returns the entire tuple we stored as its tag, not just the
first tag name.  Since the url was already entered as the second element
of this tuple, we just do 

textw.tag_names(...)[1]

to retrieve it.


| I mean to 
| use textw.get(...) you need to know the index of the first and last 
| character in the URL we want, right?

Nope; you just need to know the coordinate of one of the characters
within the url.  The character closest to where the mouse was clicked is
the most convenient one to use :)


| tag_ranges seam interesting. With the help of that one I can get the
| positions of all the words tagged with 'url'. What I don't understand is
| how to know which of these was just clicked.

The callback function takes an argument (event), and you can get the x
and y positions where the mouse was clicked using event.x and event.y.
So if you want to use tag_ranges, you could just look for the range which
contains (event.x, event.y).


-- 
cliff crawford    -><-    http://www.people.cornell.edu/pages/cjc26/
                          Synaesthesia now!            icq 68165166



More information about the Python-list mailing list