0

I am trying to use Tkinter to access different webpages by clicking on different buttons. The problem is that with the way I wrote my code, the different buttons keep accessing the same URL, and I can't figure out why.

Here is how my code looks like, where the websites I want to visit is encapsulated in the websites variable.

from tkinter import *
import webbrowser

websites = [{"Name": "Wikipedia",   "URL": "www.wikipedia.org", },
            {"Name": "Gmail",       "URL": "www.gmail.com", },
            ]

root = Tk()
root.title('Bookmarks')
root.geometry("1400x100")


def open_browser(url):
    webbrowser.open_new_tab(url)


for website in websites:
    button = Button(root, text=website["Name"],
                    font=("Helvetica", 24),
                    command=lambda: open_browser(website["URL"]))
    button.pack(side=LEFT)

root.mainloop()

Anytime I click on Gmail, it takes me to www.gmail.com, which is great. But anytime I click on Wikipedia, it takes me to www.gmail.com as well.

The part of my code I must be misunderstanding is:

command=lambda: open_browser(website["URL"]))

Is there a simple fix to this?

0 Answers0