Search code examples
python-3.xuser-interfacetkinterrss-reader

How to make a text link in Python3.7 tkinter


I'm trying to make a RSS reader with GUI in Python 3.7 and everything is almost working but there is a problem, instead of that each line pointing to another link, each line moves to the exact same link. What can I do to solve this?

The Code:

import feedparser
from tkinter import *
import webbrowser

def callback(event):
    webbrowser.open_new(article_link)


feed = feedparser.parse("http://www.widgeti.co.il/feed/")

feed_title = feed['feed']['title']
feed_entries = feed.entries


root = Tk()
text = Text(root)
for entry in feed.entries:

    article_title = entry.title
    article_link = entry.link
    article_published_at = entry.published # Unicode string
    article_published_at_parsed = entry.published_parsed # Time object
    article_author = entry.author
    content = entry.summary
    article_tags = entry.tags

    print ("{}[{}]".format(article_title, article_link))
    print ("Published at {}".format(article_published_at))
    print ("Published by {}".format(article_author))
    print("Content {}".format(content))
    print("catagory{}".format(article_tags))



    link = Label(root, text="{}\n".format(article_title), fg="blue", cursor="hand2")
    link.pack()

    link.bind("<Button-1>" ,callback)

text.tag_config("here")
root.mainloop()

Solution

  • You are not passing the link in argument to the callback function, therefore when callback is triggered, it uses the current value of article_link which is the link of the last entry. To fix this you can pass the link to callback using a lambda function in the for loop:

    def callback(event, article_link):
        webbrowser.open_new(article_link)
    
    # ...
    
    for entry in feed.entries:
        # ...
        link.bind("<Button-1>", lambda event, link=article_link: callback(event, link))