0

I am trying to write a Tkinter code when a user types a name the URL appears:

I am trying to get the Label which is mySubmit to be clickable and to be opened in a new browser.

I have search several solutions but I didn't was not successful in getting it.

Here is the code:

root = Tk()

e = Entry(root, width=50)
e.pack(padx=50)

def open_url(mySubmit):
    webbrowser.open_new(mySubmit)


mySubmit = Label(root, open_url)

def myClick(event=None):
    global mySubmit
    global reply
    mySubmit["text"] = results()
    if e.get() != "":
        search.append(e.get())
    # print(search)
    e.delete(0,"end")
    mySubmit.pack()

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

root.bind('<Return>', myClick)
mySubmit.bind("<Button-1>", lambda e, url=mySubmit: open_url(mySubmit ))

My question is how to make mySubmit = Label(root) clickable to open the url in the new browser

Etchosh
  • 111
  • 7
  • Why do you have the `url=mySubmit` in your `lambda`? It serves no purpose. Also you might want to take a look at the `webbrowser` library – TheLizzard May 16 '21 at 19:00
  • @TheLizzard it was a trial which didnt work – Etchosh May 16 '21 at 19:01
  • You should pass the text of the label instead of label itself to `open_url()`. Also you have defined `open_url()` twice and the later one (which does nothing) will be the effective one. – acw1668 May 17 '21 at 02:08
  • The webbrowser answer proposed by TheLizzard will work. It's plain python (independent of Tk) so could be called by that button handler. – Danny Staple May 17 '21 at 10:36

1 Answers1

2
mySubmit.pack()


 mySubmit.bind("<Button-1>", lambda e: open_url("http://www.google.com"))
Nikolay Patarov
  • 305
  • 3
  • 11