473,739 Members | 7,416 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 5099
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
4374
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
1083
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
3123
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
1101
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
1104
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
2513
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
5710
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
2700
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
8969
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
8794
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
9341
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
9269
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
9211
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6756
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6056
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3282
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2195
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.