-1

I have several widgets, or more correctly, Labels, that update perfectly when a method finishing with self.after() is called in __init__. That's not the case with this Label here, which gets the weather Information from OWM (OpenWeatherMap) through pyowm, and should be updated every specific amount of time, defined in the after() function.

I've tried almost everything in my knowledge, though I'm pretty newbie to Python. I've searched google for days, but no working solution was found, or not that I could make it work. I've tried to put the after function in every method, even __init__.

The trimmed down main() for debug and the Weather Class follows:

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

And the class:

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)

Now, what should happen is the widget updating every 3 secs (just to debug), although I know even if updating works it won't change every 3 seconds, 'cause you know...weather doesn't change every 3 secs.

martineau
  • 119,623
  • 25
  • 170
  • 301
Shade Reogen
  • 136
  • 2
  • 13
  • 1
    Not familiar with the API, but are you sure you're getting the refreshed data in each `WeatherInfo()` and `Temperatur()`? It could be that once you have initialized `self.weatherdata` the data is already stagnant. Try `print`ing out the `get_detailed_status()` to see if they are actually any different in each refresh. If not, you might need to refresh `self.weatherdata` first. – r.ook Jan 07 '19 at 20:07
  • Thanks, I'll check – Shade Reogen Jan 07 '19 at 20:09
  • The `tkinter` part works for me, `Label`s updated every 3 sec. – stovfl Jan 07 '19 at 20:47
  • Thanks, @Idlehands, the problem was, as you pointed out, the variable itself being called once. – Shade Reogen Jan 07 '19 at 21:19

1 Answers1

1

As done in suggestion of Idlehands the problem was not in the Label or the updating. The Code, if written that way, would call .get_weather only once, thus creating a stagnating variable. I added a method that updates the pyowm parsing and now evertyhing's working fine!

Shade Reogen
  • 136
  • 2
  • 13