Search code examples
python-3.xtkintertimerfeedparser

Update RSS feed every 2 minutes


I've searched pretty everywhere so that's why I'm asking here. My program shows a parsed RSS feed (news), that looks like this in the end:

feedShow = feed['entries'][0]['title'] 

Now, this feedShow element is the text displayed later by a Label. The 0 in the code line determines which news title to display. I'd like to change that every 2 minutes to +1. How do I do that? I'd have known in a more basic situation but that's in the middle of the code. As my clock, I use

import time
from datetime import date
from datetime import datetime 

Solution

  • How about something like this. Code will update the self.headlineIndex every X seconds (currently set to 1 second). The headline is grabbed using headline = feed['entries'][self.headlineIndex]['title']. Notice the self.headlineIndex inside the square braces rather than a number like zero.

        try:
        import tkinter as tk
    except:
        import Tkinter as tk
    
    """Some dummy data in the same format as OP"""
    feed = {'entries': [{'title':"US Pres. incompetent"},
                        {'title':"Sport happened"},
                        {'title':"Politican Corrupt"}]
            }
    
    class App(tk.Frame):
        def __init__(self,master=None,**kw):
            tk.Frame.__init__(self,master=master,**kw)
            self.txtHeadline = tk.StringVar()
            self.headline = tk.Label(self,textvariable=self.txtHeadline)
            self.headline.grid()
    
            self.headlineIndex = 0
    
            self.updateHeadline()
    
        def updateHeadline(self):
            try:
                headline = feed['entries'][self.headlineIndex]['title']
            except IndexError:
                """This will happen if we go beyond the end of the list of entries"""
                self.headlineIndex = 0
                headline = feed['entries'][self.headlineIndex]['title']
    
            self.txtHeadline.set(headline)
            self.headlineIndex += 1
            self.after(1000,self.updateHeadline)
    
    
    if __name__ == '__main__':
        root = tk.Tk()
        App(root).grid()
        root.mainloop()