0

I'm working on a tkinter project and I have used tkinter.scrolledtext to show the result of a searching operation, and what I want is a way to redirect each searching result in the scrolled text to a specified url.

my idea was to make the string in the scrolled text clickable and when I click, it will redirect me to the URL that I specify. Another solution is to add a button, but I have really searched about that but I didn't find a lot of documentation.

Any ideas, please!

Ravikant Singh
  • 338
  • 5
  • 18
Jalil
  • 91
  • 9
  • Probably related, https://stackoverflow.com/questions/52436214/add-web-browser-window-to-tkinter-window – Kamal Jun 24 '19 at 02:26

1 Answers1

0

If you can use Listbox instead of ScrolledText, below is an example:

from tkinter import *
import webbrowser

def on_url_click(event):
    selected = event.widget.nearest(event.y) # find the index near the mouse click position
    url = event.widget.get(selected)
    webbrowser.open(url) # open the URL using default browser

root = Tk()

urllist = Listbox(root, width=80, height=20)
urllist.pack()
urllist.bind('<Button-1>', on_url_click)

# insert some URLs
for url in ('http://www.google.com', 'http://www.reddit.com', 'http://www.stackoverflow.com'):
    urllist.insert(END, url)

root.mainloop()
acw1668
  • 40,144
  • 5
  • 22
  • 34
  • My problem is that the text in the listbox's items is too long and it should be written in two lines... and in listboxs I didn't find a way to write two lines in the same item, because `\n` is not working there! any solution ? – Jalil Jun 28 '19 at 16:57