473,782 Members | 2,525 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="blu e", underline=1)
text.tag_bind(" a", "<Enter>", show_hand_curso r)
text.tag_bind(" a", "<Leave>", show_arrow_curs or)
text.tag_bind(" a", "<Button-1>", click)
text.config(cur sor="arrow")

#add text
text.insert(INS ERT, "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.Even t 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 5103
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(ap p)
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("h ref:"):
print "clicked href %s" % t[5:]
break
else:
print "clicked without href"
return "break"

def show_hand_curso r(event):
event.widget.co nfigure(cursor= "hand1")
def show_arrow_curs or(event):
event.widget.co nfigure(cursor= "")

# configure text tag
text.tag_config ("a", foreground="blu e", underline=1)
text.tag_bind(" a", "<Enter>", show_hand_curso r)
text.tag_bind(" a", "<Leave>", show_arrow_curs or)
text.tag_bind(" a", "<Button-1>", click)
text.config(cur sor="arrow")

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

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

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

iD8DBQFCsFkoJd0 1MZaTXX0RAn/+AJ95Ne78zTwUFu 2i0w4njg6rbnrZb ACgq648
YAV3lPfsQP7j7wk n3Lost4M=
=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(Tki nter.END, "link", ("a", "href:"+href,"a nother: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("h ref:"):
print "clicked href %s" % t[5:]
#commented out the break
elif t.startswith("a nother:"):
print "clicked href %s" % t[8:]
# commented out the break
elif t.startswith("l ast:"):
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
1446
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 is something I never considered possible before looking at Python." -- Qp http://groups.google.com/groups?selm=k4y0c.7173%24ED1.3250%40fe03.usenetserver.com "Python is built on a small set of entirely uninteresting control structures, designed...
7
4381
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 newbie), most characters are displayed as \u???? where ???? is the Hex code. ## start of script Tkinter_gui.py---------- import Tkinter import ScrolledText
0
1085
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 without unit tests. And once you have unit tests, the added value of declarations is tiny, and their cost remains." -- martellibot
3
3124
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++ program 2) then use "tkinter.createfilehandler" to create a callback that would be called when the C/C++ program creates output on stdout. Somehow, I can't get this to work. here is what I have tried so far:
2
1103
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 simple: it has a textbox, and a grid. in my webform, on button_click i call one of its methods to do stuff (like bind data and set a value to the textbox), and it works great.
1
1105
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 markup but I am having trouble getting it working. <ec:intelleform runat="server" id="intelleform1" SubmitButtonId="FormSubmit" Macro="$" SmtpServer="<%$ AppSettings:SMTPServer %>"> <FormTemplate>
6
2514
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 manager. Can anybody tell me what I am doing wrong? I hope you can tell what look I'm trying to achieve from the code as its hard to explain.
3
5730
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 in advance!
2
2703
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, # ctrl+v to paste, and ctrl+/ to select all # count words in a text and show the first ten items # by decreasing frequency
0
9641
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10313
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10146
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10080
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2875
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.