Tkinter Text Widget Hyperlink Manager

October 25, 2000 | Fredrik Lundh

The tkHyperlinkManager module is a simple container for Text widget hyperlinks. Just create a manager for your text widget, and use the add method to register actions.

    text = Text(...)
    hyperlink = HyperlinkManager(text)
    text.insert(INSERT, "this is a link", hyperlink.add(callback))

Here’s the code:

Support module for Text hyperlinks (File:
from Tkinter import *

class HyperlinkManager:

    def __init__(self, text):

        self.text = text

        self.text.tag_config("hyper", foreground="blue", underline=1)

        self.text.tag_bind("hyper", "<Enter>", self._enter)
        self.text.tag_bind("hyper", "<Leave>", self._leave)
        self.text.tag_bind("hyper", "<Button-1>", self._click)


    def reset(self):
        self.links = {}

    def add(self, action):
        # add an action to the manager.  returns tags to use in
        # associated text widget
        tag = "hyper-%d" % len(self.links)
        self.links[tag] = action
        return "hyper", tag

    def _enter(self, event):

    def _leave(self, event):

    def _click(self, event):
        for tag in self.text.tag_names(CURRENT):
            if tag[:6] == "hyper-":

And here’s an example:

# File:

import tkHyperlinkManager

from Tkinter import *

root = Tk()

text = Text(root)

hyperlink = tkHyperlinkManager.HyperlinkManager(text)

def click1():
    print "click 1"

text.insert(INSERT, "this is a ")
text.insert(INSERT, "link", hyperlink.add(click1))
text.insert(INSERT, "\n\n")

def click2():
    print "click 2"

text.insert(INSERT, "this is another ")
text.insert(INSERT, "link", hyperlink.add(click2))
text.insert(INSERT, "\n\n")



A Django site. rendered by a django application. hosted by webfaction.