470,586 Members | 1,379 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,586 developers. It's quick & easy.

Programmatic links in a TKinter TextBox

I've been hacking around this for a few days and have gotten close to
what I want... but not quite...

The TKinter Docs provide this example:
# configure text tag
text.tag_config("a", foreground="blue", underline=1)
text.tag_bind("a", "<Enter>", show_hand_cursor)
text.tag_bind("a", "<Leave>", show_arrow_cursor)
text.tag_bind("a", "<Button-1>", click)
text.config(cursor="arrow")

#add text
text.insert(INSERT, "click here!", "a")

#add a link with data
text.insert(END, "this is a ")
text.insert(END, "link", ("a", "href"+href))

What I don't understand is how the function "click" sees the "href"
text.

def click(self, data):
#this doesn't work
print data

The print statement inside function "click" displays:
<Tkinter.Event instance at 0x01A1FFA8>

If I try to access the link data like this "data[0]" I get an error.

I'm not planning to use web links, I just need each link to send unique
data to the "click" function.
How can I retrieve the custom link data from within my function???

Jul 19 '05 #1
2 4703
Based on the location where the user clicked, you can find the
associated tags. Then you must loop through them to find the one that
gives the "href" value.

Jeff
:r /tmp/link.py

import Tkinter

app = Tkinter.Tk()
text = Tkinter.Text(app)
text.pack()

def click(event):
#this doesn't work
print event
w = event.widget
x, y = event.x, event.y
tags = w.tag_names("@%d,%d" % (x, y))
for t in tags:
if t.startswith("href:"):
print "clicked href %s" % t[5:]
break
else:
print "clicked without href"
return "break"

def show_hand_cursor(event):
event.widget.configure(cursor="hand1")
def show_arrow_cursor(event):
event.widget.configure(cursor="")

# configure text tag
text.tag_config("a", foreground="blue", underline=1)
text.tag_bind("a", "<Enter>", show_hand_cursor)
text.tag_bind("a", "<Leave>", show_arrow_cursor)
text.tag_bind("a", "<Button-1>", click)
text.config(cursor="arrow")

#add text
text.insert(Tkinter.INSERT, "click here!", "a")
text.insert(Tkinter.INSERT, "\n")

#add a link with data
href = "http://www.example.com"
text.insert(Tkinter.END, "this is a ")
text.insert(Tkinter.END, "link", ("a", "href:"+href))
app.mainloop()

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFCsFkoJd01MZaTXX0RAn/+AJ95Ne78zTwUFu2i0w4njg6rbnrZbACgq648
YAV3lPfsQP7j7wkn3Lost4M=
=YlMC
-----END PGP SIGNATURE-----

Jul 19 '05 #2
Many thanks Jeff!!!
This is what should be in the TK docs. Your example was very clear.
And now I've learned some new stuff... :)

For my project, I needed to add three pieces of data per link like
this:
text.insert(Tkinter.END, "link", ("a", "href:"+href,"another:This Is
More Data", "last:and one more bit for fun"))

I tweaked the above example here to catch multiple bits by adding a
couple elif statements to the for loop, and removing the break lines.

def click(event):
w = event.widget
x, y = event.x, event.y
tags = w.tag_names("@%d,%d" % (x, y))
for t in tags:
if t.startswith("href:"):
print "clicked href %s" % t[5:]
#commented out the break
elif t.startswith("another:"):
print "clicked href %s" % t[8:]
# commented out the break
elif t.startswith("last:"):
print "clicked href %s" % t[5:]
else:
print "clicked without href"
return "break"

Thanks again, I hope others will find this as useful as I did..

Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Justin Ezequiel | last post: by
2 posts views Thread by matt.delvecchio | last post: by
1 post views Thread by Graham | last post: by
6 posts views Thread by Adam | last post: by
3 posts views Thread by Gigs_ | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.