Programmatic links in a TKinter TextBox
Jeff Epler
jepler at unpythonic.net
Wed Jun 15 12:37:00 EDT 2005
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()
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20050615/e12ea5fd/attachment.sig>
More information about the Python-list
mailing list