将while循环值插入到tkinter魔术窗口

时间:2019-04-22 02:28:41

标签: python python-3.x tkinter

我有一个代码可以在python中创建一个“魔术窗口”。 它应该显示标题,时间和新闻。 我将消息与功能newz一起循环播放(它应该在Tkinter窗口中逐个显示新闻) 我试图将其插入到tkinter label中,但是它不起作用(只是没有出现,但是当我插入纯文本标记它可以工作时),我该怎么做?这是带有时间功能和新闻功能+ tkinter的整个代码


import tkinter as tk
from tkinter import *
import feedparser
import json
from time import sleep
import time
import os

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='日本')
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()



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

# main window
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)

    def newz():
        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
            frequency = myDict.keys()
        frequency = list(frequency)
        for x in range(len(frequency)):
            print (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)


    tick()
    tickk()
    newz() #here it doesnt work

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


1 个答案:

答案 0 :(得分:0)

我不知道您要显示什么,但您必须使用

    source.config(text="some text")
    root.after(1000, newz)

内部函数newz

类似这样的东西

#--- newz - start ---
def newz():
    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
        frequency = myDict.keys()
    frequency = list(frequency)
    for x in range(len(frequency)):
        print(frequency[x])
        source.config(text=str(frequency[x]))
        x += 1

    #source.config(text="some text")
    root.after(1000, newz) # 1000ms = 1s
#--- newz - end ---