Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clickable Tkinter labels

Tags:

python

tkinter

I will be populating a frame with a list of labels representing URLs. The url's will be fed in from a list and can be between 3 and 5 in number, decided by user. What is the easiest way to make these url's clickable, so the user can get to the website displayed? Is there a better way to do this than use labels?

Thanks

like image 944
SquidsEnMasse Avatar asked Jul 16 '12 12:07

SquidsEnMasse


People also ask

How to make labels clickable in Tkinter?

Label widgets in Tkinter are used to display text and images. 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.

What does mainloop() do?

window.mainloop() tells Python to run the Tkinter event loop. This method listens for events, such as button clicks or keypresses, and blocks any code that comes after it from running until you close the window where you called the method.

How Tkinter mainloop works?

mainloop() is simply a method in the main window that executes what we wish to execute in an application (lets Tkinter to start running the application). As the name implies it will loop forever until the user exits the window or waits for any events from the user.

What is label in Tkinter?

The Label is used to specify the container box where we can place the text or images. This widget is used to provide the message to the user about other widgets used in the python application.


1 Answers

Labels are fine I think. You just need to bind a callback to a mouse click.

def open_url(url):
    pass #Open the url in a browser

for i,url in enumerate(url_list):
    label=tk.Label(frame,text=url)
    label.grid(row=i)
    label.bind("<Button-1>",lambda e,url=url:open_url(url))
like image 179
mgilson Avatar answered Sep 22 '22 11:09

mgilson