Python:当使用threading.Timer时,如何在标签(Tkinter)中重写输出?
我正在编写一个程序,使用python GUI Tkinter显示当前的日期、时间和天气。天气详情是使用另一个网站的API检索的。当我运行程序时,我希望时间在变化(秒移动等)。我已经研究了线程和计时器,并得出了下面的输出。但是,我希望每秒钟都有时间来重写自己,而不是每秒钟都创建一个新标签。谁能告诉我怎么做Python:当使用threading.Timer时,如何在标签(Tkinter)中重写输出?,python,multithreading,tkinter,timer,Python,Multithreading,Tkinter,Timer,我正在编写一个程序,使用python GUI Tkinter显示当前的日期、时间和天气。天气详情是使用另一个网站的API检索的。当我运行程序时,我希望时间在变化(秒移动等)。我已经研究了线程和计时器,并得出了下面的输出。但是,我希望每秒钟都有时间来重写自己,而不是每秒钟都创建一个新标签。谁能告诉我怎么做 from tkinter import * import requests import time import threading url='http://api.openweatherm
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
忽略随机的#注释,它们是由我插入的,以帮助我理解一些代码,因为我是新手。要更改标签文本,您只需执行以下操作: lbl4.config(text=localtime) 在f()函数中 您还必须通过取消对lbl4原始定义的注释来定义标签一次。删除f()中的pack()调用。您可以使用
frame.after(1000, f)
而不是odthreading.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()
如果您所做的只是更新时钟,那么您肯定不需要线程。例如:使用
root.after(毫秒,回调)
而不是thread@BryanOakley我只是以时钟为例。我需要更新当前GUI上的所有详细信息,因为我计划让程序连续运行数小时/数天?您仍然不需要线程,除非获取数据以更新显示需要几百毫秒以上的时间。@BryanOakley好的,谢谢您的反馈。还有一个问题,我是否需要像你在评论中附加的链接中那样使用“自我”论点?你的回答让我有点困惑。。这就是f()现在的样子吗?def():lbl4.config(text=localtime)threading.Timer(1,f).start()