Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to create hyperlink in a Tkinter Text widget?
Tkinter Text widgets are generally used for accepting multiline user input in the given text field. For a particular text document, the content may contain hyperlinks too which is useful in case we want to redirect the user. You can create hyperlinks within the text widget by using HyperLinkManager snippet in Python.
The HyperLinkManager code snippet adds the hyperlink on the keyword within the text widget and handles click events to open URLs in the default web browser.
HyperlinkManager Class
First, we need to create the HyperlinkManager class that handles hyperlink functionality ?
import tkinter as tk
import webbrowser
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):
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(tk.CURRENT):
if tag[:6] == "hyper-":
self.links[tag]()
return
Creating Hyperlinks in Text Widget
Here's how to use the HyperlinkManager to create clickable hyperlinks ?
import tkinter as tk
import webbrowser
from functools import partial
# HyperlinkManager class (same as above)
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):
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(tk.CURRENT):
if tag[:6] == "hyper-":
self.links[tag]()
return
# Create the main window
root = tk.Tk()
root.title("Hyperlink Example")
root.geometry("500x300")
# Create Text widget
text_widget = tk.Text(root, wrap=tk.WORD, font=("Arial", 12))
text_widget.pack(expand=True, fill="both", padx=10, pady=10)
# Create HyperlinkManager
hyperlink = HyperlinkManager(text_widget)
# Add text with hyperlinks
text_widget.insert(tk.END, "Welcome to ")
text_widget.insert(tk.END, "TutorialsPoint",
hyperlink.add(partial(webbrowser.open, "https://www.tutorialspoint.com")))
text_widget.insert(tk.END, " - your learning destination.\n\n")
text_widget.insert(tk.END, "Visit ")
text_widget.insert(tk.END, "Python.org",
hyperlink.add(partial(webbrowser.open, "https://www.python.org")))
text_widget.insert(tk.END, " for more Python resources.")
root.mainloop()
How It Works
The HyperlinkManager class works by:
- Tag Configuration: Creates a "hyper" tag with blue color and underline styling
- Event Binding: Binds mouse events (Enter, Leave, Click) to hyperlink behavior
- Cursor Changes: Changes cursor to hand pointer when hovering over links
- Click Handler: Opens URLs in the default web browser when clicked
Multiple Hyperlinks Example
You can add multiple hyperlinks within the same text widget ?
import tkinter as tk
import webbrowser
from functools import partial
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):
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(tk.CURRENT):
if tag[:6] == "hyper-":
self.links[tag]()
return
root = tk.Tk()
root.title("Multiple Hyperlinks")
root.geometry("600x400")
text_widget = tk.Text(root, wrap=tk.WORD, font=("Arial", 11))
text_widget.pack(expand=True, fill="both", padx=20, pady=20)
hyperlink = HyperlinkManager(text_widget)
# Add multiple hyperlinks
links_data = [
("GitHub", "https://github.com"),
("Stack Overflow", "https://stackoverflow.com"),
("Python Docs", "https://docs.python.org"),
("TutorialsPoint", "https://www.tutorialspoint.com")
]
text_widget.insert(tk.END, "Useful Programming Resources:\n\n")
for i, (name, url) in enumerate(links_data, 1):
text_widget.insert(tk.END, f"{i}. ")
text_widget.insert(tk.END, name, hyperlink.add(partial(webbrowser.open, url)))
text_widget.insert(tk.END, f" - {url}\n")
root.mainloop()
Conclusion
The HyperlinkManager class provides an easy way to add clickable hyperlinks to Tkinter Text widgets. It handles styling, cursor changes, and URL opening automatically when links are clicked.
