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
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.
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.
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.
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.
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))
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