По мотивам вот этого ответа Python TKinter get clicked tag in text widget
import re
import webbrowser
from tkinter import *
from tkinter import scrolledtext
def click(event):
# get the index of the mouse click
index = event.widget.index("@%s,%s" % (event.x, event.y))
# get the indices of all "link" tags
tag_indices = list(event.widget.tag_ranges('link'))
# iterate them pairwise (start and end index)
for start, end in zip(tag_indices[0::2], tag_indices[1::2]):
# check if the tag matches the mouse click index
if event.widget.compare(start, '<=', index) and event.widget.compare(index, '<', end):
# take string between tag start and end
webbrowser.open_new_tab(event.widget.get(start, end))
window = Tk()
txtscroll = scrolledtext.ScrolledText(window, width=20, height=20)
txtscroll.insert(INSERT, 'текст со ссылками')
txtscroll.grid(row=0, column=0, sticky=W + NS + E, padx=(5, 5), pady=(5, 5))
# привязываем регулярное выражение к событию нажатия на ссылку
txtscroll.tag_config("link", foreground="blue", underline=1)
regex = r'(https?://\S+)'
for match in re.finditer(regex, txtscroll.get("1.0", END)):
start = txtscroll.index("1.0 + %d chars" % match.start())
end = txtscroll.index("1.0 + %d chars" % match.end())
txtscroll.tag_add("link", start, end)
txtscroll.tag_bind("link", "<Button-1>", click)
window.mainloop()