Tkinter RSS Feed scrolling (Left to Right)

时间:2018-12-03 12:52:53

标签: python python-3.x tkinter scroll rss

I have this bit of code in my Graduation-project-program...thing.

Extract:

[...]

rssfeed = tkinter.Frame(window, bg='black', width=200, height=80)
feed = feedparser.parse('http://www.repubblica.it/rss/homepage/rss2.0.xml')
feedShow = {'entries': [{feed['entries'][0]['title']}]}


class RSSDisplay(tkinter.Frame):[4]
    def __init__(self, master=None, **kw):
        tkinter.Frame.__init__(self, master=master, **kw)
        self.txtHeadline = tkinter.StringVar()
        self.headline = tkinter.Label(self, textvariable=self.txtHeadline,
                                      bg='black', fg='white', font=("arial", 20))
        self.headline.grid()
        self.headlineIndex = 0
        self.updateHeadline()

    def updateHeadline(self):
        try:
            headline = feed['entries'][self.headlineIndex]['title']
        except IndexError:
            self.headlineIndex = 0
            headline = feed['entries'][self.headlineIndex]['title']
        self.txtHeadline.set(headline)
        self.headlineIndex += 1
        self.after(10000, self.updateHeadline)

[...]

RSSDisplay(window).place(x=340, y=500)

Now this displays the Headlines of my favorite newspaper ( La Repubblica, I'm Italian), which are updated every 10 seconds. Since RSSDisplay(window).place(x=340, y=500)looks ugly beacause the text isn't centered, 'cause every sentence starts at said coordinates and not at the center for each entry in the Headlines, but always at x=340 and y=500. I'd need it to be scrolling on top from left to right, instead of abruptly change.

If this isn't achieveable please point out to me under what conditions this could be done (i.e. changing Framework, GUI or language even thi I'd prefer to stick with Python).

If more information is needed please tell me and I'll try to add it. Thanks everyone.

2 个答案:

答案 0 :(得分:0)

Tkinter comes with different geometry managers of which I don't recommend place (not very flexible..). I am not exactly sure how you want your layout to look like. But you could try pack (besides pack there is also grid):

RSSDisplay(window).pack(expand='yes', fill='x')

I recommend to read up on how tkinter geometry managers work, e.g. here: http://effbot.org/zone/tkinter-geometry.htm

Note: it is not recommended (and leads to bugs) to mix different geometry managers. But it works if you use pack on the window level while using grid inside your frame - as you do to place the label with the grid method - just in case you want to add more widgets later inside your frame...

答案 1 :(得分:0)

以下是一个简单的代码:我使用tkinter.Text小部件(http://effbot.org/tkinterbook/text.htm)。在tick()中,我移动了文本小部件的see()功能以将新闻源的一个字符向右移。 当然,可以对此进行很多改进(要获得更通用的布局,可以尝试使用tkinter.Canvas小部件)。

import feedparser
import tkinter as tk

feed = feedparser.parse('http://www.repubblica.it/rss/homepage/rss2.0.xml')
feedShow = {'entries': [{feed['entries'][0]['title']}]}

class RSSTicker(tk.Text):
    def __init__(self, parent, **kw):
        super().__init__(parent, height=1, wrap="none", state='disabled', **kw)
        self.headlineIndex = 0
        self.text = ''
        self.pos = 0
        self.after_idle(self.updateHeadline)
        self.after_idle(self.tick)

    def updateHeadline(self):
        try:
            self.text += '.....' + feed['entries'][self.headlineIndex]['title']
        except IndexError:
            self.headlineIndex = 0
            self.text = feed['entries'][self.headlineIndex]['title']

        self.headlineIndex += 1
        self.after(10000, self.updateHeadline)

    def tick(self):
        if self.pos < len(self.text):
        self.config(state='normal')
        self.insert('end', self.text[self.pos])
        self.pos += 1
        self.see('end')
        self.config(state='disabled')
    self.after(300, self.tick)

if __name__ == '__main__':
    root = tk.Tk()
    ticker = RSSTicker(root, bg='black', fg='white', font=("arial", 20))
    ticker.pack(side='top', fill='x')
    root.mainloop()