正如title提到的,我正在尝试在tkintergui中更新标签中的值。这些值是使用pyown从openweathermapapi获取的,在我的订阅级别,我每分钟只能调用60次。因为我计划打很多电话,所以我希望我的图形用户界面每分钟或5分钟更新一次。我花了几天时间阅读了类似的问题,我发现我需要sleep函数来延迟更新。有人建议我把我想重复的东西放到while真的无限循环中,但是当我尝试的时候,gui只在我关闭窗口时才更新,我无法控制更新之间的时间。其他人建议我使用.after函数,但是当我这样做时,我的程序会编译,但是gui永远不会弹出。我正在寻找一个人来告诉我这两种解决方案在我的代码中是如何工作的,或者如果有第三种解决方案能更好地帮助我的代码,那就更好了,请让我看看它会是什么样子,因为我被难住了。在
import tkinter as tk
import pyowm
from datetime import datetime, timedelta
class WeatherInfo(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.wm_title('Forecast')
self.currentTime = tk.StringVar(self, value='')
self.d2temp_7 = tk.StringVar(self,value='')
self.owm = pyowm.OWM('*INSERT YOUR OWM KEY HERE*')
self.headLabel = tk.Label(self, text='5-Day Forecast of Cayce, US.')
self.headLabel.pack()
self.footLabel = tk.Label(self, textvariable=self.currentTime)
self.footLabel.pack(side=tk.BOTTOM)
self.day2Frame = tk.LabelFrame(self, text='D2')
self.day2Frame.pack(fill='both', expand='yes', side=tk.LEFT)
tk.Label(self.day2Frame, text="Temperature:").pack()
tk.Label(self.day2Frame, textvariable=self.d2temp_7).pack()
self.search()
def search(self):
fc = self.owm.three_hours_forecast_at_id(4573888)
try:
self.currentTime.set(datetime.today())
self.d2temp_7.set("7am: " + str(fc.get_weather_at((datetime.today().replace(hour=13, minute=00) + timedelta(days=1))
.strftime ('%Y-%m-%d %H:%M:%S+00')).get_temperature('fahrenheit')['temp']))
except:
self.temp.set('Pick a city to display weather.')
def _quit(self):
self.quit()
self.destroy()
if __name__== "__main__":
app = WeatherInfo()
app.mainloop()
关于我尝试过的更多信息:
^{pr2}$但正如这个答案所指出的,other answer,我不会看到我在whiletrue中所做的任何更改根.mainloop()
这个问题很接近我的答案根。后(毫秒,结果),但当我实现这个答案时,我的gui从未显示出来。infinitely update
感谢任何试图回答这个问题的人。在
编辑:我已经根据建议缩短了代码。在
基于this可以有一个函数,
forecast_update
,如下所示:相关问题 更多 >
编程相关推荐