Python:使用线程时如何在标签(Tkinter)中覆盖写入输出.定时器



我正在编写一个程序,使用python GUI,Tkinter显示当前的日期,时间和天气。天气详细信息是使用来自其他网站的 API 检索的。当我运行程序时,我希望时间发生变化(秒移动等)我已经研究了线程和计时器,我想出了下面的输出。但是,我希望时间每秒都覆盖自己,而不是每次都为每一秒创建一个新标签。谁能告诉我怎么做?

from tkinter import *
import requests
import time 
import threading
url='http://api.openweathermap.org/data/2.5/weather?q=Athlone,ie&appid=ca8b6f0388c49b7d926706a9c498310d'
data=requests.get(url)
read=data.json()
Cname = ("City Name: {}".format(read['name']))
TempDeg =("Tempterature: {}".format(read['main']['temp'] - 273.15))
WeaDesc =("Description: {}".format(read['weather'][0]['description']))
frame = Tk() #Constructor to make the frame in the background to put widgets on
frame.configure(background='black')
#frame.attributes('-fullscreen', True) #Takes up whole screen, no title bar
frame.state('zoomed') #Takes up whole screen with title bar on top. (Easier to exit when testing)
currentdate = time.strftime("%d/%m/%Y")
lbl1 = Label(frame, text=Cname, font=('Times New Roman', 30), fg='white', bg = 'black') 
lbl2 = Label (frame, text=TempDeg + '°C', font=('Times New Roman', 30), fg='white', bg = 'black')
lbl3 = Label (frame, text=WeaDesc, font=('Times New Roman',30), fg='white', bg = 'black')
#lbl4 = Label (frame, text=localtime, font=('Times New Roman',30), fg='white', bg = 'black')
lbl5 = Label (frame, text=currentdate, font=('Times New Roman',30), fg='white', bg = 'black')
lbl6 = Label (frame, text='*Insert complimentary comment here*', font=('Lucida Handwriting', 25), fg = 'white', bg = 'black')
def f():
    localtime = time.strftime("%H:%M:%S")
    lbl4 = Label (frame, text=localtime, font=('Times New Roman',30), fg='white', bg = 'black')
    lbl4.pack()
    threading.Timer(1, f).start()
f()
#lbl4.pack()
lbl5.pack()
lbl1.pack()
lbl2.pack()
lbl3.pack()
lbl6.pack()
frame.mainloop() #inifinte loop that allows the window to stay open

忽略随机 #Comments,它们是我插入的,以帮助我理解一些代码,因为我是新手。

您可以使用

frame.after(1000, f) 

而是threading.Timer(1, f).start()

您必须在函数外部创建lbl4然后使用函数更改文本

lbl4['text'] = localtime
#or 
lbl4.config(text=localtime)

工作示例

import tkinter as tk
import requests
import time 
# --- functions ---
def f():
    localtime = time.strftime("%H:%M:%S")
    lbl4['text'] = localtime
    # run again after 1000ms (1s)
    frame.after(1000, f)
# --- main ---
url = 'http://api.openweathermap.org/data/2.5/weather?q=Athlone,ie&appid=ca8b6f0388c49b7d926706a9c498310d'
data = requests.get(url)
read = data.json()
Cname = "City Name: {}".format(read['name'])
TempDeg = "Tempterature: {}".format(read['main']['temp'] - 273.15)
WeaDesc = "Description: {}".format(read['weather'][0]['description'])
frame = tk.Tk()
frame.configure(background='black')
#frame.attributes('-fullscreen', True) #Takes up whole screen, no title bar
#frame.state('zoomed') #Takes up whole screen with title bar on top. (Easier to exit when testing)
currentdate = time.strftime("%d/%m/%Y")
font = ('Times New Roman', 30)
lbl1 = tk.Label(frame, text=Cname, font=font, fg='white', bg='black') 
lbl2 = tk.Label(frame, text=TempDeg+'°C', font=font, fg='white', bg='black')
lbl3 = tk.Label(frame, text=WeaDesc, font=font, fg='white', bg='black')
# empty label
lbl4 = tk.Label(frame, font=font, fg='white', bg='black')
lbl5 = tk.Label(frame, text=currentdate, font=font, fg='white', bg='black')
lbl6 = tk.Label(frame, text='*Insert complimentary comment here*', font=('Lucida Handwriting', 25), fg='white', bg='black')
lbl4.pack()
lbl5.pack()
lbl1.pack()
lbl2.pack()
lbl3.pack()
lbl6.pack()
# run first time
f()
frame.mainloop()

要更改标签文本,您只需执行以下操作:

lbl4.config(text=localtime)

在你的 f() 函数中。

您还必须通过取消注释 lbl4 的原始定义来定义一次标签。 删除 f() 中的 pack() 调用。

最新更新