Tkinter RSS Ticker 在每次更新时加速

Tkinter RSS Ticker accelerating at every update

google 没有任何帮助,我也问过一些人,但 none 似乎知道如何回答我的问题。

我正在为一个项目编写 GUI,它包含一个 RSS-Feed 自动收报机。 它滚动浏览新闻,当它更新时(由于明显的调试原因,每 3 秒一次)它会加快一点。 这意味着,如果我 运行 程序,两小时后自动收报机以非人类可读的速度滚动。

主要代码不是我写的,我自己修改,加上更新功能

main():

import tkinter as tk
from Press import RSSTicker

def displayRSSticker(win):
    # Place RSSTicker portlet
    tickerHandle = RSSTicker(win, bg='black', fg='white', highlightthickness=0, font=("avenir", 30))
    tickerHandle.pack(side='bottom', fill='x', anchor='se')

def main():
    # Set the screen definition, root window initialization
    root = tk.Tk()
    root.configure(background='black')
    width, height = root.winfo_screenwidth(), root.winfo_screenheight()
    root.geometry("%dx%d+0+0" % (width, height))
    label = tk.Label(root, text="Monitor Dashboard", bg='black', fg='red')
    label.pack(side='bottom', fill='x', anchor='se')

    # Display portlet
    displayRSSticker(root)
    # Loop the GUI manager
    root.mainloop(0)

###############################
#     MAIN SCRIPT BODY PART   #
###############################
if __name__ == "__main__":
    main()

RSSTicker class:

import feedparser
import tkinter as tk

class RSSTicker(tk.Text):
    # Class constructor
    def __init__(self, parent, **params):
        super().__init__(parent, height=1, wrap="none", state='disabled', **params)
        self.newsFeed = feedparser.parse('http://www.repubblica.it/rss/homepage/rss2.0.xml')
        self.update()

    # Class methods
    def update(self):
        self.headlineIndex = 0
        self.text = ''
        self.pos = 0
        self.after_idle(self.updateHeadline)
        self.after_idle(self.scroll)
        self.after(4000, self.update)

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

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

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

我还以为是self.pos这个变量的问题,打印出来结果是加数,重新设置为1加数比较快。。不过好像不是问题导致的加速自动收报机。 据我了解,问题一定出在滚动方法中。

如果有人知道如何在更新时保持原来的滚动速度,谢谢。

我认为你可以使用几个跟踪变量来确保 update 只启动循环一次,然后下次调用 update 时它只会 运行 scroll 而不开始新的循环。同时如果 scroll 没有被 update 调用,那么它将根据需要继续循环。

将您的 RSSTicker class 更改为以下内容:

class RSSTicker(tk.Text):
    # Class constructor
    def __init__(self, parent, **params):
        self.scroll_started = False # Tracker for first update.
        super().__init__(parent, height=1, wrap="none", state='disabled', **params)
        self.newsFeed = feedparser.parse('http://www.repubblica.it/rss/homepage/rss2.0.xml')
        self.update()

    def update(self):
        self.headlineIndex = 0
        self.text = ''
        self.pos = 0
        self.after_idle(self.updateHeadline)
        self.after_idle(lambda: self.scroll('update'))
        self.after(4000, self.update)

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

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

    def scroll(self, from_after_or_update = 'after'):
        self.config(state='normal')
        if self.pos < len(self.text):
            self.insert('end', self.text[self.pos])
        self.pos += 1
        self.see('end')
        self.config(state='disabled')
        # Check if the loop started by after.
        if from_after_or_update != 'update':
            self.scroll_started = True
            self.after(180, self.scroll)
        # If not started by after check to see if it is the 1st time loop is started by "update".
        elif self.scroll_started is False and from_after_or_update == 'update':
            self.scroll_started = True
            self.after(180, self.scroll)
        # If neither of the above conditions then do not start loop to prevent multiple loops.
        else:
            print("ran scroll method without adding new after loop!")