473,405 Members | 2,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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 5034
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Cameron Laird | last post by:
QOTW: "Well, to do what I've done so far in Java would have taken at least 5 and probably more like 10 times the code I've written. A simple yet decent TCP chat server in about 30 lines of code...
7
by: Justin Ezequiel | last post by:
What font is Tkinter using for displaying utf-8 characters? On my Windows XP, most of the characters with names (unicodedata.name) are displayed WYSIWYG. However, on my Mandrake (warning: Linux...
0
by: Cameron Laird | last post by:
QOTW: "I've forgotten what we are arguing about, but I'm sure I'm right." -- Jive Dadson "I believe the best strategy against Identity theft is bad credit." -- Tom Willis "You can't live...
3
by: Jo Schambach | last post by:
I am trying to write a GUI with tkinter that displays the stdout from a regular C/C++ program in a text widget. The idea i was trying to use was as follows: 1) use "popen" to execute the C/C++...
2
by: matt.delvecchio | last post by:
hello, i have a user control that works great, when its used as a design-time controls. however, when i try to use it as a programmtic control, im running into troubles. the control is pretty...
1
by: Graham | last post by:
Hi, I am trying to write a custom email form control that doesn't require programming experiance in order to implement (for our html guys). I have a base design that I would like to use for the...
6
by: Adam | last post by:
Hey, I'm pretty new to programming. Been trying to learn using Python. The code I'm struggling with is for my GUI. I'm am having trouble getting this to display the way I want with the grid...
3
by: Gigs_ | last post by:
how to write text on canvas. i know that i need to use canvas.create_text, but how to write text than when i create_text? or how to access object ID in canvas and change some options? thanks...
2
by: alivip | last post by:
when I wont to inser (anyting I print) to the textbox it will not inser it just print then hanging # a look at the Tkinter Text widget # use ctrl+c to copy, ctrl+x to cut selected text, #...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.