- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create a hyperlink with a Label in Tkinter?
Tkinter Label widgets are generally used to display text or images. In this example, we will see how to add a hyperlink on a Label widget in an application.
In order to add a hyperlink, we can bind the label text with a button that makes it clickable. The open_new(url) method is used to define the function that opens a web browser to follow the link. The open_new(url) method is defined in the webbrowser module in Python which can be imported in the notebook using 'import webbrowser'.
Example
#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=('Helveticabold', 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 Label text with a URL.
The displayed window will show a hyperlink which upon clicking will redirect the user to the website: www.tutorialspoint.com
Advertisements