全屏时钟上 RSS 提要的时间延迟问题 python
Issues in time delay for RSS feed on full screen clock python
我为我的 Raspberry Pi 3 Model B 设计了一个全屏时钟,它 运行 在 Raspbian 上,但也可以 运行 在 Windows 上.时钟的全部意义在于显示日期、时间和 r/news (Reddit) 上的 RSS 提要。
新闻源应该在屏幕上停留 5 秒然后切换到下一个,这个过程应该一直持续到我退出。如果我使用 sleep()
,时钟停止。我试过使用线程,但它只适用于第一个循环,然后尝试显示下一个,但是 returns 到前一个。
时钟和日期工作正常,只是我无法让提要在屏幕上停留 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 可以 运行 on Python 最多v.3.4.
做你想做的你需要一个 "iterator",这是一种可以一次吐出一个元素的对象。对于你,我会推荐 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 以清理代码。
我为我的 Raspberry Pi 3 Model B 设计了一个全屏时钟,它 运行 在 Raspbian 上,但也可以 运行 在 Windows 上.时钟的全部意义在于显示日期、时间和 r/news (Reddit) 上的 RSS 提要。
新闻源应该在屏幕上停留 5 秒然后切换到下一个,这个过程应该一直持续到我退出。如果我使用 sleep()
,时钟停止。我试过使用线程,但它只适用于第一个循环,然后尝试显示下一个,但是 returns 到前一个。
时钟和日期工作正常,只是我无法让提要在屏幕上停留 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 可以 运行 on Python 最多v.3.4.
做你想做的你需要一个 "iterator",这是一种可以一次吐出一个元素的对象。对于你,我会推荐 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 以清理代码。