Tkinter-For Loop为时过早

时间:2019-04-22 05:12:41

标签: python python-3.x loops tkinter

我有一个“魔术镜”的代码,我想在其中显示时钟,标题和新闻(日语)。

我有一个完整的代码,可以在其中使用news代码-在Tkinter循环中-news代码使用json接收整个消息,隐藏标题以外的所有内容,然后放入到列表并显示循环遍历以逐个显示消息。它在终端中运行良好,但是我很难将其放入Tkinter窗口循环中-循环遍历并仅显示最后一个news主题-我希望它们全部出现,每10秒左右。请问有办法吗?谢谢,我会很高兴的。

这是密码


import tkinter as tk
from tkinter import *

startupscreen = tk.Tk()
startupscreen.title('Magic Mirror: Python Mod')
welcometext = tk.Label(startupscreen, font = ('caviar dreams', 40), bg='black', fg='white')
startupscreen.configure(background='black')
startupscreen.overrideredirect(True)
welcometext.config(text='Mirror: Vuoristo Mod')
welcometext.pack(side=LEFT, padx= 120, pady=80)
# Gets the requested values of the height and widht.
windowWidth = startupscreen.winfo_reqwidth()
windowHeight = startupscreen.winfo_reqheight()
# Gets both half the screen width/height and window width/height
positionRight = int(startupscreen.winfo_screenwidth()/3 - windowWidth/2)
positionDown = int(startupscreen.winfo_screenheight()/2 - windowHeight/2)

# Positions the window in the center of the page.
startupscreen.geometry("+{}+{}".format(positionRight, positionDown))
startupscreen.update()

import time
from newsapi import NewsApiClient
import os
import feedparser
import json
from time import sleep

decrypt = list()
global iteration
global timecount
global repull
global sleep
iteration = 0
timecount = 0
repull = 0
sleep = 0


while True:


    def tick(time1=''):
        time2 = time.strftime("%H")
        if time2 != time1:
            time1 = time2
            clock_frame.config(text=time2)
        clock_frame.after(200, tick)

    def tickk(time3=''):
        time4 = time.strftime(":%M:%S")
        if time4 != time3:
            time3 = time4
            clock_frame2.config(text=time4)
        clock_frame2.after(200, tickk)


    #This function waits for a certain amount of 'tocks' and then initiates 'newsheader' -function
    def tock():
        global timecount
        global repull
        global sleep
        global decrypt
        newstitle.after(200, tock)
        if timecount < 20:
            timecount +=1
        else:
            timecount = 0
            newsheader()
        if repull < 200:
            repull +=1

        if sleep < 800:
            sleep+=1
        else:
            sleep = 0
            motiondetector()


    #This function iterates over the news headlines. Iteration is the news number, 'itemlist' brings out only the title.
    def newsheader():
        url = 'https://news.google.com/rss?hl=ja&gl=JP&ceid=JP:ja'
        d = feedparser.parse(url)
        news = list()

        for i, entry in enumerate(d.entries, 1):
            p = entry.published_parsed
            sortkey = "%04d%02d%02d%02d%02d%02d" % (p.tm_year, p.tm_mon, p.tm_mday, p.tm_hour, p.tm_min, p.tm_sec)

            tmp = {
                "title": entry.title,
            #"link": entry.link,
                "sortkey": sortkey
                }

            news.append(tmp)

        news = sorted(news, key=lambda x: x['sortkey'])

        myDict = {}
# HERE IS THE PROBLEM, I HAVE LIKE 30 news IN `frequency`, BUT IT SHOWS ONLY LAST ONE
        for d in news:
            c = d['title']
            myDict[c] = myDict.get(c,0)+1
            frequency = myDict.keys()
            frequency = list(frequency)
            for x in range(len(frequency)):
                 source.config(text=str(frequency[x]))
                 x += 1


    root = tk.Tk()
    root.title('Mirror')
    lab = Label(root, text=" 日本", font = ('', 40), bg='black', fg='white')
    lab.pack(anchor=SW, fill=X, padx=45)
    masterclock = tk.Label(root)
    masterclock.pack(anchor=NW, fill=X, padx=45)
    masterclock.configure(background='black')
    clock_frame = tk.Label(root, font = ('caviar dreams', 130), bg='black', fg='white')
    clock_frame.pack(in_=masterclock, side=LEFT)
    clock_frame2 = tk.Label(root, font = ('caviar dreams', 70), bg='black', fg='white')
    clock_frame2.pack(in_=masterclock, side=LEFT, anchor = N, ipady=15)
    newstitle = tk.Label(root, font = ('caviar dreams', 30), bg='black', fg='white')
    newstitle.pack(side=BOTTOM, anchor=W, fill=X)
    source = tk.Label(root, font = ('caviar dreams', 20), bg='black', fg='white')
    source.pack(side=BOTTOM, anchor=W, fill=X)

    newsheader()
    tick()
    tickk()
    tock()

    root.attributes("-fullscreen", True)
    root.configure(background='black')
    startupscreen.destroy()
    root.mainloop()


2 个答案:

答案 0 :(得分:1)

此代码使用函数display_next_item从列表frequency获取下一个元素并将其放入Label。 1秒后,它使用after()再次执行此操作。您可以设置10秒,但为了测试,我使用较小的值。

对于测试,我还必须删除fullscreennewsapi(我尚未安装)

import tkinter as tk
from tkinter import *

startupscreen = tk.Tk()
startupscreen.title('Magic Mirror: Python Mod')
welcometext = tk.Label(startupscreen, font = ('caviar dreams', 40), bg='black', fg='white')
startupscreen.configure(background='black')
startupscreen.overrideredirect(True)
welcometext.config(text='Mirror: Vuoristo Mod')
welcometext.pack(side=LEFT, padx= 120, pady=80)
# Gets the requested values of the height and widht.
windowWidth = startupscreen.winfo_reqwidth()
windowHeight = startupscreen.winfo_reqheight()
# Gets both half the screen width/height and window width/height
positionRight = int(startupscreen.winfo_screenwidth()/3 - windowWidth/2)
positionDown = int(startupscreen.winfo_screenheight()/2 - windowHeight/2)

# Positions the window in the center of the page.
startupscreen.geometry("+{}+{}".format(positionRight, positionDown))
startupscreen.update()

import time
#from newsapi import NewsApiClient
import os
import feedparser
import json
from time import sleep

decrypt = list()
global iteration
global timecount
global repull
global sleep
iteration = 0
timecount = 0
repull = 0
sleep = 0


while True:


    def tick(time1=''):
        time2 = time.strftime("%H")
        if time2 != time1:
            time1 = time2
            clock_frame.config(text=time2)
        clock_frame.after(200, tick)

    def tickk(time3=''):
        time4 = time.strftime(":%M:%S")
        if time4 != time3:
            time3 = time4
            clock_frame2.config(text=time4)
        clock_frame2.after(200, tickk)


    #This function waits for a certain amount of 'tocks' and then initiates 'newsheader' -function
    def tock():
        global timecount
        global repull
        global sleep
        global decrypt
        newstitle.after(200, tock)
        if timecount < 20:
            timecount +=1
        else:
            timecount = 0
            newsheader()
        if repull < 200:
            repull +=1

        if sleep < 800:
            sleep+=1
        else:
            sleep = 0
            motiondetector()


    #This function iterates over the news headlines. Iteration is the news number, 'itemlist' brings out only the title.
    def newsheader():
        url = 'https://news.google.com/rss?hl=ja&gl=JP&ceid=JP:ja'
        d = feedparser.parse(url)
        news = list()

        for i, entry in enumerate(d.entries, 1):
            p = entry.published_parsed
            sortkey = "%04d%02d%02d%02d%02d%02d" % (p.tm_year, p.tm_mon, p.tm_mday, p.tm_hour, p.tm_min, p.tm_sec)

            tmp = {
                "title": entry.title,
            #"link": entry.link,
                "sortkey": sortkey
                }

            news.append(tmp)

        news = sorted(news, key=lambda x: x['sortkey'])

        myDict = {}

        for d in news:
            c = d['title']
            myDict[c] = myDict.get(c,0)+1

        global frequency
        frequency = list(myDict.keys())


    def display_next_item():
        global frequency
        global next_index

        next_index += 1
        if next_index >= len(frequency):
            next_index = 0

        source.config(text=str(frequency[next_index]))

        root.after(1000, display_next_item)


    frequency = [] # value at start
    next_index = 0 # value at start

    root = tk.Tk()
    root.title('Mirror')
    lab = Label(root, text=" 日本", font = ('', 40), bg='black', fg='white')
    lab.pack(anchor=SW, fill=X, padx=45)
    masterclock = tk.Label(root)
    masterclock.pack(anchor=NW, fill=X, padx=45)
    masterclock.configure(background='black')
    clock_frame = tk.Label(root, font = ('caviar dreams', 130), bg='black', fg='white')
    clock_frame.pack(in_=masterclock, side=LEFT)
    clock_frame2 = tk.Label(root, font = ('caviar dreams', 70), bg='black', fg='white')
    clock_frame2.pack(in_=masterclock, side=LEFT, anchor = N, ipady=15)
    newstitle = tk.Label(root, font = ('caviar dreams', 30), bg='black', fg='white')
    newstitle.pack(side=BOTTOM, anchor=W, fill=X)
    source = tk.Label(root, font = ('caviar dreams', 20), bg='black', fg='white')
    source.pack(side=BOTTOM, anchor=W, fill=X)

    newsheader()
    tick()
    tickk()
    tock()
    display_next_item() # <- start displaying


    #root.attributes("-fullscreen", True)
    root.configure(background='black')
    startupscreen.destroy()
    root.mainloop()

答案 1 :(得分:0)

您需要使用threading。而且,您可以创建重复类或背景类,以每10秒执行一次新闻阅读,例如this