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.
Build A Paint Program With TKinter and Python We can link a URL with the label widget to make it clickable. Whenever the label widget is clicked, it will open the attached link in the default browser. To work with the browser and hyperlinks we can use webbrowser module in Python.
The Label widget in tkinter is generally used to display text as well as image. Text can be added in a Label widget by using the constructor Label(root, text= "this is my text"). Once the Label widget is defined, you can pack the Label widget using any geometry manager.
To display one or more lines of text in a label widget, set this option to a string containing the text. Internal newlines ("\n") will force a line break. To slave the text displayed in a label widget to a control variable of class StringVar, set this option to that variable.
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):
webbrowser.open_new(url)
root = Tk()
link1 = Label(root, text="Google Hyperlink", fg="blue", cursor="hand2")
link1.pack()
link1.bind("<Button-1>", lambda e: callback("http://www.google.com"))
link2 = Label(root, text="Ecosia Hyperlink", fg="blue", cursor="hand2")
link2.pack()
link2.bind("<Button-1>", lambda e: callback("http://www.ecosia.org"))
root.mainloop()
You can also open files by changing the callback to:
webbrowser.open_new(r"file://c:\test\test.csv")
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):
webbrowser.open_new(event.widget.cget("text"))
root = tk.Tk()
lbl = tk.Label(root, text=r"http://www.google.com", fg="blue", cursor="hand2")
lbl.pack()
lbl.bind("<Button-1>", callback)
root.mainloop()
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="http://www.google.com"> Google Hyperlink </a>')
html_label.pack()
root.mainloop()
You can simply import webbrowser, add a function and call that inside of the button. Declare your layout manager. Here is how the code looks like:
from tkinter import *
import webbrowser
# Displaying the root window
window = Tk()
window.config(padx=100, pady=100)
# Function Declaration
def callback():
webbrowser.open_new("https://www.google.com/")
# Button Declaration
your_variable_name = Button(text="button_name", command=callback)
your_variable_name.grid(column=2, row=3)
# keeping the Tkinter Interface running
window.mainloop()
FYI. It'll open in computer's default browser
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With