我正在编写一个程序,使用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,我插入它们是为了帮助我理解一些代码,因为我是新手。
答案 0 :(得分:0)
要更改标签文字,您只需:
你的f()函数中的lbl4.config(文字=本地时间)
。
您还必须通过取消注释 lbl4 的原始定义来定义标签一次。删除f()中的 pack()调用。
答案 1 :(得分:0)
您可以使用
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()