2017-11-11 140 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