0

I have the following code but when no matter what label i click it always opens "bing.com"(last link in tuple). How can i open different links based on the label i click?

from tkinter import *
import webbrowser
root = Tk()

def callback(url):
    webbrowser.open_new(url)

qtextnlinks = (('apple', 'google.com'), ('orange', 'bing.com'))
for label_text, url in qtextnlinks:
    print(label_text,url)
    label_p = Label(root, text=label_text, fg="blue", cursor="hand2")
    label_p.pack()
    label_p.bind("<Button-1>", lambda e: callback(url))

root.mainloop()

1 Answers1

0

You just have to assign the 'url' value to a new param before using it inside the lambda 'callback' function:

label_p.bind("<Button-1>", lambda e, _url=url: callback(_url))
Ze'ev Ben-Tsvi
  • 1,174
  • 1
  • 3
  • 7