Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Added code from an external source which allows tkinter to handle hyp…
…erlinks properly within the text widget + a test file for this
- Loading branch information
Showing
2 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import tkHyperlinkManager | ||
import webbrowser | ||
from tkinter import * | ||
|
||
root = Tk() | ||
root.title("hyperlink-1") | ||
|
||
text = Text(root) | ||
text.pack() | ||
|
||
hyperlink = tkHyperlinkManager.HyperlinkManager(text) | ||
|
||
def click1(): | ||
print ("click 1") | ||
url = "https://www.google.co.uk" | ||
webbrowser.open(url, new=2) | ||
|
||
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") | ||
|
||
mainloop() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
######################################################################### | ||
# External code to allow insertion of hyperlinks in tkinter text widget # | ||
# Creator: Fredik Lundh # | ||
# Source: http://effbot.org/zone/tkinter-text-hyperlink.htm # | ||
######################################################################### | ||
|
||
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) | ||
|
||
self.reset() | ||
|
||
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): | ||
self.text.config(cursor="hand2") | ||
|
||
def _leave(self, event): | ||
self.text.config(cursor="") | ||
|
||
def _click(self, event): | ||
for tag in self.text.tag_names(CURRENT): | ||
if tag[:6] == "hyper-": | ||
self.links[tag]() | ||
return |