全屏时钟python上RSS提要的时间延迟问题

时间:2018-06-17 14:43:52

标签: python tkinter rss feedparser

我为Raspberry Pi 3 Model B设计了一个全屏时钟,它可以在Raspbian上运行,但也可以在Windows上运行。时钟的重点是在r / news(Reddit)上显示日期,时间和RSS提要。

新闻Feed应该在屏幕上停留5秒然后更改为下一个,此过程应该一直持续到我退出。如果我使用sleep(),时钟就会停止。我尝试过使用线程,但它只适用于第一个循环,然后尝试显示下一个循环,但返回到前一个循环。

时钟和日期工作正常,只是我无法让Feed停留在屏幕上5秒钟,然后转到下一个。

代码:

import sys
if sys.version_info[0] == 2:
    from Tkinter import *
else:
    from tkinter import *
from time import *
import datetime
import feedparser
root = Tk()
d = feedparser.parse('https://www.reddit.com/r/news/.rss')
def exitt():
 sleep(3)
 root.destroy()
time1 = ''
extra_time1 = ''
clock = Label(root, font=('calibri light', 150), bg='black', fg='white')
clock.pack(fill=BOTH, expand=1)
extra_clock = Label(root, font=('calibri light', 45), bg='black', fg='white')
extra_clock.pack(fill=BOTH, expand=1)
label_rss = Label(root, font=('calibri', 14), bg='black', fg='white')
label_rss.pack(fill=BOTH)
end = Button(root, text="Exit", font=('bold', 20), fg="white", 
bg='black', bd=0, borderwidth=0, highlightthickness=0, 
highlightcolor='black', command=lambda:exitt(), height=0, width=0)
end.pack(fill=BOTH)
def rssfeeds():
 for post in d.entries:
     RSSFEED = post.title
     label_rss.config(text=RSSFEED)
     #sleep(5) <-- To prevent glitches but to keep my point
#rssfeeds()
def tick():
 global time1
 time2 = strftime("%H:%M:%S")
 if time2 != time1:
  time1 = time2
  clock.config(text=time2)
 clock.after(1, tick)
def ticki():
 global extra_time1
 extra_time2 = strftime("%A, %d %B %Y")
 if extra_time2 != extra_time1:
  extra_time1 = extra_time2
  extra_clock.config(text=extra_time2)
 extra_clock.after(1, ticki)
tick()
ticki()
w, h = root.winfo_screenwidth(), root.winfo_screenheight()
root.overrideredirect(1)
root.geometry("%dx%d+0+0" % (w, h))
root.focus_set() # <-- move focus to this widget
root.mainloop()

我添加了前几行,以便在使用Python 3时更容易运行此代码,因为feedparser可以在Python上运行到v.3.4。

1 个答案:

答案 0 :(得分:1)

要做你想做的事,你需要一个&#34; iterator&#34;,这是一种可以一次吐出一个元素的对象。对于你我会推荐itertools.cycle,因为它也会在完成之后循环回到开头。请记住,在事件驱动编程(GUI)中,您无法使用正常循环,您必须触发下一个操作。您将使用的事件设置为after

#!/usr/bin/env python

import sys
if sys.version_info[0] == 2:
    import Tkinter as tk
else:
    import tkinter as tk
from time import sleep, strftime
import datetime
import feedparser
from itertools import cycle


root = tk.Tk()
d = feedparser.parse('https://www.reddit.com/r/news/.rss')
post_list = cycle(d.entries)
def exitt():
    sleep(3)
    root.destroy()
time1 = ''
extra_time1 = ''
clock = tk.Label(root, font=('calibri light', 150), bg='black', fg='white')
clock.pack(fill=tk.BOTH, expand=1)
extra_clock = tk.Label(root, font=('calibri light', 45), bg='black', fg='white')
extra_clock.pack(fill=tk.BOTH, expand=1)
label_rss = tk.Label(root, font=('calibri', 14), bg='black', fg='white')
label_rss.pack(fill=tk.BOTH)
end = tk.Button(root, text="Exit", font=('bold', 20), fg="white",
bg='black', bd=0, borderwidth=0, highlightthickness=0,
highlightcolor='black', command=lambda:exitt(), height=0, width=0)
end.pack(fill=tk.BOTH)
def rssfeeds():
    post = next(post_list)
    RSSFEED = post.title
    label_rss.config(text=RSSFEED)
    root.after(5000, rssfeeds) # call this method again in 5 seconds
rssfeeds()
def tick():
 global time1
 time2 = strftime("%H:%M:%S")
 if time2 != time1:
  time1 = time2
  clock.config(text=time2)
 clock.after(1000, tick)
def ticki():
 global extra_time1
 extra_time2 = strftime("%A, %d %B %Y")
 if extra_time2 != extra_time1:
  extra_time1 = extra_time2
  extra_clock.config(text=extra_time2)
 extra_clock.after(1000, ticki)
tick()
ticki()
w, h = root.winfo_screenwidth(), root.winfo_screenheight()
root.overrideredirect(1)
root.geometry("%dx%d+0+0" % (w, h))
root.focus_set() # <-- move focus to this widget
root.mainloop()

还有一些事项需要注意:

  • 使用4个空格进行缩进(阅读PEP8以获得更多样式建议)
  • 不要使用通配符导入(from module import *),它们会导致错误并且也会违反PEP8。
  • 总是使用shebang,尤其是在linux系统上。
  • 尽快移动到类和OOP以清理代码。