Advertisement
Guest User

Tkinter Hyperlink Manager Module

a guest
Aug 3rd, 2010
1,201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.98 KB | None | 0 0
  1. from Tkinter import *
  2.  
  3. class HyperlinkManager:
  4.  
  5.     def __init__(self, text):
  6.  
  7.         self.text = text
  8.  
  9.         self.text.tag_config("hyper", foreground="blue", underline=1)
  10.  
  11.         self.text.tag_bind("hyper", "<Enter>", self._enter)
  12.         self.text.tag_bind("hyper", "<Leave>", self._leave)
  13.         self.text.tag_bind("hyper", "<Button-1>", self._click)
  14.  
  15.         self.reset()
  16.  
  17.     def reset(self):
  18.         self.links = {}
  19.  
  20.     def add(self, action):
  21.         # add an action to the manager.  returns tags to use in
  22.         # associated text widget
  23.         tag = "hyper-%d" % len(self.links)
  24.         self.links[tag] = action
  25.         return "hyper", tag
  26.  
  27.     def _enter(self, event):
  28.         self.text.config(cursor="hand2")
  29.  
  30.     def _leave(self, event):
  31.         self.text.config(cursor="")
  32.  
  33.     def _click(self, event):
  34.         for tag in self.text.tag_names(CURRENT):
  35.             if tag[:6] == "hyper-":
  36.                 self.links[tag]()
  37.                 return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement