Python 2.7 让Kivy使用时钟更新异步映像

Python 2.7 让Kivy使用时钟更新异步映像,python-2.7,kivy,Python 2.7,Kivy,我有几个屏幕是由ScreenManager运行的,在其中一个屏幕上,我有一张图片,上面有一张从URL(天气)获取的图片。我的问题是,我似乎无法定期更新URL和图像: from kivy.app import App from kivy.lang import Builder from kivy.uix.screenmanager import ScreenManager, Screen from kivy.properties import ObjectProperty, StringPrope

我有几个屏幕是由ScreenManager运行的,在其中一个屏幕上,我有一张图片,上面有一张从URL(天气)获取的图片。我的问题是,我似乎无法定期更新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()
从kivy.app导入应用
从kivy.lang导入生成器
从kivy.uix.screenmanager导入screenmanager,屏幕
从kivy.properties导入ObjectProperty、StringProperty
从kivy.clock导入时钟
导入pyowm
班级天气(屏幕):
addr=StringProperty()
def wx_预测(自我):
owm=pyowm.owm('*********************')
观测=伦敦(英国)的天气
w=观测。获取天气()
i=w.获取天气图标名称()
地址=str(“http://openweathermap.org/img/w/“+i+”.png”)
打印(地址)
返回地址
def更新(自我):
时钟计划间隔(self.ids.wxlabel.source,45)
#def更新(自我):
#self.wx_forecast()
#时钟计划间隔(lambda dt:self.ids.wxlabel,30)
类屏幕管理(屏幕管理器):
通过
smsettings=Builder.load\u字符串(“”)
屏幕管理:
天气:
:
名称:“天气”
浮动布局:
盒子布局:
异步映像:
id:wxlabel
来源:root.wx_forecast()
盒子布局:
尺寸提示:无
身高:30
按钮:
文本:“更改位置”
新闻界:
按钮:
文本:“更多详细信息”
发布时:
""" )
类家庭实用程序(应用程序):
def生成(自):
返回设置
如果“名称”=“\uuuuuuuu主要”:
HomeUtilities().run()
其中
def更新
是我遇到问题的地方;我尝试再次调用函数
wx\u forecast
,甚至尝试了
Weather
类之外的另一个函数,但它所说的是
str:没有属性“wx\u forecast”


所以我相信我是对的?或者我可能完全错了,但我尝试的东西似乎都不管用。即使把
self.ids.wxlabel.source.sunny.png
放进去,也会出现
str:没有属性图像
,但文件就在那里。

我不完全理解你的问题,但是Clock.schedule\u间隔行是错误的(你的lambda函数什么都不做),而且
self.ids.wxlabel.source.sunny.png
没有任何意义(源代码没有名为
sunny
的属性-您需要类似于
self.ids.wxlabel.source='sunny.png'
。您可以发布一个可运行的示例来演示问题吗?嗨,恶劣,我现在无法访问在线计算机,但很快就会发布可运行的示例。基本上,我不想重新加载图像小部件perio。)Dicaly,图像为wx_forecast提供的地址的异步图像。不幸的是,也尝试了您的方法,并收到错误lamda无法包含分配。将与联系,提前感谢您。嗨,很抱歉,我花了这么长时间才进行此编辑,但该文件现在应该可以运行;希望您或任何人阅读这篇文章会有所帮助,我所要达到的目标也是有道理的