Creating Clickable Tkinter labels

TkinterPythonGUI-Programming

Tkinter Label widgets provide a way to display text and images in the Tkinter application window. We can configure the properties of the label widget by defining the attributes and properties in it. The Label widget can be converted into a clickable object by defining a function that contains some operation which can later be bound with a key.

Example

In this example, we will create a Label widget. Clicking over the Label widget will redirect the user to a specified webpage. So, the label will act as a hyperlink.

#Import the required libraries
from tkinter import *
import webbrowser

#Create an instance of tkinter frame
win = Tk()
win.geometry("750x250")

#Define a callback function
def callback(url):
   webbrowser.open_new_tab(url)

#Create a Label to display the link
link = Label(win, text="www.tutorialspoint.com",font=('Helvetica', 15), fg="blue", cursor="hand2")
link.pack()
link.bind("<Button-1>", lambda e: callback("http://www.tutorialspoint.com"))
win.mainloop()

Output

Running the above code will display a window containing a Label widget. Clicking over the label will redirect the user to the website "www.tutorialspoint.com"

raja
Published on 26-May-2021 11:52:15
Advertisements