| 31 | exclude_list = ['demo', 'common'] |
| 32 | |
| 33 | class LinkManager: |
| 34 | def __init__(self, text, url_callback = None): |
| 35 | self.text = text |
| 36 | self.text.tag_config("link", foreground="blue", underline=1) |
| 37 | self.text.tag_bind("link", "<Enter>", self._enter) |
| 38 | self.text.tag_bind("link", "<Leave>", self._leave) |
| 39 | self.text.tag_bind("link", "<Button-1>", self._click) |
| 40 | |
| 41 | self.url_callback = url_callback |
| 42 | self.reset() |
| 43 | |
| 44 | def reset(self): |
| 45 | self.links = {} |
| 46 | def add(self, action): |
| 47 | # add an action to the manager. returns tags to use in |
| 48 | # associated text widget |
| 49 | tag = "link-%d" % len(self.links) |
| 50 | self.links[tag] = action |
| 51 | return "link", tag |
| 52 | |
| 53 | def _enter(self, event): |
| 54 | self.text.config(cursor="hand2") |
| 55 | def _leave(self, event): |
| 56 | self.text.config(cursor="") |
| 57 | def _click(self, event): |
| 58 | for tag in self.text.tag_names(tk.CURRENT): |
| 59 | if tag.startswith("link-"): |
| 60 | proc = self.links[tag] |
| 61 | if callable(proc): |
| 62 | proc() |
| 63 | else: |
| 64 | if self.url_callback: |
| 65 | self.url_callback(proc) |
| 66 | |
| 67 | class App: |
| 68 | def __init__(self): |