简体   繁体   English

Python appJar 在网络响应后不更新 UI

[英]Python appJar not updating UI after network response

I'm a bit rusty with python, so hopefully I'm just doing something silly.我对 python 有点生疏,所以希望我只是在做一些愚蠢的事情。 I'm trying to create a simple weather widget using appJar and the OpenWeatherMap, and the UI isn't updated as expected when the network response returns.我正在尝试使用 appJar 和 OpenWeatherMap 创建一个简单的天气小部件,当网络响应返回时,UI 没有按预期更新。 I read the appJar documentation and I know I need to kick off the network request on another thread, and then enqueue the UI change when the response comes back, but it doesn't seem to be working.我阅读了 appJar 文档,我知道我需要在另一个线程上启动网络请求,然后在响应返回时将 UI 更改排入队列,但它似乎不起作用。 It's a very simple application so far, here's the entirety of the code:到目前为止,这是一个非常简单的应用程序,以下是完整代码:

app.py应用程序

from appJar import gui
import datetime, pyowm, WeatherUtils
from WeatherInfo import WeatherInfo

LES_LAT = 40.714080
LES_LON = -73.985890

app = gui()
app.setBg("#363636")
app.setSize("Fullscreen")

# Date and time
def addDateTimeLabel(labelName: str, text: str) -> None:
    app.addLabel(labelName, text)
    app.setLabelFg(labelName, "White")
    app.setLabelFont(labelName, size=32)

now = datetime.datetime.now()
date = now.strftime("%A %B %e, %Y")
time = now.strftime("%l:%M %p")

addDateTimeLabel("date", date)
addDateTimeLabel("time", time)

# Weather UI
addDateTimeLabel('current_weather', 'Loading...')
addDateTimeLabel('forecast', 'Loading...')

app.go()

# Weather Data Fetch
def setCurrentWeatherData(weather_data: WeatherInfo) -> None:
    weather_string = 'Current: ' + weather_data.status + " " + str(weather_data.temp) + ", High: " + str(weather_data.max_temp) + ", Low: " + str(weather_data.min_temp)
    app.queueFunction(app.setLabel, 'current_weather', weather_string)

app.thread(WeatherUtils.getCurrentTemperature, LES_LAT, LES_LON, setCurrentWeatherData)

WeatherUtils.py WeatherUtils.py

from typing import Callable, List
from WeatherInfo import WeatherInfo
import pyowm, datetime
from pyowm.weatherapi25.observation import Observation

OPEN_WEATHER_MAP_API_KEY = "XXXXXXXX"
owm = pyowm.OWM(OPEN_WEATHER_MAP_API_KEY)

def createWeatherInfoFromResponse(observation: Observation) -> WeatherInfo:
    weather_data = observation.get_weather()
    temperature_data = weather_data.get_temperature('fahrenheit')
    if temperature_data is None:
        return

    weather = WeatherInfo(temperature_data.get('temp'), temperature_data.get('temp_min'), temperature_data.get('temp_max'), weather_data.get_detailed_status().title())

    weather.snow = weather_data.get_snow()
    weather.icon_url = weather_data.get_weather_icon_url()
    weather.sunrise_time = weather_data.get_sunrise_time()
    weather.sunset_time = weather_data.get_sunset_time()
    weather.humidity = weather_data.get_humidity()
    wind_data = weather_data.get_wind()
    if wind_data is not None:
        weather.wind_direction = wind_data.get('deg')
        weather.wind_speed = wind_data.get('speed')

    weather.reference_time = weather_data.get_reference_time()
    weather.location = observation.get_location().get_name()
    return weather

def getCurrentTemperature(lat: float, lon: float, callback: Callable[[WeatherInfo], None]) -> None:
    weather_observation = owm.weather_at_coords(lat, lon)
    weather = createWeatherInfoFromResponse(weather_observation)
    if weather is not None:
        callback(weather)

WeatherInfo.py天气信息.py

class WeatherInfo:
    def __init__(self, temp: float, min_temp: float, max_temp: float, status: str):
        self.temp = temp
        self.min_temp = min_temp
        self.max_temp = max_temp
        self.status = status
        self.snow = None
        self.humidity = None
        self.icon_url = None
        self.sunrise_time = None
        self.sunset_time = None
        self.wind_direction = None
        self.wind_speed = None
        self.reference_time = None
        self.location = None

    def get_sunrise_time(self) -> str:
        return self.sunrise_time.strftime('%l:%M %p')

    def get_sunset_time(self) -> str:
        return self.sunset_time.strftime('%l:%M %p')

    def get_reference_time(self) -> str:
        return self.reference_time.strftime('%l:%M %p')

Any advice is much appreciated.非常感谢任何建议。 Thank you!谢谢!

Figured out the answer here.在这里找到答案。 For anyone else struggling with this, the label update call needs to be enqueued before the label itself is added to the UI.对于其他为此苦苦挣扎的人,在将标签本身添加到 UI 之前,需要将标签更新调用加入队列。 Here's code for app.py that seems to work as expected:这是 app.py 的代码,它似乎按预期工作:

from appJar import gui
import datetime, pyowm, WeatherUtils
from WeatherInfo import WeatherInfo

LES_LAT = 40.714080
LES_LON = -73.985890

app = gui()
app.setBg("#363636")
app.setSize("Fullscreen")

# Date and time
def addDateTimeLabel(labelName: str, text: str) -> None:
    app.addLabel(labelName, text)
    app.setLabelFg(labelName, "White")
    app.setLabelFont(labelName, size=32)

now = datetime.datetime.now()
date = now.strftime("%A %B %e, %Y")
time = now.strftime("%l:%M %p")

addDateTimeLabel("date", date)
addDateTimeLabel("time", time)

# Weather Data Fetch
def setCurrentWeatherData(weather_data: WeatherInfo) -> None:
    weather_string = 'Current: ' + weather_data.status + " " + str(weather_data.temp) + ", High: " + str(weather_data.max_temp) + ", Low: " + str(weather_data.min_temp)
    app.queueFunction(app.setLabel, 'current_weather', weather_string)

app.thread(WeatherUtils.getCurrentTemperature, LES_LAT, LES_LON, setCurrentWeatherData)

# Weather UI
addDateTimeLabel('current_weather', 'Loading...')
addDateTimeLabel('forecast', 'Loading...')

app.go()

If anyone has any insight into why this is the case, it'd be much appreciated, but figured I'd answer the question in case others were running into the same issue.如果有人对为什么会出现这种情况有任何见解,我们将不胜感激,但我想我会回答这个问题,以防其他人遇到同样的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM