让Kivy用Clock更新异步图像

时间:2015-12-02 14:31:54

标签: python-2.7 kivy

我有几个由ScreenManager运行的屏幕,而在一个屏幕上我有一个图像加载了从URL获取的图像(用于天气)。我的问题是,我似乎无法获得网址&图像定期更新:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty, StringProperty
from kivy.clock import Clock
import pyowm

class Weather(Screen):
    addr = StringProperty()

    def wx_forecast(self):
        owm = pyowm.OWM('******************')
        observation = owm.weather_at_place('London,uk')
        w = observation.get_weather()
        i = w.get_weather_icon_name()
        addr = str("http://openweathermap.org/img/w/" + i + ".png")
        print(addr)
        return addr

    def update(self):
        Clock.schedule_interval(self.ids.wxlabel.source, 45)
    #def update(self):
        #self.wx_forecast()
        #Clock.schedule_interval(lambda dt: self.ids.wxlabel, 30)

class ScreenManagement(ScreenManager):
    pass

smsettings = Builder.load_string( """
ScreenManagement:
    Weather:

<Weather>:
    name: 'weather'
    FloatLayout:
        BoxLayout:
            AsyncImage:
                id: wxlabel
                source: root.wx_forecast()
        BoxLayout:
            size_hint_y: None
            height: 30
            Button:
                text: 'Change Location'
                on_press:
            Button:
                text: 'More Details'
                on_release:
""" )

class HomeUtilities(App):
    def build(self):
        return smsettings

if __name__=='__main__':
    HomeUtilities().run()

def update是我遇到问题的地方;我已尝试再次调用函数wx_forecast,甚至尝试Weather类之外的其他函数,但所有函数都是str: has no attribute 'wx_forecast'

所以我相信我在正确的路线上?或者我可能完全错了,但我尝试的任何东西似乎都没有用。即使把self.ids.wxlabel.source.sunny.png调出来str: has no attribute image,但文件就在那里。

0 个答案:

没有答案