How to create a hyperlink with a Label in Tkinter?

Posted on

Question :

How to create a hyperlink with a Label in Tkinter?

How do you create a hyperlink using a Label in Tkinter?

A quick search did not reveal how to do this. Instead there were only solutions to create a hyperlink in a Text widget.

Answer #1:

Bind the label to "<Button-1>" event. When it is raised the callback is executed resulting in a new page opening in your default browser.

from tkinter import *
import webbrowser

def callback(url):

root = Tk()
link1 = Label(root, text="Google Hyperlink", fg="blue", cursor="hand2")
link1.bind("<Button-1>", lambda e: callback(""))

link2 = Label(root, text="Ecosia Hyperlink", fg="blue", cursor="hand2")
link2.bind("<Button-1>", lambda e: callback(""))


You can also open files by changing the callback to:

Answered By: James Burke

Answer #2:

Alternatively if you have multiple labels and want the one function for all. That is assuming you have the link as the text

import tkinter as tk
import webbrowser

def callback(event):

root = tk.Tk()
lbl = tk.Label(root, text=r"", fg="blue", cursor="hand2")
lbl.bind("<Button-1>", callback)
Answered By: Steven Summers

Answer #3:

There is a module on PyPi called tkhtmlview (pip install tkhtmlview) that supports HTML in tkinter. It only supports some tags, but on the page, it says that it has full support fro tags (anchor tags for hyperlinks), and supports the href attribute. It requires Python 3.4 or later with tcl/tk (tkinter) support and the Pillow 5.3.0 module. I haven’t tried the tag yet, but I tried the module in general and it works.

As an example:

import tkinter as tk
from tkhtmlview import HTMLLabel

root = tk.Tk()
html_label=HTMLLabel(root, html='<a href=""> Google Hyperlink </a>')
Answered By: Xbox One

Answer #4:

you can create a class that inherits from label widget of tkinter module , initialize it with additional values including the link of course …
then you create an instance of it as shown below.

import tkinter as t
import webbrowser

class Link(t.Label):
    def __init__(self, master=None, link=None, fg='grey', font=('Arial', 10), *args, **kwargs):
        super().__init__(master, *args, **kwargs)
        self.master = master
        self.default_color = fg # keeping track of the default color 
        self.color = 'blue'   # the color of the link after hovering over it 
        self.default_font = font    # keeping track of the default font = link 

        """ setting the fonts as assigned by the user or by the init function  """
        self['fg'] = fg
        self['font'] = font 

        """ Assigning the events to private functions of the class """

        self.bind('<Enter>', self._mouse_on)    # hovering over 
        self.bind('<Leave>', self._mouse_out)   # away from the link
        self.bind('<Button-1>', self._callback) # clicking the link

    def _mouse_on(self, *args):
            if mouse on the link then we must give it the blue color and an 
            underline font to look like a normal link
        self['fg'] = self.color
        self['font'] = self.default_font + ('underline', )

    def _mouse_out(self, *args):
            if mouse goes away from our link we must reassign 
            the default color and font we kept track of   
        self['fg'] = self.default_color
        self['font'] = self.default_font

    def _callback(self, *args):

root = t.Tk()
root.title('Tkinter Links !')

URL = ''

link = Link(root, URL, font=('sans-serif', 20), text='Python', bg='LightBlue')

Answered By: Ali Zaraket

Leave a Reply

Your email address will not be published. Required fields are marked *