Python 如何使用pyown和tkinter更新GUI中的文本变量

Python 如何使用pyown和tkinter更新GUI中的文本变量,python,datetime,tkinter,auto-update,openweathermap,Python,Datetime,Tkinter,Auto Update,Openweathermap,正如标题所提到的,我正试图在tkinter gui中更新标签中的值。这些值是使用pyown从OpenWeatherMapAPI获取的,在我的订阅级别,我每分钟只能调用60次。因为我计划打很多电话,所以我希望每分钟或5分钟更新一次gui。过去几天我一直在阅读类似的问题,我发现我需要睡眠功能来延迟更新。有人建议我将我想重复的内容放在一个while-True-infinite循环中,但当我尝试这样做时,gui只在关闭窗口时更新,我无法控制更新之间的时间。其他人建议我使用.after函数,但当我这样做时

正如标题所提到的,我正试图在tkinter gui中更新标签中的值。这些值是使用pyown从OpenWeatherMapAPI获取的,在我的订阅级别,我每分钟只能调用60次。因为我计划打很多电话,所以我希望每分钟或5分钟更新一次gui。过去几天我一直在阅读类似的问题,我发现我需要睡眠功能来延迟更新。有人建议我将我想重复的内容放在一个while-True-infinite循环中,但当我尝试这样做时,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()
更多关于我尝试过的内容:

while True:
    def __init__
    def search
但正如这个答案所指出的,我不会看到在root.mainloop()之前的while True中所做的任何更改

这个问题接近我使用root.after(毫秒,结果)的答案,但当我实现这个答案时,我的gui从未显示

感谢所有试图回答这个问题的人

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

基于此,您可以有一个功能,
预测\u更新
,如下所示:

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

这是太多的代码。请把它浓缩成感谢布赖恩的推荐。我已经将我的代码示例缩短了很多。很好的操作链接。您的
.after
实现如何不起作用?还要进一步检查,返回函数只返回一件事,对吗?正如我在问题中提到的,我会一次打很多电话(大约40个)。这是否意味着我需要为每个def创建一个新def?当然有更好的方法…您可以从函数返回多个值。退房