2017-11-11 141 views
0

正如标题提到的,我试图更新tkinter gui中标签的值。这些值来自OpenWeatherMap API,使用pyown和我的订阅级别,我只能打60个电话/分钟。由于我打算打很多电话,所以我想每分钟或5分钟更新一次。我花了最近几天阅读类似的问题,我发现我需要睡眠功能来延迟更新。有人建议我把想要重复的东西放在真正的无限循环中,但是当我尝试这个时,gui只在我关闭窗口时更新,而且我无法控制更新之间的时间。其他人建议我使用.after函数,但是当我这样做时,我的程序会编译,但gui永远不会弹出。我正在寻找某人向我展示这些解决方案中的任何一个如何在我的代码中专门工作,或者如果有第三种解决方案更适合我的代码,那会更好,请让我看看它的外观,因为我我难住了。如何使用pyown和tkinter更新图形用户界面中的文本变量

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() 

更多关于我曾尝试:

while True: 
    def __init__ 
    def search 

但正如这个答案指出,other answer,我不会看到我做我的,而真正的root.mainloop之前的任何变化()

这个问题接近我的答案使用root.after(毫秒,结果),但是当我提供这个答案我gui从来没有显示。 infinitely update

谢谢任何​​试图回答这个问题的人。

编辑:我已经根据建议缩短了我的代码。

+0

这是太多的代码。请将其浓缩至[mcve] –

+0

谢谢Bryan提供的建议。我已经使我的代码示例缩短了很多。良好的如何链接。 – Purin

+0

您的'.after'实现如何不起作用?进一步检查[this](https://stackoverflow.com/a/11505034/7032856)。 – Nae

回答

1

基于this你可以有一个功能,forecast_update,如下所示:

import tkinter as tk 

#these two needed only for API update simulation 
import random 
import string 

root = tk.Tk() 

forecast = tk.Label(text="Forecast will be updated in 60 seconds...") 
forecast.pack() 

# returns a string with 7 random characters, each time it is called, in order to simulate API 
def update_request_from_api(): 
    return ''.join(random.choice(string.ascii_lowercase) for x in range(7)) 


# Your function to update the label 
def forecast_update(): 
    forecast.configure(text=update_request_from_api()) 
    forecast.after(60000, forecast_update) # 60000 ms = 1 minute 


# calling the update function once 
forecast_update() 
root.mainloop() 
+0

谢谢,我的电话按错误顺序排列。 – Purin

+0

返回函数只返回一件事吗?正如我在我的问题中提到的那样,我会立即拨打很多电话(约40)。这是否意味着我需要为每一个做一个新的def?当然有更好的方法... – Purin

+1

您可以从一个函数返回多个值。检查[this](https://stackoverflow.com/questions/354883/how-do-you-return-multiple-values-in-python)。 – Nae