0

The following loop unintentionally associates all the links with the last link found in brand_url[i], how can this be corrected?

for i in databrand.index:
    brand_name = databrand['Brands'][i]
    brand_url = databrand['Url'][i]
    link_text = brand_name + " " + brand_url
    my_link = tk.Label(right_frame, text= link_text,
                       fg="blue", cursor="hand2", font=['Times', 22, 'underline'])
    my_link.pack()
    my_link.bind("<Button-1>",
                 lambda e: webbrowser.open_new(brand_url))

    text = tk.Label(right_frame, text=databrand['Brands'][i], font=['Times', 22, 'bold'])

    text.pack()
    print(databrand['Brands'][i])
1
  • It sounds like you need to change the lambda to be like this: lambda e,url=brand_url: webbrowser.open_new(url) Commented Sep 28, 2023 at 19:57

1 Answer 1

1

Due to how variables are bound in Python, you'll need one more level of indirection to bind the lambda's url to a local name that doesn't change:

def make_opener(url):
   return lambda e: webbrowser.open_new(url)

# ...

my_link.bind("<Button-1>", make_opener(url))
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.