Tkinter pyowm未更新

2024-04-25 06:44:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我有几个小部件,或者更准确地说是Label,当在__init__. 中调用以self.after()结束的方法时,这些小部件会完美地更新 这里的标签不是这样的,它通过pyowm从OWM(OpenWeatherMap)获取天气信息,并且应该在after()函数中定义的每一个特定的时间段进行更新。你知道吗

我已经尝试了我所知的一切,尽管我对Python还很陌生。我已经搜索谷歌好几天了,但是没有找到有效的解决方案,或者说我不能让它工作。 我尝试过在每个方法中使用after函数,甚至是__init__。你知道吗

debug和Weather类的修剪main()如下所示:

import tkinter as tk
from Weather import Meteo


def displayMeteo(win):
    # Place Meteo portlet
    meteoHandler = Meteo(win, bg='black', fg='white')
     meteoHandler.pack(side='top', anchor='ne')


def main():
    # Set the screen definition, root window initialization
    root = tk.Tk()
    root.configure(background='black')
    width, height = root.winfo_screenwidth(),
    root.winfo_screenheight()
    root.geometry("%dx%d+0+0" % (width, height))
    label = tk.Label(root, text="Monitor Dashboard", bg='black',
                     fg='red')
    label.pack(side='bottom', fill='x', anchor='se')

    # Display portlet
    displayMeteo(root)

    # Loop the GUI manager
    root.mainloop(0)


###############################
#     MAIN SCRIPT BODY PART   #
###############################
if __name__ == "__main__":
    main()

以及班级:

import pyowm
import tkinter as tk
from PIL import Image, ImageTk


class Meteo(tk.Label):
    def __init__(self, parent=None, **params):
        tk.Label.__init__(self, parent)
        self.API = pyowm.OWM('API KEY', config_module=None,
                         language='it', subscription_type=None)
        self.location = self.API.weather_at_place('Rome,IT')
        self.weatherdata = self.location.get_weather()
        self.weather = str(self.weatherdata.get_detailed_status())
        self.dictionary = {'poche nuvole': 'Parzialmente Nuvoloso',
        'cielo sereno': 'Sereno', 'nubi sparse': 'Nuvoloso',
        'temporale con pioggia': 'Temporale', 'pioggia leggera':
        'Pioggerella'}

        self.Display()

def Temperatur(self):
    self.Temp = tk.StringVar()

    self.tempvalue = self.weatherdata.get_temperature('celsius')
    self.temperature = str(self.tempvalue.get('temp'))

    self.Temp.set(self.temperature)
    self.after(3000, self.Temperatur)

def WeatherInfo(self):
    self.Weather = tk.StringVar()

    self.weather = str(self.weatherdata.get_detailed_status())
    if self.weather in self.dictionary:
        self.weather = self.dictionary[self.weather]

    self.weatherstring = self.weather.title()

    self.Weather.set(self.weatherstring)
    self.after(3000, self.WeatherInfo)

def Display(self):
    self.Temperatur()
    self.WeatherInfo()
    self.configure(text=self.Weather.get() + ",   " + self.Temp.get() + "°", bg='black',
                   fg='#FFFF96', font=("arial, 35"))
    self.after(3000, self.Display)

现在,小部件应该每3秒更新一次(只是为了调试),尽管我知道即使更新有效,也不会每3秒更改一次,因为你知道…天气不会每3秒更改一次。你知道吗


Tags: importselfgetinitmaindefdisplayroot
1条回答
网友
1楼 · 发布于 2024-04-25 06:44:30

正如在Idlehands的建议中所做的那样,问题不在标签或更新中。 如果以这种方式编写代码,则只会调用.get_weather一次,从而创建一个停滞变量。我添加了一个更新pyowm解析的方法,现在一切正常了!你知道吗

相关问题 更多 >