我为我的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之前的版本上运行。
发布于 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)
from module import *
),它们会导致错误,也不利于PEP8。https://stackoverflow.com/questions/50897702
复制相似问题