-1

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 
Joshua
  • 40,822
  • 8
  • 72
  • 132
Reogen
  • 13
  • 1
  • What have you tried so far? I would suggest you take a look at 1) https://stackoverflow.com/questions/3393612/run-certain-code-every-n-seconds/13151299 2) https://stackoverflow.com/questions/474528/what-is-the-best-way-to-repeatedly-execute-a-function-every-x-seconds-in-python – RPT Mar 19 '18 at 12:31
  • Possible duplicate of https://stackoverflow.com/questions/474528/what-is-the-best-way-to-repeatedly-execute-a-function-every-x-seconds-in-python – RPT Mar 19 '18 at 12:32
  • Possible duplicate of [What is the best way to repeatedly execute a function every x seconds in Python?](https://stackoverflow.com/questions/474528/what-is-the-best-way-to-repeatedly-execute-a-function-every-x-seconds-in-python) – nattfodd Mar 19 '18 at 12:34
  • I don't see why this should be duplicate to the question you have linked. My main question is not how to execute the program every 2 minutes, but mainly how to change the 0. Could you please explain how that's related? I'm kind of noobish here (i have no intent of sounding rude) – Reogen Mar 19 '18 at 14:55

1 Answers1

0

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()
scotty3785
  • 6,763
  • 1
  • 25
  • 35
  • IT WORKS! Thank you! At least there is someone who answers the questions instead of downvoting! – Reogen Mar 22 '18 at 08:45
  • Hey, the program is awesome, but I've stumbled across a really noobish and probably facepalming. I'm working with a full screen window, and use labels for everything. I cannot find a way to move your "self.headline" in the middle. The .place(coordinates) isn't working, and modifying the .grid() to .grid(rows&columns) doesn't result in anything either – Reogen Mar 22 '18 at 09:48
  • Its generally a bad idea to mix grid/pack/place layout methods. You may have to change my code to only use place throughout. I never use place so I'm not sure. – scotty3785 Mar 22 '18 at 09:56