首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >全屏时钟python上RSS源的时间延迟问题

全屏时钟python上RSS源的时间延迟问题
EN

Stack Overflow用户
提问于 2018-06-17 22:43:52
回答 1查看 116关注 0票数 0

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

新闻提要应该在屏幕上停留5秒钟,然后切换到下一个提要,这个过程应该一直持续下去,直到我退出。如果我使用sleep(),时钟就会停止。我尝试过使用线程,但它只在第一个循环中起作用,然后尝试显示下一个循环,但返回到前一个循环。

时钟和日期运行正常,只是我不能让提要在屏幕上停留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()

如果您使用的是Python3,我添加了前几行,以便更容易地运行此代码,因为feedparser可以在Python v.3.4之前的版本上运行。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-18 01:33:25

要做你想做的事情,你需要一个“迭代器”,这是一种可以一次输出一个元素的对象类型。对于您,我推荐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()

还有一些需要注意的事情:

linux recommendations)

  • Do使用4个空格进行缩进(有关更多样式,请阅读
  • not use wildcard (from module import *),它们会导致错误,也不利于PEP8。
  • 总是使用缩进,但尤其是在系统上)。
    • 尽快转移到类和OOP来清理代码。
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50897702

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档