0

So I have a list of text I'm looping in a frame one by one. I want some of the text (ex: 'apple') to have a hidden hyperlink attached to the text or just the hyperlink itself. I know we can use a webbrowser module to open one link. Is there a way to add links to each text in the list?

qlist.py

qtextnlinks = ['apple', 'orange', 'banana', 'grapes', 'https://www.youtube.com/watch?v=yPGK1-6m_j4']

mainprogram.py

frame_p = tk.Frame(root)
frame_p.place(relx = 0.5, rely = 0.9, anchor = "center")

label_p = tk.Label(frame_p,font="helvetica 14", wraplength=qwidth, justify="center")
label_p.pack()
def after():
    label_p.config(text=random.choice(qlist.qtextnlinks))
    root.after(15000, after)
after()
Advik
  • 43
  • 9

1 Answers1

0

I suggest you to use dict instead of list. Or nested sets at least.

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:
    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()

Full answer is available there

stilManiac
  • 454
  • 3
  • 13