Re: Text tags and hypertext
Guido.van.Rossum@cwi.nl
Tue, 10 Jan 1995 17:26:25 +0100
> I have been trying to set up a kind of small X11 hypercard.
> To do this I want to have text widgets with words tagged so
> that when the user releases the first button on that word, some
> action is invoked.
>
> The docs says that this is possible, but provide no examples.
> None of the demos have any real examples either. So I have
> been trying unsuccessfully to do this as follows,
>
> def do_click():
> print 'got a click'
>
> w_out = ScrolledText( ... )
> w_out.insert('end', "This ")
> here = w_out.index('end')
> w_out.insert('end', "is")
> w_out.tag_add( {'<ButtonRelease-1>': do_click}, here, 'end')
> w_out.insert('end', " a link")
>
> This is supposed to set up a document with the words "This is a
> link" in it, with the word "is" being "active". Now either my tag
> construction is wrong, or I need to apply it over a range of
> characters, or both.
>
> Anyone know what I am doing wrong?
Yes. Looking back over your code:
> w_out.tag_add( {'<ButtonRelease-1>': do_click}, here, 'end')
This line is probably bad. Where did you get the idea that this
syntax was allowed? Apparently the code doesn't detect the error, but
you have defined a tag name that's equivalent to str({...}).
The tag_add() call is only tags a range of characters with a tag name.
You can then call tag_bind() to bind an event callback to the
characters tagged with a given tag name.
What I suggest:
tagname = 'spam'
w_out.tag_add(tag_name, here, 'end')
w_out.tag_bind(tag_name, '<ButtonRelease-1>', do_click)
but note that if you tag many sequences with the same tag name, you
should only call tag_bind once (for that tag name).
--Guido van Rossum, CWI, Amsterdam <mailto:Guido.van.Rossum@cwi.nl>
<http://www.cwi.nl/cwi/people/Guido.van.Rossum.html>